Monday, 11 January 2010
Working with Arrays in QTP
Dealing with Arrays in QTP
What are Arrays?
An array is a contiguous area in the memory referred to by a common name. It is a series of variables having the same data type. Arrays are used to store related data values. VBScript allows you to store a group of common values together in the same location. These values can be accessed with their reference numbers.
An array is made up of two parts, the array name and the array subscript. The subscript indicates the highest index value for the elements within the array. Each element of an array has a unique identifying index number by which it can be referenced. VBScript creates zero based arrays where the first element of the array has an index value of zero.
Declaring Arrays
An array must be declared before it can be used. Depending upon the accessibility, arrays are of two types:
· Local Arrays: A local array is available only within the function or procedure, where it is declared.
· Global Arrays: A global array is an array that can be used by all functions and procedures. It is declared at the beginning of the VBScript Code.
The Dim statement is used to declare arrays. The syntax for declaring an array is as follows:
Dim Array Name(subscript value)
Where, Array Name is the unique name for the array and Subscript Value is a numeric value that indicates the number of elements in the array dimension within the array.
Static and Dynamic Arrays:
VBScript provides flexibility for declaring arrays as static or dynamic.
A static array has a specific number of elements. The size of a static array cannot be altered at run time. A dynamic array can be resized at any time. Dynamic arrays are useful when size of the array cannot be determined. The array size can be changed at run time.
Ways to work with Arrays
The easiest way create an array is to simply declare it as follows
Dim strCustomers()
Another method is to define a variable and then set it as an array afterwards
Dim strStaff
strStaff = Array("Alan","Brian","Chris")
Yet another way is to use the split command to create and populate the array
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
To itterate through the contents of an array you can use the For Each loop
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
Dim i
For i=LBound(strProductArray) To UBound(strProductArray)
Msgbox strProductArray(i)
Next
This will itterate through the array backwards
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
For i = UBound(strProductArray) To LBound(strProductArray) Step -1
Msgbox strProductArray(i)
Next
To add extra data to an array use Redim Preserve
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
For i = UBound(strProductArray) To LBound(strProductArray) Step -1
Msgbox strProductArray(i)
next
Redim Preserve strProductArray(3)
strProductArray(3) = "Mice"
For i = UBound(strProductArray) To LBound(strProductArray) Step -1
Msgbox strProductArray(i)
next
To itterate through the contents of an array you can use the For Each loop
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
Dim strItem
For Each strItem In strProductArray
MsgBox strItem
Next
To store the contents of an array into one string, use Join
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
Msgbox Join(strProductArray, ",")
To delete the contents of an array, use the Erase command
Dim strProductArray
strProductArray = "Keyboards,Laptops,Monitors"
strProductArray = Split(strProductArray, ",")
Erase strProductArray
Msgbox Join(strProductArray, ",")
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment