accessing data by id

x86

Member
Joined
Jan 22, 2006
Messages
10
Programming Experience
Beginner
I'm trying to access the data stored using the id that i assigned when inputting the data

here is some pseudocode:
VB.NET:
dim id = inputbox("Enter a id number") // let's say i entered 50 
Dim firstName = InputBox("Enter first name:") // let's say i entered "Fred" 
Dim lastName = InputBox("Enter last name:") // let's say i entered "Flintstone" 
 
dim id = inputbox("Enter a id number") // let's say i entered 51 
Dim firstName = InputBox("Enter first name:") // let's say i entered "Wilma" 
Dim lastName = InputBox("Enter last name:") // let's say i entered "Flintstone" 
 
Dim findById = inputbox("Enter the id number to display the first and last name:") // let's say i entered 50 
It should show Fred Flintstone because he is referenced by id 50

I need to know how to refernce the data, not by index, but by id.
Thanks. :)
 
You have to save the ID values somewhere and create a correspondence between them and the names. You would use some type of dictionary, like Hashtabel, SortedList, StringDictionary, NameValueCollection or one of your own creation, depending on the types of the values you want to store. If you want to use Integers as keys then that means StringDictionary and NameValueCollection are out. You could concatenate the first name and last name into a single string, or else you could create your own type with those two properties.
 
I'm actually looking for something really simple that i can accomplish through arrays. I'm not creating a full app.
:)
 
Well i don't know a way to do that simply. However you can concatenate the id,firstname and second name into a single string. Then place it into the array. Upon retreiving it you will need to strip out and examine the first two characters in each item in the array and run code to examine it to see if it matches the id you want to search.
 
If you want a quick and dirty solution then you can use concurrent arrays, which means more than one array where there is understood to be a correspondence between the elements at the same index in each array. I say "understood" because there is no way to specifically enforce that relationship. I'd actually suggest using collections rather than arrays. They are similar but a collection can grow and shrink dynamically, whereas an array is more trouble when you need to resize it. The ideal option here is the StringCollection, in which the items strongly typed to String. You could declare three collections like this:
VB.NET:
Private ids As New Specialized.StringCollection
Private firstNames As New Specialized.StringCollection
Private lastNames As New Specialized.StringCollection
You would then call the Add method of each to add a new ID, first name and last name to the appropriate collection. When the user entered an ID you would use the IndexOf method to get the index of the ID in the collection. You could then use that index to get the corresponding first name and last name. That's how concurrent arrays, or in this situation collections, work. I'm not going to provide actual code because, frankly, this sounds like homework to me.
 
figured it out, i only needed this line:
nameOfArray(placeWhereIstoredID) = placeWhereIcanStoreName

i'm so ingenious! arn't i? ;)
 
Back
Top