0 Shares 3628 Views

Using Array in VBScript

Apr 30, 2013
3628 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 Mobile Automation Project in Windows
Katalon Studio
77 views
Katalon Studio
77 views

Setting up Mobile Automation Project in Windows

Saket - Sep 20, 2017

This tutorial explains how to setup a mobile automation project and test a mobile app using Katalon Studio on Windows. This tutorial assumes that you are familiar…

Katalon Studio GUI Overview
Katalon Studio
147 views2
Katalon Studio
147 views2

Katalon Studio GUI Overview

Saket - Sep 14, 2017

GUI Overview After installing and setting up Katalon Studio for your automated test, you must understand the GUI. In this post we will walk you through the…

LeanFT Tutorial #4: Application Model Overview
LeanFT
124 views1
LeanFT
124 views1

LeanFT Tutorial #4: Application Model Overview

Saket - Sep 10, 2017

In this post we are going to focus on Application Model in LeanFT. Refer our earlier posts on LeanFT Getting Ready with LeanFT Browser Extensions and Settings…

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