Udostępnij za pośrednictwem


Array Size Declaration for Visual Basic 6.0 Users

Visual Basic 2008 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 2005

In Visual Basic 2008, an array does not have a permanent size. The preceding example can be rewritten as either of the following declarations:

Dim Month(11) AsInteger   ' Reserves 12 elements -- (0) through (11).
Dim Month() AsInteger = NewInteger(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() AsInteger = {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 2008, the number of dimensions must be fixed. The following example declares a three-dimensional array:

Dim Point(,,) AsDouble

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

See Also

Concepts

Programming Element Support Changes Summary

Reference

Dim Statement (Visual Basic)

New (Visual Basic)

ReDim Statement (Visual Basic)

Other Resources

Arrays in Visual Basic