0 Shares 5733 Views

Using Array in VBScript

Apr 30, 2013
5733 Views

 

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 :

where
ArrName=Array Name
ArrSize =Array Size

 

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

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

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.

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.

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.

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

You may be interested

Setting up Web Service Automation Project
Katalon Studio
240 views
Katalon Studio
240 views

Setting up Web Service Automation Project

Saket - Oct 24, 2017

Web services are specific services that can be published and called over the Internet by client applications. Katalon Studio supports functional testing of web services. This topic…

Setting up Mobile Automation Project in MacOS
Katalon Studio
241 views1
Katalon Studio
241 views1

Setting up Mobile Automation Project in MacOS

Saket - Oct 09, 2017

Using Katalon Studio, mobile tester can design automation test for both Android and iOS to run on physical devices, cloud services and emulators. This tutorial explains how…

UFT Jenkins Integration
Unified Functional Testing
795 views
Unified Functional Testing
795 views

UFT Jenkins Integration

Saket - Oct 03, 2017

In our earlier post, we got to know the easy integration of UFT with GIT. This post focuses on integration of UFT and Jenkins. I have also…

Leave a Comment

Your email address will not be published.

Most from this category

Join The Growing Group of 15000+ Test Automation Engineers

Signup for our newsletter and get access to free downloads, as well as tips, tricks and techniques and various test automation resources.
Name
Email Address
Secure and Spam free...