Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, a few days ago I learned how to use a Dictionary list so I could create a project. Later, requirements changed and I had to look at List(of T). Right now I have a Contact class that contains: firstName, lastName, phoneNumber, and birthday.

I'm able to add these fields to the list, but I don't know how to get all the related information back when I need it. What I need is, once I have multiple records in the list, how do I retrieve all the related fields.

So, what do I do to find all this information that belong together? How do I know that this first name goes with this last name?

I hope this makes sense. If it doesn't, just let me know and I will try again.

Thanks for any help.

What I have tried:

Here's my current code that I've used just to load things up so I can see how it works.

Dim thisContact As New List(Of Contact)()

' Add Contact info to the list.
thisContact.Add(New Contact() With {
.FirstName = "Joe",
.LastName = "Langley",
.PhoneNumber = "2175443",
.Birthday = CDate("05/16/1954")})

For Each detail In thisContact
MsgBox("First Name: " & detail.FirstName _
& Environment.NewLine & "Last name: " & detail.LastName _
& Environment.NewLine _
& Environment.NewLine & "Phone Number: " & detail.PhoneNumber _
& Environment.NewLine & "birthDaty: " & detail.Birthday)
Next
Posted
Updated 12-Apr-16 3:23am
v5

1 solution

When you created the class, you gave it properties: FirstName and LastName. Which means that each instance of a class has it's own set of LastName and FirstName, which aren;t share with any other instance.
If you like, think of a car. Each car has it's own key, and it's own glove box.
Take five cars, and each will open with it's own key only - and inside each car is a separate glove box. If you place an item - your mobile phone, say - in the glove box of the Green car, then you wouldn't expect to find it in the Red or Blue cars, would you?
So if you create two instance of your Contact class (by using New twice) each Contact instance has independant FirstName and LastName:
VB.NET
Dim firstPerson As New Contact()
firstPerson.FirstName = "joe"
firstPerson.LastName = "smith"
Dim secondPerson As New Contact()
secondPerson.FirstName = "mike"
secondPerson.LastName = "hunt"
Console.WriteLine("{0}:{1}", firstPerson.FirstName, firstPerson.LastName)
Console.WriteLine("{0}:{1}", secondPerson.FirstName, secondPerson.LastName)


" Is there a better way to keep up with firstperson and secondperson and on and on?"

Of course there is! Just use a List (Of Contact):
VB.NET
Dim contacts As New List(Of Contact)()
Dim newContact As New Contact()
newContact.FirstName = "joe"
newContact.LastName = "smith"
contacts.Add(newContact)
newContact = New Contact()
newContact.FirstName = "mike"
newContact.LastName = "hunt"
contacts.Add(newContact)
For Each c As Contact In contacts
	Console.WriteLine("{0}:{1}", c.FirstName, c.LastName)
Next


[edit]Forgot to add the second contact to the List! :O[/edit]
 
Share this answer
 
v3
Comments
jumper777 12-Apr-16 9:39am    
You sure cleared one thing up for me. I didn't realize the each person was their own instance of the Contact class. Guess I'm a bit thick headed. But now I wonder about another thing about your answer. All of this is going to run in a GUI front end. Is there a better way to keep up with firstperson and secondperson and on and on?
OriginalGriff 12-Apr-16 9:51am    
Answer updated
jumper777 12-Apr-16 10:10am    
Hi Griff, that's awesome. That probably gets me over the biggest hump there will be. The beginning of understanding is always the hardest part for me.

I would like to be able to say I accept your solution but you need to put it into the solution box (if you want), then I'll accept it. Thanks so much for your help.
OriginalGriff 12-Apr-16 10:15am    
Um...it is in the solution box! :laugh:
If it wasn't, I couldn't use the pre tags to format the code and engage the syntax highlighter! :laugh:
jumper777 12-Apr-16 10:25am    
I apologize for being an idiot <smile> I just accepted your solution and really appreciate your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900