0

I currently Have an array ...

Dim mTeam(10) As String

In the Form load For visual basic i have

   'Load Teams
    mTeam(1) = "Oklahoma"
    mTeam(2) = "USC"
    mTeam(3) = "LSU"
    mTeam(4) = "Michigan"
    mTeam(5) = "Georgia"
    mTeam(6) = "Texas"
    mTeam(7) = "Tennessee"
    mTeam(8) = "Ohio State"
    mTeam(9) = "Florida State"
    mTeam(10) = "Miami(FL)"

Now what im trying to do, is have the user enter a value between 1-10 in a textbox, and in return. A messagebox will appear with the team name.

Example:

User enters 5 into TextBoxNumber, a message box will apear when i hit button ButtonName, and in that message box it will have the word "Georgia".


TextBoxNumber (Name of input value) ButtonName (button to do all the work and show messagebox)

Any help would be nice, i already have a try catch to have it only take integer values between 1 and 10.

PLEASE NOTE THIS IS FOR VISUAL BASIC , AND IM USING MICROSOFT VISUAL STUDIO 2010

4
  • textbox1.text = mTeam(textbox1.text) Commented Apr 20, 2012 at 2:23
  • @Satya The index for an array should be an integer, not a string. That code will not work if Option Strict is On Commented Apr 20, 2012 at 13:23
  • will keep this is mind for future Chris! Thanks Commented Apr 20, 2012 at 13:35
  • @UniversityStudent: Be aware that you are creating an array with 11 entries like this, the first (index 0) being Nothing. Commented Aug 2, 2019 at 19:46

1 Answer 1

3

You can attempt to convert the user's value to an integer, and if successful return the array value at that index:

Dim ind As Integer = -1
If Integer.TryParse(TextBoxNumber.Text, ind) Then
    If ind >= 0 AndAlso ind < mTeam.Length
        MessageBox.Show(mTeam(ind).ToString())
    End If
End If
Sign up to request clarification or add additional context in comments.

1 Comment

Holy crap, i cant believe it was that "simple". thanks for the help, was stuck on this part for a few hours!

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.