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.
Reverend Jim
Illigitimae non carborundum
3,743 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33
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
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
Question Answered as of 1 Year Ago by
codeorder
and
Reverend Jim