
Collections | 67
Variables and
Data Types
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Array declarations can:
• Call the array’s constructor implicitly or explicitly
• Specify an initial size for each dimension or leave the initial size unspecified
• Initialize the elements of the array or not
In VB 6, the programmer could specify both the lower and upper bounds of any
array dimension. With .NET, all Visual Basic arrays have a lower bound of zero.
The statement:
Dim myArray(5) As Integer
declares an array with six elements, numbered zero through five.
Arrays can include multiple dimensions. The following example declares and
initializes a two-dimensional array:
Dim rectArray(,) As Integer = {{1, 2, 3}, {4, 5, 6}}
The following code displays the contents of this array:
Debug.Write(rectArray(0, 0))
Debug.Write(rectArray(0, 1))
Debug.WriteLine(rectArray(0, 2))
Debug.Write(rectArray(1, 0))
Debug.Write(rectArray(1, 1))
Debug.WriteLine(rectArray(1, 2))
' ----- The output is:
123
456
The upper bound of any array dimension can be modified using the ReDim
statement.
ReDim [Preserve] arrayName(newUpperBound)
The Preserve qualifier retains any existing values in the array; all array elements
are cleared in the absence of this qualifier. When using
Preserve, only the last
dimension of an array can have its upper bound modified. The number of dimen-
sions ...