Arrays in VBScript

An Array variable can have different data type values like int, char, string, Boolean etc
An Array variable values can be assigned or retrieved by using it’s index, index can not be -ve.

An Array variable can be single/multi dimension: if multi dimension variable, can have up to 60 dimension.

Static Array variable

‘Declaration of a single dimension array variable
  Dim MyArray()
‘Assigning values to an array
MyArray(0) = 1
MyArray(1) = 2.5
MyArray(2) = ‘s’
MyArray(3) = “VBScript”

MyArray = Array(1,2.5,”s”,”VBScript”,#12/03/2013#)

Dynamic Array variable – 

  Dim MyArray()

‘Declaring a dynamic array with size using REDIM keyword
  ReDim MyArray(5) / MyArray(3,6)

‘Preserving the previous values of variable MyArray and expanding the size further
  ReDim Preserve MyArray(10) / MyArray(10,20)

Note: If we do not use Preserve keyword and expand the size, then previous stored data will be empty.

Accessing each array values using loops –
1) For…Next
For i=0 to UBound(MyArray)
MsgBox MyArray(i)
2) For Each…Next
For Each Arr in MyArray
MsgBox Arr

NOTE: how to get the UBound of a multi dimensional array
Dim Arr2(3,2)
Ubound(Arr, 1) = 3 ‘ The Largest Subscript of the first dimension in Arr
UBound(Arr, 2) = 2 ‘ The Largest Subscript of the second dimension in Arr

@Please feel free to comment which will encourage me to write more information in future@

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *