Arrays VBScript | Automation Testing QTP/UFT
Arrays VBScript:
Arrays are two types – Static Array & Dynamic Array
ARRAY:
Array is a collection of data with different types of data type.
Syntax:
1 |
Dim ArrayName(size) |
Example:
1 |
Dim arrayvalue(3) |
Assigning values to the array:
1 2 3 4 |
arrayvalue (0) = 1 arrayvalue (1) = 2 arrayvalue (2) = 3 arrayvalue (3) = 4 |
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:
1 2 3 |
Dim arrayvalue(3) Msgbox lbound(arrayvalue) Msgbox ubound(arrayvalue) |
Size of the Array:
1 2 |
Dim arrayvalue(3) Msgbox “The array size is ”& Ubound(arrayvalue) + 1 |
Another way to assigning Array:
Syntax:
1 |
array(arglist) |
Example:
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 |
Dim arrayvalue(1) arrayvalue(0)=10 arrayvalue(1)=20 msgbox arrayvalue(0) msgbox arrayvalue(1) |
MULTI DIMENSION ARRAY:
1 2 3 4 5 6 7 8 9 |
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
1 2 3 4 5 6 7 8 9 10 11 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 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