| Bookshelf Home | Contents | Index | Search | PDF | ![]() ![]() |
Siebel VB Language Reference > Language Overview >
Dynamic Arrays
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 PreserveArrayName(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
cflowper=2
ReDim varray(cflowper)
For x= 1 to cflowper
varray(x)=500
Next x
aprate=10
If aprate>1 then
aprate=aprate/100
End If
netpv=NPV(aprate,varray())
msgtext="The net present value is: "
msgtext=msgtext & Format(netpv, "Currency")
TheApplication.raiseErrorText msgtext
End SubIf 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.
| Bookshelf Home | Contents | Index | Search | PDF | ![]() ![]() |
Siebel VB Language Reference Published: 18 June 2003 |