Sometimes you may not know how large to make an array. Instead of making it large enough to hold the maximum number of data (which means that most of the array may be empty), you can declare a.dynamic array. The size of a dynamic array can vary during the course of the program. Or, you might need an array until the user has entered a bunch of data and the application has processed it and displayed the results. Why keep all the data in memory when it is no longer needed? With a dynamic array, you can discard the data and return the resources it occupied to the system.
To create a dynamic array, declare it as usual with the Dim statement (or Public or Private), but don’t specify its dimensions:
Dim DynArray()
Later in the program, when you know how many dements you want to store in the array, use the ReDim statement to redimension the array, this time with its actual size. In the following example, UserCount is a user-entered value:
ReDim DynArray(UserCount)
The ReDim statement can appear only in a procedure. Unlike the Dim statement, ReDim is executable-it forces the application to carry out an action at runtime. Dim statements aren’t executable, and they can appear outside procedures.
A dynamic array also can be redimensioned to multiple dimensions. Declare it with the Dim statement outside any procedure as follows:
Dim Matrix() As Double
and then use the ReDim statement in a procedure to declare a three-dimensional array:
ReDim Matrix(9, 9, 9)
Note that the ReDim statement can’t change the type of the array. Moreover, subsequent ReDim statements can change the bounds of the array Matrix, but not the number of its dimensions. For example, you can’t use the statement ReDimMatrix(99, 99) later in your’ code. Once an array has been redimensioned the first time, its number of dimensions can’t change. In the previous example, the Matrix array would remain three-dimensional.
Arrays of Arrays
One of the possibilities introduced to the Visual Basic language with the Variant data type is that of creating complex structures, such as arrays of arrays. If an array is ):ie declared as Variant, you can assign other types to its elements, including arrays suppose have declared and populated two arrays, one with integers and . another. with strings; You can then declare a Variant array with two elements and populate it with the two arrays as follows:
Dim IntArray(10) As Integer
Dim StrArray(99) As String
Dim BigArray(2) As . Variant
populate array IntArray)
(Populate array StrArray)
BigArray(0) = IntArray()
“BigArray(1) =·StrArray()
Notice the empty parentheses after the array names, This notation signifies the entire array and is used in passing arrays to Dynamic Link Libraries (DLLs), which are discussed further in Chapter 13, The Windows API. The empty parentheses are equivalent to IntArray(0) and StrArray(0).Had you supplied an index, part of the array would have been assigned to the corresponding element of BigArray. For example.fhe following statement assigns to the first element of BigArray the last ) nine element(2 through 10) of the array IntArray:
BigArray(0) =. IntArray(2)
Big array was declared as a one-dimensional array, but because each of its elememts is an array you must use two indices to aCCeSSit. To access the third element of IntArray in BigArray, ls the indices 0 and 2. Likewise, the tenth element of the StrArrayin-BigArniy is BigArray(1) (9).The notation is quite usual, but the indties of the BigArray must be entered in separate parentheses.