Any of the data types described above, including objects, support array variables. The rank (dimension) of an array can be from 1 to 4. The number of array elements within a dimension is limited by available memory.
The first index in an array is always element 0. When you declare an array size, you are specifying the upper bound for a dimension. So, the number of elements for a dimension is always equal to the upper bound+1. For example:
Dim Count(9) As Integer ' Allocates array of 10 elements
Versions of Basic such as VB6 supported means for defined ranges of indices that started with an arbitrary first index number (e.g. “10 to 20”) and also statements such as “Option Base 1” that forced the first index to always be 1. However, VB.Net always starts arrays with index 0 and this is the convention that is supported in GPL.
The Dim statement is used to declare an array variable. The supported forms of this statement are as follows:
Dim MyArray(3, 4) As Integer
Dim MyArray(,) As Integer
The first statement specifies a 2-dimensional array with 4 elements (0 to 3) in the first dimension and 5 (0 to 4) in the second, for a total of 20 elements. These elements are allocated when the Dim statement is executed.
The second statement simply specifies a 2-dimensional array, but does not allocate any elements. Before you can use the array, you must either assign an array to it, or you must use a ReDim statement to allocate the elements.
When array elements are allocated, numeric arrays have the value 0 and object arrays have the value Nothing. Initialization of array values using an “=” clause is not supported in GPL.
Once an array has been declared and its dimensionality established, the ReDim instruction can be used to initialize or change the number of elements within any dimension. ReDim can be applied to any array, so no distinction is made between dynamically sizeable arrays and fixed arrays. However, ReDim cannot be used to change the rank of an array and ReDim cannot be used to initially declare an array. Some examples of ReDim are as follows:
Dim Count() As Integer
ReDim Count(9)
Dim TwoDCount(2,3) As Integer
ReDim TwoDCount(1,100)
Whole arrays may be assigned to each other with a single statement. When that occurs, the data are not actually copied, but a pointer to the data in the right-hand array is copied to the left-hand array variable so that both array variables access the same data. This behavior is similar to object variables. For example:
Dim CountA(9) As Integer
Dim CountB() As Integer
CountB = CountA ' CountB now refers to the same
' data as CountA
When single array elements are passed as procedure arguments, they behave the same as non-array variables of the same type. When whole array elements are passed as procedure arguments, pointers to either the array value () or the array variable () are passed, and the behavior is the same as when passing objects. ByVal ByRef
All arrays of variables are members of the built-in Array class. You can use properties of this class (Table 19-5) to determine the properties of any variable array.
| Property | Description |
|---|---|
|
array.GetUpperBound (dim) |
Returns the upper bound for a particular dimension of an array. The lower bound is always 0, so the total number of elements in this dimension is one greater than the upper bound. |
|
array.Length |
The total number of elements in the entire array, in all dimensions. |
|
array.Rank |
Returns the rank, which is the number of dimensions, in the array. |
These property methods may only be used with an entire array, not with a subset or individual array element.
Do not be confused when using the Length property with string arrays, for example, if you declare: Dim sarray(3) As String:
sarray.Length is the number of elements in the array, in this case 4 (from 0 to 3).
sarray(0).Length is the length of the string contained in sarray(0), initially 0.