Using 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 a tedious and time-consuming task. The second approach of using an array is the best solution in such a 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 elements 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 :

Dim ArrName(ArrSize)

where
ArrName=Array Name
ArrSize =Array Size

 

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

Dim ArrName()

where
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(ArrIndex)=Value

where
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.

Redim ArrName(ArrSize)

where
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.

Dim Arr()

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

Redim Arr(5)

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)

Arr(0)="10"
.
.
.
Arr(4)="50"

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

Dim Arr
Redim Arr(5)
Arr(0)="10"
Arr(1)="20"
Arr(2)="30"
Arr(3)="40"
Arr(4)="50"

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

For i= 0 to 4
   FirstArr=FirstArr+"Arr("+cstr(i)+") : "+(Arr(i))&vbnewline
next
msgbox FirstArr

The output will be as follows

Arr(0) : 10
Arr(1) : 20
Arr(2) : 30
Arr(3) : 40
Arr(4) : 50

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

ReDim Arr(5)
Arr(5)=60

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

For i= 0 to 5
  SecondArr=SecondArr+"Arr("+cstr(i)+") : "+(Arr(i))&vbnewline
next
msgbox SecondArr

The output will be as follows

Arr(0) :
Arr(1) :
Arr(2) :
Arr(3) :
Arr(4) :
Arr(5): 60

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.

See also  Error Handling in VBScript

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

Redim Preserve ArrName(ArrSize)

where
ArrName=Array Name
Arrsize=Size of an array

So ,now the code will be as below

Dim Arr
Redim Arr(5)
Arr(0)="10"
Arr(1)="20"
Arr(2)="30"
Arr(3)="40"
Arr(4)="50"
Redim Preserve Arr(5)
For i= 0 to 5
SecondArr=SecondArr+"Arr("+cstr(i)+") : "+(Arr(i))&vbnewline
next
msgbox SecondArr

The output will be as follows

Arr(0) :10
Arr(1) :20
Arr(2) :30
Arr(3) :40
Arr(4) :50
Arr(5): 60

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!!!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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