Hi,
I am learning about the search capability in an array. I wrote a code that stores three names (first & last) and I want to:

- Make the user select a number (from 1-3) by using a (combobox)
- If the user select the proper number then a message will appear that the name is there.
- If not then an error message will appear.

I did all I could by creating this but my main problem is in the (ButtonFind). I went through lots of tutorials and this is what I came up with and I hope that my code is correct.
I appreciate the help and thank you in advance.

Public Class Form1
    Structure NamesInfo

        Dim FirstName As String
        Dim LastName As String
        Dim NamesNumber As Integer

    End Structure

    Public NamesArray(4) As NamesInfo
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        NamesArray(1).FirstName = "James"
        NamesArray(1).LastName = "Bond"
        NamesArray(1).NamesNumber = 1
        NamesArray(2).FirstName = "Jack"
        NamesArray(2).LastName = "Black"
        NamesArray(2).NamesNumber = 2
        NamesArray(3).FirstName = "Michael"
        NamesArray(3).LastName = "Jordan"
        NamesArray(3).NamesNumber = 3
        NamesArray(4).FirstName = "Mickey"
        NamesArray(4).LastName = "Mouse"
        NamesArray(4).NamesNumber = 4
    End Sub

    Private Sub ButtonFind_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFind.Click
	'Get the number from the combobox
	
	'Create 2 variables of Name (Name & FoundName)
	
	'Use for loop
	' For Each Name In NameArray
        ' If (the Name Number is the one we are looking for) Then
        '       Messagebox and write "We found the name"
        '    End If
        'Next
        ' Messagebox "The name wansn't found"
        '    End Sub
	' 
End Class

I wrote a code that stores three names[QUOTE}

Your code actually stores 4 names.

- Make the user select a number (from 1-3) by using a (combobox)
- If the user select the proper number then a message will appear that the name is there.

How do you determine what is a "proper" number? As far as I can tell, the list of names doesn't figure into this at all. If you are trying to search the list of names then you typically are comparing a given name to the names in the list. In your case you are entering a number, not a name. If you are looking for the name that corresponds with the entered number then it not necessary to look. It's in the list at the index that was entered.

Searches on simple lists are usually done in one of two ways. If the list is unordered (yours is unordered because "Bond" occurs before "Black") then you start at the top of the list and compare each entry to the name you are looking for. If you get a match you can stop. If you get to the end of the list then the search failed.

On ordered lists you can use a binary search. In this case you start by comparing the name at the midpoint of the list. If it matches then you are done. If not then you ignore either the first or last half of the list depending on whether the current name is greater or less than the name you are looking for. You take the remaining names and repeat (check the name at the midpoint). Each iteration eliminates half of the remaining names.

Imagine a guessing game (think of a number from 1 to 100). First guess - is it 50? You answer either "yes", "bigger" or "smaller". If smaller then is it 25? If bigger then is it 75? Much faster than "is it 1", "is it 2", etc.

See if this helps.

Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        For i As Integer = 1 To NamesArray.Length - 2
            With NamesArray(i)
                If .NamesNumber = ComboBox1.SelectedIndex + 1 Then
                    MsgBox(.FirstName & " " & .LastName & " #" & .NamesNumber)
                    Exit For
                End If
            End With
        Next
    End Sub

Thank you all for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.