1

Hey so i have a textbox and i want to compare what the user types in the textbox to the words that are in the array which is from a text file. So i've got the users text as Dim answer as string and im trying to see if answer is in the list of words in the array that are loaded. This is what i have at the moment and i was wanting to just use arrayname.contains but i get the error "'contains' is not a member of form1.words". plz help. Words is the array name btw.

Dim answer As String
If Words.contains(answer) Then
        MessageBox.Show("you got it")
        End If
6
  • 1
    Can you show how you have declared and initialized the Words variable? Commented May 16, 2018 at 15:00
  • 3
    So you discovered that .Net arrays do not expose a Contains method, but did you consider looking to see it they expose other methods that may help you achieve the goal? Commented May 16, 2018 at 15:06
  • Possibly also show how you assigned the user input to the answer string. Commented May 16, 2018 at 15:18
  • Hello and Welcome to Stack Overflow! Before asking here it is crucial that you do your own research and/or attempts into solving the problem yourself (refer to: How much research effort is expected of Stack Overflow users?). Posts that don't show this are usually not well received. Infact, googling your very title of this question gives you plenty of good results! :) Commented May 16, 2018 at 15:39
  • In addition to the above please also take a moment to take the Tour and read How to ask to get a better grasp of how this site works. Thank you! Commented May 16, 2018 at 15:42

1 Answer 1

1

I'll list two different approaches

ContainsWord1() uses:
Enumerable.Any(Of TSource) - .NET 3.5+
Ref: https://msdn.microsoft.com/en-us/library/yw84x8be(v=vs.110).aspx

ContainsWord2() uses:
Array.Exists(Of T) Method (T(), Predicate(Of T)) - .NET 2.0+
Ref: https://msdn.microsoft.com/en-us/library/bb337697(v=vs.110).aspx

Both are case insensitive.

   Private Function ContainsWord1(p_wordsArray as string(), p_word As string) as Boolean
        return p_wordsArray.Any(Function(s as String) s.Equals(p_word.trim, StringComparison.CurrentCultureIgnoreCase))
    End Function

    Private Function ContainsWord2(p_wordsArray as string(), p_word As string) as Boolean
        return Array.Exists(p_wordsArray, Function(s As String) s.Equals(p_word.trim,  StringComparison.CurrentCultureIgnoreCase))
    End Function

    private Sub ContainsWordExample()
        Dim _words = New String(){"blue","red","yellow", "black"}
        Dim _yellow as string = "YeLloW"
        Dim _green as string = "green"

        'Yellow test
        If ContainsWord1(_words, _yellow) then 
            messagebox.Show("You got it!")
        Else
            messagebox.Show("Try again!")
        End If

        If ContainsWord2(_words, _yellow) then 
            messagebox.Show("You got it!")
        Else
            messagebox.Show("Try again!")
        End If

        'Green test
        If ContainsWord1(_words, _green) then 
            messagebox.Show("You got it!")
        Else
            messagebox.Show("Try again!")
        End If

        If ContainsWord2(_words, _green) then 
            messagebox.Show("You got it!")
        Else
            messagebox.Show("Try again!")
        End If

    End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.