<< Back to Index
Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number. Arrays have both upper and lower bounds, and the elements of the array are contiguous within those bounds. Because Visual Basic allocates space for each index number, avoid declaring an array larger than necessary.
In Visual Basic there are two types of arrays: a fixed-size array which always remains the same size, and a dynamic array whose size can change at run-time. Dynamic arrays are discussed in more detail in the section "Dynamic Arrays" later in this chapter.
To specify a lower bound, provide it explicitly (as a Long data type) using the To keyword:
With Visual Basic, you can declare arrays of multiple dimensions. For example, the following statement declares a two-dimensional 10-by-10 array within a procedure:
Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number. Arrays have both upper and lower bounds, and the elements of the array are contiguous within those bounds. Because Visual Basic allocates space for each index number, avoid declaring an array larger than necessary.
Note The arrays
discussed in this section are arrays of variables, declared in code.
They are different from the control arrays you specify by setting the
Index property of controls at design time. Arrays of variables are
always contiguous; unlike control arrays, you cannot load and unload
elements from the middle of the array.
All the elements in an
array have the same data type. Of course, when the data type is Variant,
the individual elements may contain different kinds of data (objects,
strings, numbers, and so on). You can declare an array of any of the
fundamental data types, including user-defined types (described in the
section, "Creating Your Own Data Types," in "More About Programming")
and object variables (described in "Programming with Objects").In Visual Basic there are two types of arrays: a fixed-size array which always remains the same size, and a dynamic array whose size can change at run-time. Dynamic arrays are discussed in more detail in the section "Dynamic Arrays" later in this chapter.
Declaring Fixed-Size Arrays
There are three ways to declare a fixed-size array, depending on the scope you want the array to have:- To create a public array, use the Public statement in the Declarations section of a module to declare the array.
- To create a module-level array, use the Private statement in the Declarations section of a module to declare the array.
- To create a local array, use the Private statement in a procedure to declare the array.
Setting Upper and Lower Bounds
When declaring an array, follow the array name by the upper bound in parentheses. The upper bound cannot exceed the range of a Long data type (-2,147,483,648 to 2,147,483,647). For example, these array declarations can appear in the Declarations section of a module:Dim Counters(14) As Integer ' 15 elements.
Dim Sums(20) As Double ' 21 elements.
To create a public array, you simply use Public in place of Dim:Public Counters(14) As Integer
Public Sums(20) As Double
The same declarations within a procedure use Dim:Dim Counters(14) As Integer
Dim Sums(20) As Double
The first declaration creates an array with 15
elements, with index numbers running from 0 to 14. The second creates an
array with 21 elements, with index numbers running from 0 to 20. The
default lower bound is 0. To specify a lower bound, provide it explicitly (as a Long data type) using the To keyword:
Dim Counters(1 To 15) As Integer
Dim Sums(100 To 120) As String
In the preceding declarations, the index numbers of Counters
range from 1 to 15, and the index numbers of Sums
range from 100 to 120.Arrays that Contain Other Arrays
It's possible to create a Variant array, and populate it with other arrays of different data types. The following code creates two arrays, one containing integers and the other strings. It then declares a third Variant array and populates it with the integer and string arrays.Private Sub Command1_Click()
Dim intX As Integer ' Declare counter variable.
' Declare and populate an integer array.
Dim countersA(5) As Integer
For intX = 0 To 4
countersA(intX) = 5
Next intX
' Declare and populate a string array.
Dim countersB(5) As String
For intX = 0 To 4
countersB(intX) = "hello"
Next intX
Dim arrX(2) As Variant ' Declare a new two-member
' array.
arrX(1) = countersA() ' Populate the array with
' other arrays.
arrX(2) = countersB()
MsgBox arrX(1)(2) ' Display a member of each
' array.
MsgBox arrX(2)(3)
End Sub
Multidimensional Arrays
Sometimes you need to keep track of related information in an array. For example, to keep track of each pixel on your computer screen, you need to refer to its X and Y coordinates. This can be done using a multidimensional array to store the values.With Visual Basic, you can declare arrays of multiple dimensions. For example, the following statement declares a two-dimensional 10-by-10 array within a procedure:
Static MatrixA(9, 9) As Double
Either or both dimensions can be declared with explicit lower bounds:Static MatrixA(1 To 10, 1 To 10) As Double
You can extend this to more than two dimensions. For example:Dim MultiD(3, 1 To 10, 1 To 15)
This declaration creates an array that has three
dimensions with sizes 4 by 10 by 15. The total number of elements is the
product of these three dimensions, or 600.
Note When
you start adding dimensions to an array, the total storage needed by
the array increases dramatically, so use multidimensional arrays with
care. Be especially careful with Variant arrays, because they are larger
than other data types.
Using Loops to Manipulate Arrays
You can efficiently process a multidimensional array by using nested For loops. For example, these statements initialize every element inMatrixA
to a value based on its location in the array:Dim I As Integer, J As Integer
Static MatrixA(1 To 10, 1 To 10) As Double
For I = 1 To 10
For J = 1 To 10
MatrixA(I, J) = I * 10 + J
Next J
Next I