Sunday, February 6, 2011

VBScript for QTP Arrays

ArraysVBScript allows you to store data in variables that you can then reference and manipulate. While there is no limit to the number of variables that you can create in a single VBScript, you’ll find that trying to manage too many of them can become difficult.VBScript provides another storage structure for managing large groups of related data known as an array. An array is an indexed collection of values that are managed as a unit. For example, rather than creating a whole series of individual variables to store a list of values such as people’s names, you can create an array and assign each name as an array element.VBScript supports the creation of single dimensional and multidimensional arrays. In fact, VBScript allows for the creation of arrays with as many as 60 dimensions, although you’ll probably never need to work with anything bigger than one or two dimensions.VBScript allows you to create an array using the Dim statement. The syntax of the Dim statement when used to establish an array is
  Dim arrayname(dimensions)
Dimensions is a comma-separated list specifying the length of each of an array’s dimensions. For example, the following statement defines a single dimension array called myArray that can hold up to four elements.
Dim myArray(2)
An array is an indexed list. The first value stored in the array is automatically assigned an index value 0. The second element stored in an array has an index value of 1, and so on. Because an array’s index always begins with zero the actual length of an array dimension is equal to the number supplied in the declaration statement plus one. So the array defined above can hold up to 4 elements.Once you have defined and array you can populate it as demonstrated here.
myArray(0) = "Welcome"myArray(1) = "To"  

myArray(2) = "KR Testing Solution"
After you have populated an array you can reference its contents. For example, you could use the following statement to display the value stored as the third element in the array.
WScript.Echo myArray(2)
The following examples show how to create an array with more than one dimension. In this case, a two-dimensional array is created using the Dim statement that can store its data in a spreadsheet like format.
Dim myArray (4,4)
You can think of the array created by this example as having five rows by five columns. Remember, the length of any array dimension equals 1 plus the value that specified its length. Similarly, your could define a three-dimensional array by adding additional comma separated values in the Dim statement as shown here.
Dim myArray (4,4,4)

No comments:

Post a Comment