Export (0) Print
Expand All

Multidimensional Arrays in Visual Basic

An array can have one dimension or more than one. If it has more than one, it is called a multidimensional array. Note that having multiple dimensions is not the same thing as a jagged array, which has other arrays as its elements.

The dimensionality or rank of an array corresponds to the number of indexes used to identify an individual element. You can specify up to 32 dimensions, although more than three is rare. The following example declares a two-dimensional array variable and a three-dimensional array variable.

Dim populations(200, 3) As Long
Dim matrix(5, 15, 10) As Single

The total number of elements is the product of the lengths of all the dimensions. In the preceding example, populations has a total of 804 elements (201 x 4), and matrix has 1056 elements (6 x 16 x 11). Each index ranges from 0 through the length specified for its dimension.

A two-dimensional array is also called a rectangular array.

NoteNote:

When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.

All arrays inherit from the Array class in the System namespace, and you can access the methods and properties of Array on any array. The following members of Array can be useful:

  • The Rank property returns the array's rank (number of dimensions).

  • The GetLength method returns the length along the specified dimension.

  • The GetUpperBound method returns the highest index value for the specified dimension. The lowest index value for each dimension is always 0.

  • The Length property returns the total number of elements in the array.

  • The Array.Sort method sorts the elements of a one-dimensional array.

Note that GetLength and GetUpperBound take a 0-based argument for the dimension you are specifying.

Community Additions

ADD
Show:
© 2014 Microsoft