Need help storing data

  • Thread starter Thread starter Paulers
  • Start date Start date
P

Paulers

Hello all,

I am a VB.NET beginner who is unfamilier with all the different
collection types that VB.NET has to offer. I am in need of a way to
store multiple elements which can be retrieved with a unique key
(string). For example, I would like to store characteristics like first
name, last name, age, and phone number and associate them with the
unique key like a USERID. Then loop through the data collection and
pull certain elements out for processing. Which collection type would
be best suited for this purpose?

Thanks!
 
Paulers,

As you say it already in your message you need a table for your data that
can have a primary key.

In other words the "datatable".

I hope this helps,

Cor
 
Paulers said:
I am in need of a way to store multiple elements which can be
retrieved with a unique key (string). For example, I would like to
store characteristics like first name, last name, age, and phone number
and associate them with the unique key like a USERID. Then loop
through the data collection and pull certain elements out for
processing. Which collection type would be best suited for this purpose?

/Any/ of them really, because what you'll be placing into the "collection"
is a class of your own devising that contains all these properties.

HashTable is probably the easiest, though.

Public Class Person
Public Sub New( Username as String, ...
Public ReadOnly Property Age() as Integer
Public Property DoB() as DateTime
Public Property Forename() as String
Public Property Phone() as String
Public Property Surname() as String
Public ReadOnly Property Username() as String
End Class

Dim htPeople as New HashTable

htPeople.Add( "fred", New Person( "fred", ... ) )

Dim employee as Person _
= DirectCast( htPeople.Item( "fred" ), Person )

Bear in mind, though, that some collections hold things internally
as DictionaryEntry objects, so you might have to do an extra layer
of "translation" when iterating through the items therein.

HTH,
Phill W.
 
Thanks for the example! Can I talk you into an example showing how to
retrieve the information from the HashTable? I really appreciate the
help.
 
I understand that you are storing objects in the collection, but in the
Person Class how are the properties getting set? I see the declaration
but I dont see set and get methods. Do I need them in the example
above? Don't I have to define the values of each property?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top