0

I'm trying to use this script to compare a users input from a text box with the 22 correct words. I'm not looking for multiple cases, such as VICE is in ADVICE so it would be 2 values; I want it to have the string values to accept only equal values.

At the moment, it is only recognizing the first word TIED and displays a message box "found", but it doesn't not recognize any other word in the list.

I am writing in visual basic script

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
    Dim Find As String = userinput
    For Each Str As String In StrCorrect
        If StrComp(Str, userinput, CompareMethod.Text).ToString = 0 Then
            MsgBox("Found" & userinput)
            Return
        Else : MsgBox("incorrect word")
            Return
        End If
    Next
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
Paul B
  • 199
  • 1
  • 4
  • 13

4 Answers4

1

The problem is that your loop is explicitly returning if the first item isn't a match. You only know you don't have a match if your loop completes without finding one, so try something like this instead:

For Each Str As String In StrCorrect
    If StrComp(Str, userinput, CompareMethod.Text).ToString = 0 Then
        MsgBox("Found" & userinput)
        Return
    End If
Next

MsgBox("incorrect word")

This will only display "incorrect word" if all of the items in your list fail the first test.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
0

Why STRCOMP? Why not a direct comparision if you want an exact match?

    For Each Str As String In StrCorrect
        If Str = Find Then
            MessageBox.Show("Found :" & Str)
        End If
    Next
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Very useful but doesn't solve the problem stated in the question: "it is only recognizing the first word "TIED". Also worth noting that using `=` omits any culture and case sensitivity options that might be required - it's generally better practise to use a method of the `string` class for these operations. – Dan Puzey Apr 25 '13 at 15:45
  • It is only supposed to recognize the exact match and not `ADVICE` See this from the question `I want it to have the string values to accept only equal values.` – Siddharth Rout Apr 25 '13 at 15:45
0

I would use a for loop, something like

For i As Integer = 0 To StrCorrect.Length - 1
        If StrCorrect(i) = Find Then
            MsgBox("Found" & Find)
            Return
        'End if

        'The else statement simply alerting that it didnt find the right word on this iteration
        'The else can be removed if you dont want this alert
        Else
            MsgBox("incorrect word")
            'Return
        End If
Next
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

Try like below, It will help you...

Sample :

Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If

Full Code :

Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
Dim Find As String = userinput
Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If
Pandian
  • 8,848
  • 2
  • 23
  • 33