Why my array.IndexOf continues to return -1 (Visual Basic)

advertisements

I have a homework program for Visual basic. It stores 7 phone numbers for 7 friends in parallel arrays. I am using array.indexof to see if my input string is within the name array, then return the complete name and the corresponding number. I am not looking for the answer from posters, just some guidance or point out an over site on my part. Thanks in advance. sorry if my post is garbled or formatted incorrect, first time posting Robert

  Dim intCounter As Integer
    Dim strNames() As String = {"BILLY", "JILLY", "MILLY", "PHILLY", "LARRY", "CURLY", "MOE"}
    Dim strNumbers() As String = {"313-213-1234", "248-123-3452", "123-321-1234", "987-986-3456", "567-635-7632", "524-456-6782", "918-872-3452"}
    Dim strLookFor As String
    Dim found As Boolean

    strLookFor = InputBox("Type in the person you want to call", "Phone a Friend") 'prompt from user
    strLookFor.TrimEnd()  'trim whitespace after entry
    strLookFor = strLookFor.ToUpper  'convert to upper case to match

    txtResults.Clear() 'clear text box
    found = False  'set boolean to false
    intCounter = 0  'set intCounter to 0
    Do While Not found And intCounter < strNames.Length

        Dim intIndex As Integer = Array.IndexOf(strNames, strLookFor) 'SHOULD return a value of 0 or higher if found
        If intIndex >= 0 Then  'if loop for value of 0 or higher
            found = True
        End If

        intCounter += 1 'add 1 to intCounter

    Loop

    If found Then  'display phone match up results
        txtResults.Text = ((strNames(intCounter - 1)) & " " & (strNumbers(intCounter - 1)))
    Else
        txtResults.Text = ("Match not found.")
    End If
End Sub


I would suggest using a dictionary(of string, string), its ideal for this type of thing:

Dim dic As New Dictionary(Of String, String)
    dic.Add("BILLY", "123-456-888")
    dic.Add("JILLY", "333-555-222")
    dic.Add("MILLY", "777-334-667")
    dic.Add("PHILLY", "122-665-333")

    Dim strLookFor As String = InputBox("Type in the person you want to call", "Phone a Friend").ToUpper

    If dic.ContainsKey(strLookFor) Then
        MessageBox.Show(dic(strLookFor))
    Else
        MessageBox.Show("Match not found.")
    End If