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.

Daniel Flannery
  • 1,166
  • 8
  • 23
  • 51

4 Answers4

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
Motti
  • 110,860
  • 49
  • 189
  • 262
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.

Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
gbonetti
  • 1,334
  • 17
  • 18
  • 2
    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. – Giacomo Lacava Apr 06 '16 at 13:26
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
  • 1
    Instead of the (wrong!) magic numbers, the constants vbTextCompare/vbBinaryCompare should be used (http://msdn.microsoft.com/en-us/library/05z4sfc7%28v=vs.84%29.aspx); setting the return value *in* the loop makes no sense. – Ekkehard.Horner Jan 21 '14 at 13:31
  • 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 you – Viraj John Maria Jan 21 '14 at 14:07
  • "working (fine)" is *not* a valid criterion for the quality of software. Don't be sorry, but correct your contribution. – Ekkehard.Horner Jan 21 '14 at 14:13
  • Please Check : http://www.qtpworld.com/index.php?cid=36 , http://www.gcreddy.com/2010/04/vb-script-conditional-statements.html , http://www.w3schools.com/vbscript/func_strcomp.asp – Viraj John Maria Jan 21 '14 at 14:19
  • 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 you – Viraj John Maria Jan 21 '14 at 14:32
  • Please if the above was wrong please let me know ... Because I'm using it in my entire test.. – Viraj John Maria Jan 21 '14 at 14:55
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
  • 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()). – Ekkehard.Horner Jan 21 '14 at 14:09