Arrays (VBscript)

|

The VBScript language provides support for arrays. You declare an array using the Dim statement, just as you did with variables:

Dim States(50)

The statement above creates an array with 51 elements. Why 51? Because VBScript arrays are zero-based, meaning that the first array element is indexed 0 and the last is the number specified when declaring the array.

You assign values to the elements of an array just as you would a variable, but with an additional reference (the index) to the element in which it will be stored:
States(5) = "California"
States(6) = "New York"

Arrays can have multiple dimensions-VBScript supports up to 60. Declaring a two dimensional array for storing 51 states and their capitals could be done as follows:

Dim StateInfo(50,1)
To store values into this array you would then reference both dimensions.

StateInfo(18,0) = "Michigan"
StateInfo(18,1) = "Lansing"

VBScript also provides support for arrays whose size may need to change as the script is executing. These arrays are referred to as dynamic arrays. A dynamic array is declared without specifying the number of elements it will contain:

Dim Customers()

The ReDim statement is then used to change the size of the array from within the script:
ReDim Customers(100)

There is no limit to the number of times an array can be re-dimensioned during the execution of a script. To preserve the contents of an array when you are re-dimensioning, use the Preserve keyword:
ReDim Preserve Customers(100)