How to Work With Arrays in VBScript or UFT?

Spread the love
  • 28
  • 34
  • 28
  • 35
  • 12

Arrays in VbScript or UFT
Arrays in VbScript or UFT image credit-wiki

Arrays in Vbscript or arrays in UFT

A variable containing a series of values, is called an array variable.Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name.


Dim ArrayName(Dimension)
Dim A(3) 

Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements.

In order of save this information we need to create 4 scalar variables. And if the data structure size goes high we need to create a series of scalar variables.

This way of creating multiple scalar variables may lead to unwanted failure of the script. Hence the better approach is to create a Array (as a collection).

What is an array and how to assign values to Array in VBScript?

An array is a list of related data governed by index or subscript. An array can be viewed as a column and row combination.Arrays have dimensions.Vbscript supports upto 60 dimensions.
1-dimensional array
Sl No Stud_No Stud_Name Stud_Email
1 Stud1 XYZ [email protected]

How to Assign values

We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:

' For 1 dimensional array
A(0) = 256
A(1) = 324
A(2) = 100
A(3) = 55
' For 2 dimensional array
A(1,2)="[email protected]"

Similarly, the data can be retrieved from any element using an index into the particular array element you want.

For example:

SomeVariable = A(4) 

Static Array

Static Array is an array whose size is defined at the beginning of the script.

Multidimensional Array

An array can store 60 dimensions.

2-dimensional array

The syntax is the same:

Dim arrayName(Dimensions)

The below one is studentInfo(2,3). It is a representation of ArrayName(X, Y).X and Y corresponds to an intersection of the points of each dimension. In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns

As the array is zero based hence the total number of information that is saved here is (2+1)*(3+1)=12

Sl No Stud_No Stud_Name Stud_Email
1 Stud1 XYZ [email protected]
2 Stud2 ABC [email protected]

Each cell corresponds to an element of the array can hold a single value.

Arrays aren’t limited to a single dimension. We can have as many as 60 dimensions, although most people can’t comprehend more than three or four dimensions.
In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

Dim MyTable(5, 10)

How to Use Ubound() function for multidimensional array?

The use of Ubound() function for the multidimensional array is little tricky:


nameOfArray is the array whose Ubound needs to be calculated on the given dimension.

Dynamic Arrays:

Resizing the single-dimensional array

Till this time all the discussion happened on array is of the fixed size where the VBScript engines know the size of the array. We can also declare an array whose size changes during the time our script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the ReDim statement. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.

For example:

Dim MyArray()
ReDim AnotherArray() 

To use a dynamic array, we must subsequently use ReDim to determine the number of dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30 but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

ReDim MyArray(25)
ReDim Preserve MyArray(30) 

There is no limit to the number of times we can resize a dynamic array, although if we make an array smaller, we lose the data in the eliminated elements.

We can declare a variable without the array notation like MyArray and later can be redim to a multidimensional array.

Resizing the multidimensional array

Multidimensional array works almost in the same way as single-dimensional array except for few exceptions:

  • If myArray(4,4) is a multidimensional array, we can only make a change to the last dimension, so myArray(4,10) is legal. Using preserve a redim keyword we can save the data.
  • If myArray(4,4) is a multidimensional array, we can change all the dimensions. So myArray(5,10) is also legal. But in this case, we can not use preserve keyword and the existing data will be lost

Where to use a dynamic array?

When we do not know the size or the volume of the incoming data to our script, we need to use a dynamic array.


  1. Customer survey
  2. Customer details  etc

In these scenarios, we beforehand don’t know how many customer we are going survey and how many of the customer will give me the details. So in those scenarios, if we use a fixed-size array, we may eat up space if we get more than what we anticipated or we may lose memory space in case we do not get the anticipated numbers.

In such uncertain conditions, we need to use a dynamic array.

VarSafety of Variant in Array:

We can use dynamic array using a double bracket “()”.Hence the case-I is a normal variable. We can change the normal variable to array by using redim keyword. The transformation is done by the VBScript engine. But this is not a good coding style to use a multipurpose variable and change the type. We can test the types via debugging.

Dim a
redim a(2,0)
Dim b
redim b(2,0)

Looping through Array:

we can loop through an Array using Index. The important factors are:

  • Index
  • The upper bound of an Array function name-UBound

Ubound function takes two arguments:

  1. Name of the array variable
  2. The number of the dimension

Ubound and Lbound function of Array

When UBound() Function provides the upper limit or the maximum size of the Array, the Lbound() function provides the lowest non-negative size of the array.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 28
  • 35
  • 12


Please enter your comment!
Please enter your name here