Arrays VBScript | Automation Testing QTP/UFT
Arrays VBScript
Arrays are two types – Static Array & Dynamic Array
What is an Array
Array is a collection of data with different types of data type.
Syntax:
Dim ArrayName(size)
Where, “ArrayName” is the unique name for the array and “size” is a numeric value that indicates the number of elements in the array dimension within the array.
Example:
Dim arrayvalue(3)
It stores 4 values. The array always starts from 0.
Assigning values to the array:
arrayvalue (0) = 1 arrayvalue (1) = 2 arrayvalue (2) = 3 arrayvalue (3) = 4
There are two types of arrays: 1. Static Array, 2. Dynamic Array.
Static Array:
It has a specific number of elements. Once assigned, size of a static array can’t be modified at runtime.
Dynamic Array:
Size of a dynamic array can be modified at runtime.
Lower Bound & Upper Bound of Array:
Dim arrayvalue(3) Msgbox lbound(arrayvalue) Msgbox ubound(arrayvalue)
Size of the Array:
Dim arrayvalue(3) Msgbox “The array size is ”& Ubound(arrayvalue) + 1
Another way to assigning Array:
Syntax:
array(arglist)
Example:
Dim arrayvalue Arrayvalue = array(91,92,93,94) Msgbox Ubound(arrayvalue) Msgbox Arrayvalue(0) Msgbox Arrayvalue(1) Msgbox Arrayvalue(2) Msgbox Arrayvalue(3)
REDIM:
It recreates the array and erases all the old data
PRESERVE:
Preserve should be used along with redim. It will retain the old data.
SINGLE DIMENSION ARRAY:
Dim arrayvalue(1) arrayvalue(0)=10 arrayvalue(1)=20 msgbox arrayvalue(0) msgbox arrayvalue(1)
MULTI DIMENSION ARRAY:
Dim arrayvalue(1,1) arrayvalue(0,0)=10 arrayvalue(0,1)=20 arrayvalue(1,0)=30 arrayvalue(1,1)=40 msgbox arrayvalue(0,0) msgbox arrayvalue(0,1) msgbox arrayvalue(1,0) msgbox arrayvalue(1,1)
Some Examples:
Without Preserve
dim stm() redim stm(1) stm(0)=1 stm(1)=2 msgbox stm(0) msgbox stm(1) redim stm(2) ‘REDIM WITHOUT PRESERVE stm(2)=3 msgbox stm(0) msgbox stm(1) msgbox stm(2)
With Preserve
dim stm() redim stm(2) stm(0)=11 stm(1)=22 stm(2)=33 msgbox stm(0) msgbox stm(1) msgbox stm(2) redim preserve stm(3) ‘REDIM WITH PRESERVE stm(3)=44 msgbox stm(0) msgbox stm(1) msgbox stm(2) msgbox stm(3)
Redim Preserve with Multi-Dimensional Array
dim Stm() redim Stm(1,1) stm(0,0)=10 stm(0,1)=20 stm(1,0)=30 stm(1,1)=40 msgbox stm(0,0) msgbox stm(0,1) msgbox stm(1,0) msgbox stm(1,1) redim preserve stm(1,2) stm(0,2)=50 stm(1,2)=60 msgbox stm(0,0) msgbox stm(0,1) msgbox stm(1,0) msgbox stm(1,1) msgbox stm(0,2) msgbox stm(1,2)
VBScript Series:
- Control Statements VBScript | Automation Testing QTP/UFT
- VBScript for Automation (QTP/UFT) Testing – Part 1
- VBScript for Automation (QTP/UFT) Testing – Part 2
- VBScript for Automation (QTP/UFT) Testing – Part 3
- VBScript for Automation (QTP/UFT) Testing – Part 4
- VBScript for Automation (QTP/UFT) Testing – Part 5