Working With Arrays in VBScript - Creating Arrays
(Page 2 of 5 )
Arrays are typically created using the Dim statement. The Dim statement reserves space in memory for a provided variable reference. This statement is extended to allow creation of arrays.
Try to think of an array as a column in a spreadsheet. An array contains several data elements, much like the individual cells in a spreadsheet. You could, for instance, have a list of names.
Dim arrNames(2)
arrNames(0) = "John Doe"
arrNames(1) = "Jane Smith"
arrNames(2) = "Dick Tracy"
Here the Dim statement is used to create an array. This is denoted by the number in parenthesis that follows the array name. This number provides the Upper Boundary of the array.
Data elements are stored in an array in a zero-based fashion. This means that each element can be referenced in the array by a number starting from zero. So an array with an upper boundary of 2 can hold three elements: 0, 1, and 2.
We can assign values to these elements in just this fashion. Notice how I’ve used the array name followed by the element’s number to assign these values. Values in an array are read/write.
Dim arrNumbers(2)
arrNumbers(0) = 5
arrNumbers(1) = 25
arrNumbers(2) = 7
The first example created a String Array, or an array containing string values. You can just as easily create an Integer Array, or an array containing integers. Any valid variant value can be assigned to an array element.
Dim arrNames(2)
arrNames(3) = "Rob Dunham"
Be aware of your array’s boundaries. Attempting to assign a value like this will result in a “subscript out of range” error because there is no element 3 in this array.
Next: Using Values from an Array >>
More Windows Scripting Articles
More By Nilpo