Siebel VB Language Reference > VB Language Overview >

Dynamic Arrays in VB

Dynamic arrays differ from fixed arrays because a subscript range for the array elements is not specified when the array is dimensioned. Instead, the subscript range is set using the ReDim statement. With dynamic arrays, the number of array elements can be set based on other conditions in your procedure. For example, you may want to use an array to store a set of values entered by the user, but you may not know in advance how many values the user will enter. In this case, you dimension the array without specifying a subscript range and then execute a ReDim statement each time the user enters a new value. Or you may want to prompt for the number of values to be entered and execute one ReDim statement to set the size of the array before prompting for the values.

If you use ReDim to change the size of an array and want to preserve the contents of the array at the same time, be sure to include the Preserve argument to the ReDim statement:

Redim Preserve ArrayName(n)

The following procedure uses a dynamic array, varray, to hold cash flow values entered by the user:

Sub main
   Dim aprate as Single
   Dim varray() as Double
   Dim cflowper as Integer
   Dim msgtext as String
   Dim x as Integer
   Dim netpv as Double
   ReDim varray(cflowper)
   For x= 1 to cflowper
   Next x
   If aprate>1 then
   End If
   msgtext="The net present value is: "
   msgtext=msgtext & Format(netpv, "Currency")
   TheApplication.raiseErrorText msgtext
End Sub

If you declare a dynamic array (with a Dim statement) before using it, the maximum number of dimensions it can have is 8. To create dynamic arrays with more dimensions (up to 60), do not declare the array at all and use only the ReDim statement inside your procedure.

Siebel VB Language Reference