Link to home
Start Free TrialLog in
Avatar of Robert Ehinger
Robert EhingerFlag for United States of America

asked on

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
Avatar of SStory
SStory
Flag of United States of America image

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

Instead of doing:
Dim strFound As String

It seems it would be better to use a boolean variable:
Dim bFound as boolean=false
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

Avatar of Robert Ehinger

ASKER

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
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


OK, when I put in the code you send I do get the "Invalid Index" Message.
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.
I am getting -1 for the index and the the message box that say "Invalid Index."
ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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"}
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.
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.
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.
I'm glad to be of help.
Don't go away, there could be more as I progress through this book.
What book?
Are you learning on your own, or in a class?