One of the most common Array definitions says that an Array is a set of sequentially indexed elements (variables or objects) having one and the same data type. The array elements are indexed and each element of an array has its own unique index number.
The VBScript arrays are 0 based, meaning that the array element indexing starts always from 0. The 0 index represents the first position in the array, the 1 index represents the second position in the array, and so forth.
There are two types of VBScript arrays - static and dynamic. Static arrays remain with fixed size throughout their life span. To use static VBScript arrays you need to know upfront the maximum number of elements this array will contain. If you need more flexible VBScript arrays with variable index size, then you can use dynamic VBScript arrays. VBScript dynamic arrays index size can be increased/decreased during their life span.
Use the Dim statement along with the array name to create a static VBScript array:
Dim arrEmployees(20)
The array created above can have a maximum of 21 elements. The number in parentheses defines the array’s upper bound.
There are several ways to create a dynamic VBScript array.
If you use the Dim statement along with the array’s name, without specifying upper bound, you will create a dynamic array:
Dim arrEmployees
In order to use this array you need to use the ReDim statement to define the array’s upper bound:
ReDim arrEmployees(20)
The second way to create a dynamic VBScript array is to use the ReDim statement directly:
ReDim arrEmployees(20)
The third way is to use the VBScript Array function along with a Dim statement to create and populate your dynamic array:
Dim arrEmployees
arrEmployees = Array(“Peter”, “Stephen”, “Tracy”, “Jerry”)
The above array will have 4 elements.
You can change array element values by simply specifying their new value:
arrEmployees(1) = “Bill”
You can get the VBScript array upper and lower bounds with the following VBScript functions:
Ubound(arrEmployees)
Lbound(arrEmployees)
You can resize your dynamic VBScript arrays with the ReDim statement.
ReDim arrEmployees(10)
Be very careful with the ReDim statement. When you use the ReDim statement you lose all elements of the array. To avoid that use the ReDim statement in conjunction with Preserve keyword, which will preserve the already existing array elements:
ReDim Preserve arrEmployees(30)
|