QTP: Checking If an array of strings contains a value
Solution 1
Strings in VBScript are not objects, in that they do not have member functions. Searching for a substring should be done by using the InStr
function.
For j=Lbound(options) to Ubound(options)
If InStr(options(j), choice) <> 0 Then
MsgBox("Found " & choice & " at index " & j
Else
MsgBox "String not found!"
End If
Next
Solution 2
A concise way to check if an array of strings contains a value would be to combine the Filter and UBound functions:
If Ubound(Filter(options, choice)) > -1 Then
MsgBox "Found"
Else
MsgBox "Not found!"
End If
Cons: you don't get the indexes where the elements are found
Pros: it's simple and you have the usual include and compare parameters to specify the matching criteria.
Comments
-
Daniel Flannery almost 2 years
I am having trouble getting my test case to run correctly.
The problem is in the code below, the first if statement to be exact. QTP complains that an object is required
For j=Lbound(options) to Ubound(options) If options(j).Contains(choice) Then MsgBox("Found " & FindThisString & " at index " & _ options.IndexOf(choice)) Else MsgBox "String not found!" End If Next
When I check the array I can see that it is populated correctly and 'j' is also the correct string. Any help with this issue would be greatly appreciated.