This documentation is archived and is not being maintained.

Array Size Declaration Changes in Visual Basic

Visual Basic .NET updates array size declaration for interoperability with the common language runtime.

Visual Basic 6.0

In Visual Basic 6.0, you can specify the size of an array in its declaration, as in the following example:

Dim Month(0 To 11) As Integer 

This causes the array to have a fixed size, which you cannot change with the ReDim statement.

Visual Basic .NET

In Visual Basic .NET, an array cannot have a fixed size. The preceding example can be rewritten as either of the following declarations:

Dim Month(11) As Integer   ' Reserves 12 elements -- (0) through (11).
Dim Month() As Integer = New Integer(11) {} 

These declarations are equivalent. Each specifies an initial size, which you can change during execution with the ReDim statement. To initialize the elements, you can use the following syntax:

Dim Month() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} 

If you specify –1 for one of the dimensions, the array does not contain any elements. A ReDim statement can change an array back and forth between empty and nonempty.

Number of Dimensions

Although an array's size can change in Visual Basic .NET, the number of dimensions must be fixed. The following example declares a three-dimensional array:

Dim Point( , , ) As Double 

The ReDim statement can set or change the size of each dimension, but the array always remains three-dimensional.

See Also

Arrays | Dim Statement | New | ReDim Statement | Programming Element Support Changes Summary

Show: