Help with array searching
Hello again I need some help with searching through arrays. Here is what I have to do. I have a list box with names and id numbers. When I choose a persons name and corresponding id number the result is the persons address. The addresses are preloaded into the program. I am having trouble figuring out where to start. Can anyone help me as to where to start in writing the code to start the search?
Re: Help with array searching
Can you post the code where you are declaring the array that holds the addresses and where they are "preloaded" ?
Also, have you considered using a database to store these addresses as it would make things much easier to add/modify new names & addresses in the future
Re: Help with array searching
The easiest way would be to put all the information into a Person object and add the Persons to the combobox. If you override the ToString method (which is what the combobox displays by default), you can have it show the name and id. Then where you add the addresses, you add them to the Person objects and then just grab them from that object when it's selected:
Code:
Public Class Person
private _id as String
private _name as String
private _address as String
Public Property ID as String
Get
Return _id
End Get
Set (Value as String)
_id = Value
End Set
End Property
Public Property Name as String
Get
Return _name
End Get
Set (Value as String)
_name = Value
End Set
End Property
Public Property Address as String
Get
Return _address
End Get
Set (Value as String)
_address = Value
End Set
End Property
Public Overrides Function ToString() As String
Return _name + " - " + _id
End Function
End Class
Then to load them into the combobox:
Code:
Dim p as new Person
p.ID = "1"
p.Name = "Joe Smith"
p.Address = "123 Main St"
ComboBox1.Items.Add(p)
Now ComboBox1 will display "Joe Smith - 1"
To get the person and the corresponding address:
Code:
Dim sAddress as String = DirectCast(ComboBox1.SelectedItem, Person).Address
Re: Help with array searching
I am unable to show toe code for declaring the addresses because that code was hidden by my instructor and this vb specific, not dealing with databases. Guess that doesn't help me any further.