This documentation is archived and is not being maintained.

Array Usage

Visual Studio .NET 2003

You can declare an array to work with a set of values of the same data type. An array is a single variable with many compartments to store values, while a scalar variable (that is, not an array) has only one storage compartment in which it can store only one value. You can refer to the array as a whole when you want to refer to all the values it holds, or you can refer to its individual elements one at a time.

For example, to store daily expenses for each day of the year, you can create one array with 366 elements, rather than declaring 366 variables. Each element in the array contains one value, which you access by specifying the element's index. The following example declares the array variable CurExpense, initializes it to have 366 elements, and then assigns an initial value of 20 to each element.

Sub FillArray() 
   Dim CurExpense(365) As Decimal   ' Allocates (0) through (365). 
   Dim I As Integer 
   For I = 0 to 365 
      CurExpense(I) = 20.00 
   Next I 
End Sub 

You can mix data types in an array if it is declared of type Object. The following example stores employee information in the array variable EmployeeData.

Dim EmployeeData(3) As Object   ' Allocates (0) through (3). 
EmployeeData(0) = "Ron Bendel" 
EmployeeData(1) = "4242 Maple Blvd" 
EmployeeData(2) = 48 
EmployeeData(3) = "06-09-1953" 

Multidimensional Arrays

In Visual Basic, you can declare arrays with up to 32 dimensions. For example, the following statement declares a two-dimensional array with 5 rows and 10 columns.

Dim Rectangle(4, 9) As Single   ' (0) through (4), (0) through (9). 

The total number of elements is the product of the sizes of all the dimensions, in this case 50.

The rank of an array is held in its Rank property, and the length of each dimension is returned by the GetLength method. The lowest subscript value for a dimension is always 0, and the highest subscript value is returned by the GetUpperBound method for that dimension. Note that the argument you pass to GetLength and GetUpperBound (the dimension for which you want the length or the highest subscript) is 0-based.

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

You can efficiently process a multidimensional array by using nested For loops. For example, the following statements initialize every element in MatrixA to a value between 0 and 99, based on its location in the array.

Dim I, J As Integer 
Dim MaxDim0, MaxDim1 As Integer   ' Highest subscripts, not lengths. 
Dim MatrixA(9, 9) As Double 
MaxDim0 = MatrixA.GetUpperBound(0)   ' Maximum for first dimension. 
MaxDim1 = MatrixA.GetUpperBound(1)   ' Maximum for second dimension. 
For I = 0 To MaxDim0 
   For J = 0 To MaxDim1 
      MatrixA(I, J) = (I * 10) + J 
   Next J 
Next I 

You can obtain the overall size of an array of any rank from its Length property. This represents the total number of elements currently contained in the array, not the number of bytes they consume in storage. In the previous example, MatrixA.Length would return 100.

See Also

Object Data Type | Array.Rank Property | Array.GetLength Method | Array.GetUpperBound Method | Array.Length Property | Arrays Overview | Declaring Array Variables | Advanced Features of Arrays | Arrays of Arrays | Collections as an Alternative to Arrays