Declaring Arrays

 

Arrays are declared in the same manner as other variables, except that the array bounds are coded in parentheses following the variable name.

 

For fixed-length arrays, you must specify the upper bound of the array; you can optionally specify the lower bound.  By default, the lower bound is 0 (however, see the note on the "Option Base" statement further below). For variable-length arrays, you do not specify any bounds; you just code an empty set of parentheses.

 

Arrays can have up to 60 dimensions (although it is rare that you would have more than three dimensions; one or two is the norm). 

 

The syntax for declaring an array is:

 

[Dim | Private | Public | Static | Global] arrayname([lowerbound To] upperbound[, …])   [As datatype]

 

Sample declarations:

 

Array Declaration

Notes

Dim aintCount(9) As Integer

declares a 10-element array, indexed 0 to 9

Dim aintCount(0 To 9) As Integer

same as above, with explicit lower bound

Dim aintCount(1 To 10) As Integer

declares a 10-element array, indexed 1 To 10

Dim aintCount(3 To 12) As Integer

declares a 10-element array, indexed 3 To 12

Dim aintCount(-4 To 5) As Integer

declares a 10-element array, indexed -4 To 5

Dim aintCount() As Integer

declares a variable-length array whose bounds will be determined at run-time

 

Note from the above declarations that the lower bound is not restricted to 0 or 1, and it can even be negative.

 

To refer to an individual element of an array in a procedural statement, place the desired index in parentheses next to the array name. For example, the following statement will display the 5th element of aintCount  on the form (assuming the first or second declaration above):

 

      Print aintCount(4)

 

Following are some examples of multi-dimensional array declarations:

 

The following declares a 2-dimensional array (4 rows indexed 1 to 4, by 5 columns indexed 1 to 5):

 

Dim asngSales(1 To 4, 1 To 5) As Single

 

The following declares a 3-dimensional array (the first dimension has 4 elements indexed 0 to 3, within that, the second dimension has 12 elements indexed 1 to 12, and within that, the third dimension has 5 elements indexed 2 to 6):

 

Dim asngResults(3, 1 To 12, 2 To 6) As Single

 

To refer to an individual element of a multi-dimensional array in a procedural statement, place the desired indices in parentheses next to the array name (you must have one index per dimension, separated by commas).  Examples:

 

      Print asngSales(2, 3)

      Print asngResults(0, 11, 5)

 

 

Note:    As explained above, by default, if you do not explicitly code a lower bound, the lower bound of an array is 0. However, there is a statement called Option Base, which, if you code Option Base 1, will force VB to default the lower bound of arrays to 1. The Option Base statement can only have the number 0 or 1 after it, and 0 is the default – so you never need to code Option Base 0. The Option Base statement, if used, would be coded at the beginning of a code module and is enforced only at the module level. Recommendation: Do not use Option Base; explicitly code the lower bound of your array declarations.