Avatar of ouch_mybrain_
ouch_mybrain_
 asked on

VB.NET beginner question regarding an array - sample code/error provided >_<

Hello Experts,

I am a beginner who is trying to learn vb.net. I need some help in understanding why I am getting an error when trying to check if a variable exists against an array (see below):

Error
Here is my code ... also please note that on my form I have a button called "Button1" and a text box called "txtBoxGuess". The idea is that I enter a character into the textbox and then press the button. If the character exists in the string then I get a message saying "hooray!", and if it is not in the array, "boo".

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click

        Dim sport(6) As String
        sport(0) = "c"
        sport(1) = "r"
        sport(2) = "i"
        sport(3) = "c"
        sport(4) = "k"
        sport(5) = "e"
        sport(6) = "t"

        Dim Guess As String
        Guess = txtBoxGuess.Text

        If sport.Contains = Guess Then
            MsgBox("Hooray!")
        Else
            MsgBox("Boo1")
        End If
    End Sub
End Class

Open in new window


Thanks.
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Jacques Bourgeois (James Burger)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
andygrant2005

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ouch_mybrain_

ASKER
Excellent, thank you very much. Glad to know I was close.
Jacques Bourgeois (James Burger)

I do not see however why you use an array of strings to store characters.

Either use a single String:

    Dim Sport As String = "cricket"

Or use an array of characters:

    Dim sport(6) As Char
        sport(0) = "c"c
        sport(1) = "r"c
        sport(2) = "i"c
        sport(3) = "c"c
        sport(4) = "k"c
        sport(5) = "e"c
        sport(6) = "t"c

The array could also be initialized the following way:

    Dim sport() As Char = "cricket".ToCharArray
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck