QTP: Checking If an array of strings contains a value
Asked Answered
O

4

7

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.

Oxysalt answered 9/8, 2012 at 8:38 Comment(2)
What are the contents of options? are these strings, some kind of test object (if so what kind)?Sadyesaechao
I am populating options like so: options(0) = "welcome" which is strings if I am correct. Its a fixed sized array.Oxysalt
S
16

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
Sadyesaechao answered 9/8, 2012 at 11:35 Comment(2)
Ah yes. That explains the problems I was having. Thank you for your informative answer.Oxysalt
You implemented the Filter logic here, which searches for the string in the string instead of comparing it exactly.Perilymph
S
16

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.

Simson answered 3/9, 2014 at 4:9 Comment(1)
If I understand the docs correctly, the problem with this solution is that Filter will return all elements where choice is a substring. E.g. with an array containing "Dune", "Dunebug", "Blah", and choice = "Dune", it will return two items.Caty
P
0

Hi if you check exact String not sub-String in the array use StrComb because if use InStr then if array = "apple1" , "apple2" , "apple3" , "apple" and choice = "apple" then all will return pass for every array item.

Function CompareStrings ( arrayItems , choice )

For i=Lbound(arrayItems) to Ubound(arrayItems)

    ' 1 - for binary comparison "Case sensitive
    ' 0 - not case sensitive
    If StrComp(arrayItems(i), choice , 1) = 0 Then

    CompareStrings = True
    MsgBox("Found " & choice & " at index " & i

    Else

    CompareStrings = False
    MsgBox "String not found!"

    End If

Next

End Function
Peahen answered 21/1, 2014 at 13:5 Comment(6)
Instead of the (wrong!) magic numbers, the constants vbTextCompare/vbBinaryCompare should be used (msdn.microsoft.com/en-us/library/05z4sfc7%28v=vs.84%29.aspx); setting the return value in the loop makes no sense.Tutorial
Sorry.. You are right about the loop... but i have tried passing 0 and 1 it seems working fine.. i think instead of vbTextCompare/vbBinaryCompare 0 and 1 working fine.. Thank youPeahen
"working (fine)" is not a valid criterion for the quality of software. Don't be sorry, but correct your contribution.Tutorial
Please Check : qtpworld.com/index.php?cid=36 , gcreddy.com/2010/04/vb-script-conditional-statements.html , w3schools.com/vbscript/func_strcomp.aspPeahen
Also Thank you for your point "working (fine)" is not ok for the quality of software.. from the references i reached to a point that using 0 and 1 will work.. Thank youPeahen
Please if the above was wrong please let me know ... Because I'm using it in my entire test..Peahen
P
0
Function CompareStrings ( arrayItems , choice )
For i=Lbound(arrayItems) to Ubound(arrayItems)

' 0 - for binary comparison "Case sensitive
' 1 - for text compare not case sensitive
If StrComp(arrayItems(i), choice , 0) = 0 Then

MsgBox("Found " & choice & " at index " & i

Else

MsgBox "String not found!"

End If

Next

End Function
Peahen answered 21/1, 2014 at 13:58 Comment(1)
now your function isn't a function and you still don't use the constants. Please delete this 'answer' and correct your previous one (containing the valuable pointer to StrComp()).Tutorial

© 2022 - 2024 — McMap. All rights reserved.