Export (0) Print
Expand All

Declaring Array Variables

Array variables are declared the same way as other variables, using the Dim statement. You follow the variable name with one or more pairs of parentheses to indicate that it is an array rather than a scalar (a variable containing a single value).

To declare a single-dimensional array variable

  • In your declaration, add one pair of parentheses after the variable name, as in the following example:
    Dim MySingleArray() As Integer 
    

To declare a multidimensional array variable

  • In your declaration, add one pair of parentheses after the variable name and place commas inside the parentheses to separate the dimensions, as in the following example:
    Dim My4DArray(,,,) As Short   ' Four-dimensional array.
    

To declare a jagged array variable

  • In your declaration, add as many pairs of parentheses after the variable name as there are levels of nested arrays, as in the following example:
    Dim MyJaggedArray()()() As Byte   ' Array of arrays of Byte arrays. 
    

Initializing Arrays

You can initialize an array variable as part of its declaration. You can do one of the following in the declaration statement:

  • Specify the initial length of one or more of the dimensions in the parentheses following the variable name, without assigning an array object to the variable.
  • Assign an array object to the variable, using the New clause. When you use a New clause, you must follow it with braces ({}), even if they are empty.
  • Assign an array object to the variable and supply initial lengths in the New clause.
  • Assign an array object to the variable and supply initial element values in the New clause. You can supply both lengths and values in the same New clause.

If you specify dimension lengths in the parentheses following the variable name, you must use a subsequent assignment statement to assign an array object to the variable. The following sample declarations show valid and invalid syntax for a single-dimensional array variable:

Dim BA(2) As Byte   ' Currently contains Nothing (no object). 
Dim BA(2) As Byte = New Byte()   ' INVALID (New after length specified).
Dim BA() As Byte = New Byte() {}   ' Empty Byte array object. 
Dim BA() As Byte = New Byte()   ' INVALID (missing braces). 
Dim BA() As Byte = New Byte(2)   ' INVALID (missing braces). 
Dim BA() As Byte = New Byte() {0,1,2}   ' (0) through (2). 
Dim BA() As Byte = New Byte(2) {0,1,2}   ' (0) through (2). 

You can initialize a multidimensional array variable in a similar manner. The following sample declarations show a two-dimensional array variable being declared as an Short array with 2 rows and 2 columns.

Dim S2X2(1, 1) As Short   ' (0) through (1), (0) through (1). 
Dim S2X2(1, 1) As Short = New Short(,)   ' INVALID (New after lengths). 
Dim S2X2(,) As Short = New Short(,)   ' INVALID (missing braces). 
Dim S2X2(,) As Short = New Short(1, 1)   ' INVALID (missing braces). 
Dim S2X2(,) As Short = New Short(,) {}   ' Empty array object. 
Dim S2X2(,) As Short = New Short(1, 1) {}   ' Elements have default value. 
Dim S2X2(,) As Short = New Short(,) {{5, 6}, {7, 8}}   ' Four elements. 

The first argument represents the rows; the second argument represents the columns. In the arrays in the preceding declarations, the index values for both the row and the column range from 0 through 1.

When you initialize a jagged array variable, you can specify the dimension lengths only for the top-level array. The following sample declarations show valid and invalid syntax for an array of arrays of Byte elements:

Dim JB(1)() As Byte   ' Array of two arrays of Byte elements. 
Dim JB(1)(1) As Byte   ' INVALID (can only specify top-level size). 
Dim JB(1)() As Byte = New Byte()()   ' INVALID (New after lengths). 
Dim JB()() As Byte = {New Byte() {}, New Byte() {}}   ' Empty arrays. 
Dim JB()() As Byte = {New Byte(1) {}, New Byte(1) {}}   ' Default values. 
Dim JB()() As Byte = {New Byte() {5, 6}, New Byte() {7, 8}} 

In the last of the preceding declarations, JB is initialized to two elements, JB(0) and JB(1), each of which is initialized to a two-element Byte array, the first with element values 5 and 6 and the second with elements 7 and 8.

Array Size Limits

The length of every dimension of an array is limited to the maximum value of a Long data type, which is (2 ^ 64) - 1. The total size limit of an array varies, based on your operating system and how much memory is available. Using an array that exceeds the amount of RAM available on your system is slower because the data must be read from and written to disk.

Note   Because arrays are objects in Visual Basic .NET, it is important to distinguish between an array object and an array variable. An array object, once created, does not change its size or rank. An array variable, however, can have a succession of different arrays assigned to it during its lifetime, and these can be of different sizes and ranks.

Resizing Arrays

You can resize an array at any time by assigning a different array object to the same array variable, using either ReDim or a standard assignment statement. The new array object can have different dimensions, although it must retain the same rank. This helps you manage memory efficiently. For example, you can use a large array for a short time and then ReDim it to a smaller size. This frees up memory you no longer need.

When you ReDim an array, its existing values are normally lost. However, you can retain them by including the Preserve keyword in the ReDim statement. For example, the following statement allocates a new array, initializes its elements from the corresponding elements of the existing MyArray, and assigns the new array to MyArray.

ReDim Preserve MyArray(10, 20) 

In a multidimensional array, you can change only the last dimension when you use Preserve. If you attempt to change any of the other dimensions, a run-time error occurs. If you do not know the current size of a dimension, you can use the GetUpperBound method, which returns the highest subscript value for the dimension you specify.

In the following example, the first line is valid, but the second line is not, because it attempts to change the first of two dimensions.

ReDim Preserve Matrix(Matrix.GetUpperBound(0), Matrix.GetUpperBound(1) + 10) 
ReDim Preserve Matrix(Matrix.GetUpperBound(0) + 10, Matrix.GetUpperBound(1)) 

See Also

Arrays Overview | Array Usage | Advanced Features of Arrays | Arrays of Arrays | Collections as an Alternative to Arrays | Object-Oriented Programming in Visual Basic | Dim Statement | Byte Data Type | New | Short Data Type | Long Data Type | Preserve | Array.GetUpperBound Method | ReDim Statement | New

Show:
© 2014 Microsoft