Using Array in VBScript



ArrayVBSUsing Array in VBScript

Imagine you have 100 names in a list and you want to store them all for some validations in your script.There are two ways that can be used to handle this situation.

  • Store all the names in different variables
  • Store all the names in an array

If we follow the first approach,then we will end up mapping each name to variables.This method will be tedious and time consuming task. The second approach of using array is best solution in such situation, where we create an array and store all the names in one variable itself  which will reduce the effort in mapping each name with each variable.

Array is collection of objects/values in a systematic order.

In VBScript ,we can define arrays in two ways:

Static Array: It refers to the constant size list in which we cannot add element over its defined capacity.
Dynamic Array: Refers to the variable size list in which elements can be added or removed at run time.

For Static Array ,the format of declaring the variable is as below :

ArrName=Array Name
ArrSize =Array Size


While defining the dynamic array ,we don’t need to declare the size of array.

ArrName=Name of an array

Assigning value to static array in VBScript

The values that needs to be assigned to the array needs to be on the right hand side of the line

ArrName=Name of an array
ArrIndex : Location where value needs to be stored.
This needs to be an integer value starting from 0 (zero).

Assigning value to dynamic array in VBScript

In dynamic array we use the keyword Redim for redefining the size of an array. As we have seen in Declaring an array, dynamic array doesn’t have any size in starting. So we can use Redim for defining the same.

ArrName=Name of an array
ArrSize =Size of the array

Now,  lets take an example, where we have 5 elements (10,20,30,40,50).We need to assign these variables to an array in VB Script.

First we need to declare a dynamic array in VbScript for the same.

Now we need to increase the size of this empty array which can be done using Redim keyword

As we have increased the size of the array as per our requirement,we will now assign the value to this array starting from the position zero(0)

So the overall code which has been designed till now looks like

Lets see the value in the msgbox of the above code till now

The output will be as follows

Now if we want to add a new element say 60, so again we will redefine the size of array to 6

Now if we check the output again of the array using the code

The output will be as follows

We see that when we have added an element to an array in Vbscript using Redim keyword,it makes Arr as blank for index 0 to 4.

If we want to preserve the values of an array as it is ,we can achieve the same using the Preserve keyword.

ArrName=Array Name
Arrsize=Size of an array

So ,now the code will be as below

The output will be as follows

In this way we can assign the values to the dynamic array.

PS: While using array in Vbscript, we recommended the usage of dynamic array instead of the static array as it helps to reduce the memory consumption during execution.

Happy Learning!!!!


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.