Working with Arrays-QTP - QTP

VBScript provides two types of arrays - fixed length and dynamic.Fixed length arrays

Sample code:

'create a 3 member fixed length array Dim X(2)
X(0) = "This" X(l) = "is a" X(2) = "test""
For i = LBound(X) to UBound(X)
MsgboxX(i) Next
The LBound and UBound functions return the values of lower and upper bound of an array respectively, which in above example would be 0 and 2.

Dynamic Arrays

'create a dynamic array Dim X()
'Before using the array the first time the array must be initialized with some
dimension
ReDimX(2)
X(0) = "This""
X(l) = "is a"
X(2) = "test"

We can ReDim a dynamic array to change its size as often as we want. But each time this is done, the values in the array are destroyed unless we use the 'preserve' keyword as shown here:

'Redimension the array ReDim Preserve X(5)

The above statement resizes the array while preserving the values of the first 3 elements. In the case of a multi-dimensional array, the Preserve keyword can be used if only the last dimension of the array is changing.

'Dynamic array Dim X()
ReDimX(2,3)

'This will work fine as we are only changing the last dimension while preserving the array ReDim Preserve X (2,5)
"This will throw an error due to' the first 'dimension of the array being changed ReDim Preserve X(3,5)
'This will work fine "as normal ReDim does 'not have a restriction ReDimX(4,5,6)

Problem: How can we get the UBound/LBound values of multi-dimensional array?

Consider the multi-dimensional array X(4,5,6). UBound(X) will return 4, the upper bound of the 1st dimension. To get the other dimension we can use the optional parameter of UBound 'returns 4, if the second argument is not 'specified it is defaulted to 1 MsgBoxUBound(X, 1)

'returns 5, the upper bounds of the 2nd dimension MsgboxUBound(X, 2)
'returns 6, the upper bounds of the 3rd dimension MsgboxUBound(X, 3)

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

QTP Topics