Working with one-dimensional arrays
In VB, one-dimensional arrays are declared and stored in a linear sequence to store a collection of elements of the same data type. Here’s the syntax for declaring a one-dimensional array in VB:
Dim arrayName(size) As DataType
An example of an array of 10 integers would look like this:
Dim myInts(10) As Integer
The preceding code will create a variable named myInts
, which is an array with 10 elements. In VB, like most programming languages, the elements are indexed from 0 to size-1. In this example, they would be indexed from 0 to 9.
Array elements in VB are accessed using their index, starting from 0. You can retrieve or assign values to array elements as follows:
Dim value As Integer = myInts(3) myInts(3) = 5
This code example accesses the fourth element. First, it retrieves the value and then assigns a new value to the fourth element.
You can determine the length (number of elements) of a one-dimensional array using...