contents   index   previous   next



Arrays in Visual Basic

 

In Visual Basic there are two types of arrays; fixed-size arrays that always remain the same size, and dynamic arrays whose size can change at run-time. You declare a visual basic array using the Dim statement [08]. The following are examples of fixed arrays declared in Visual Basic:

 

Dim MyIntArray(10) As Integer
Dim MySingleArray(3 to 5) As Single

 

The first example declares an array of (2-byte) integer values with 10 elements. The range of the array index can be 0 to 10 or 1 to 10, depending on the default value indicated with the Option Base statement. The second example declares an array of single values with 3 elements. The indexes for this second array go from 3 to 5.

 

The dimensions of a fixed-size array cannot be changed; they remain the same throughout the scope of the array. Visual Basic also allows multidimensional arrays. For example the statement

 

Dim a(1 to 5,1 to 5) As Double

 

Declares a square array of double precision values with 5 rows and 5 columns. Theoretically you can declare arrays with as many as thirty-two dimensions, but it would be hard to find a reason for doing such a thing. Normally your arrays will not have more than three or four dimensions, and if you are planning on using these arrays in Fortran-f90VB subroutines, you should not create arrays with more that seven dimensions [09].

 

You declare a Visual Basic dynamic array by omitting its dimensions in the declaration statement:

 

Dim MyDynArray() As Long

 

You must then redimension the array with a ReDim statement before you use it. For example:

 

ReDim MyDynArray(10)

 

This will allocate 10 entries for array MyDynArray. You can redimension an array multiple times, so the following section of code is valid:

 

Dim B() as long

ReDim B(4)
‘Do something with array B
ReDim B(10)
‘Do something else with array B

 

Each time you execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values to the Empty value (for Variant arrays), to zero (for numeric arrays), to a zero-length string (for string arrays), or to Nothing (for arrays of objects).

 

This is useful when you want to prepare the array for new data, or when you want to shrink the size of the array to take up minimal memory. Sometimes you may want to change the size of the array without losing the data in the array. You can do this by using ReDim with the Preserve keyword. For example, you can enlarge an array by one element without losing the values of the existing elements using the UBound function to refer to the upper bound:

 

ReDim Preserve MyDynArray(UBound(MyDynArray) + 1)

 

Only the upper bound of the last dimension in a multidimensional array can be changed when you use the Preserve keyword; if you change any of the other dimensions, or the lower bound of the last dimension, a run-time error occurs. Thus, you can use code like this:

 

ReDim Preserve Matrix(10, UBound(Matrix, 2) + 1)

 

But you cannot use this code:

 

ReDim Preserve Matrix(UBound(Matrix, 1) + 1, 10)

 

In Visual Basic, you can declare arrays of any standard types, including strings, variants, objects and User Defined Types (UDT). Arrays created in Visual Basic are always Safe Arrays.

 

In Visual Basic you can also define procedures that receive array arguments. Array arguments can only be declared and passed by reference (ByRef).

 

Safe Arrays in Fortran