Visual Basic Programming Error

I am attaching the user interface and the code for a Visual Basic Program I am trying to write as I try to learn this from a book. What is supposed to happen is the user is to enter a letter and click either food or animal. The output then depends on the letter entered and the item it corresponds with. The problem is, no matter what letter is entered or which button is selected, I always get the "Invalid Entry" message that is also part of the program. Please advise.

Thank you!!

Robert
Interface.JPG
Name.doc
Robert EhingerIT specialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SStoryCommented:
For starters,
A better way to check for valid chars is
Const ValidChars As String = "ABCGK"

If ValidChars.IndexOf(e.KeyChar.ToUpper) = -1 Then
   e.Handled = True
End If

Open in new window

SStoryCommented:
Instead of doing:
Dim strFound As String

It seems it would be better to use a boolean variable:
Dim bFound as boolean=false
SStoryCommented:
oops.I hit submit too soon.

I'm not sure what you are trying to accomplish. See code below.
However, I assume you want to use the letter pressed to get the index of that in array 1 and use that index to return the value from array2... And that you want to get the value from txtLetter textbox.
This could be done as follows:

        Dim strVar() As String = {"A", "B", "C", "G", "K"}
        Dim strFood() As String = {"Apple", "Banana", "Carrot", "Grape", "Kiwi"}

       dim strResult as string=strFood(Array.IndexOf(strVar, txtLetter.Text.ToUpper))
     
       msgbox(strResult)



        Dim strLetter() As String = {"A", "B", "C", "G", "K"}
        Dim strFood() As String = {"Apple", "Banana", "Carrot", "Grape", "Kiwi"}
        Dim strSearchFor As String
        Dim strFound As String
        Dim intSubscript As Integer

        ' assign the food to a variable
        strSearchFor = strFood.Length
        strFound = "N"
        Do Until strFound = "Y" OrElse intSubscript = strFood.Length
            If strFood(intSubscript) = strSearchFor Then
                strFound = "Y"
            Else
                intSubscript = intSubscript + 1

            End If
        Loop

You set strSearchFor to the length of strFood array, means
strSearchFor is always 5.  I assume this isn't what you want.

I assume you want them to press A and give you the value "Apple"
so you need to get the index of A then use that to return the value in StrFood.

 

Open in new window

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Robert EhingerIT specialistAuthor Commented:
OK, I made some changes, obviously not exactly what you suggested, so I am again attaching the screen shot and code. Please advise.

Thank you!!

Robert
Interface2.JPG
Name2.doc
SStoryCommented:
OK. We the problem is that if it isn't found in the list you will get -1 (an invalid index)

so let's check for that


dim strResult as string=""
dim iIndex as integer= Array.IndexOf(strVar, txtLetter.Text.ToUpper)

if iIndex=-1 then
     msgbox("Invalid Text")
else
   'valid char so use the index to get the value
  strResult=strFood(iIndex)
end if


Robert EhingerIT specialistAuthor Commented:
OK, when I put in the code you send I do get the "Invalid Index" Message.
SStoryCommented:
Do you mean invalid index or invalid text

If invalid index, you should see what value iIndex is getting set to by putting a breakpoint on the code and checking it out or

msgbox(iIndex) before the

if iIndex=-1 then

line

if it is <0 or greater than the number of elements in the array -1, you will get an error.
Robert EhingerIT specialistAuthor Commented:
I am getting -1 for the index and the the message box that say "Invalid Index."
SStoryCommented:
-1 means it is NOT finding the value you are searching for in the array.

I assume instead of strVar like I used you are doing:

strLetter


So:

dim strResult as string=""
'this says, search array strLetter for the UPPERCASE value of whatever is in txtLetter
dim iIndex as integer= Array.IndexOf(strLetter, txtLetter.Text.ToUpper)

if iIndex=-1 then
     msgbox("Invalid Text")
else
   'valid char so use the index to get the value
  strResult=strFood(iIndex)
end if



I see your problem. You are searching the strFood array.
I assume you want to search the letters array to find out the index of the one they pressed,
and get the value int he strFood array at that same index.  You have:

        Dim strResult As String = strFood(Array.IndexOf(strFood, txtLetter.Text.ToUpper))

The second strFood, is wrong. It should be strLetter

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SStoryCommented:
as for the rest of this code:

        ' assign the food to a variable
        strSearchFor = strFood.Length
        Found = "False"
        Do Until Found = "True" OrElse intSubscript = strFood.Length
            If strFood(intSubscript) = strSearchFor Then
                Found = "True"
            Else
                intSubscript = intSubscript + 1

            End If
        Loop

        ' determine whether the ID was found
        If Found = "True" Then


I don't know why you'd need it.

If you are trying to do something else other than what I described, please tell me what you are trying to accomplish? How should it work?
Robert EhingerIT specialistAuthor Commented:
The user is supposed to enter a letter and then, depending on whether the user selects the food or animal button the output is supposed to correspond with the appropriate selection for the list, either from {"Apple", "Banana", "Carrot", "Grape", "Kiwi"} or from {"Antelope", "Bear", "Camel", "Goat", "Kangaroo"}
Robert EhingerIT specialistAuthor Commented:
If I remove the code that you listed above that you said "I don't know why you'd need it." then there is no output at all. No Invalid Entry or proper output.
SStoryCommented:
If you use the code I made above for Food, modify it as needed for the other four types Animals or whatever, you will have the valid item in the variable:
strResult

You can return that from a function or do whatever you want with it.  
If you want a message. Add a line that says
msgbox(strResult)

I gave you the code to test for valid or invalid chars in my first comment.

I was just trying to show you how to get the appropriate value. I assumed you would do whatever you wanted with that value once you properly obtained it.

There are MANY ways to write the code to choose from the various arrays.

you could have a function that gets passed a value depending upon which button was pressed and then you modify the code I gave you to return from the appropriate array.

You could copy that code to three different events (for each button) and modify to come from the correct array.

And there are other ways, but I think they'd be too difficult for you at this point.

I hope this helps.
Robert EhingerIT specialistAuthor Commented:
You were a tremendous help and I think I have it now. You will see when I assign the points just which entry pushed me to the solution.
SStoryCommented:
I'm glad to be of help.
Robert EhingerIT specialistAuthor Commented:
Don't go away, there could be more as I progress through this book.
SStoryCommented:
What book?
Are you learning on your own, or in a class?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.