Andrew
Andrew

Reputation: 161

VB.NET HashMap equivalent

I'm trying to store a set of objects and I need to be able to access them in constant time based on a particular property of the objects. I was hoping to do this by adding the objects to a HashMap and using the property that I want to index by as the key. Is there a HashMap object in VB like in Java, or should I use something else?

Update: Using VB 2010, .NET 4

Cheers

Upvotes: 16

Views: 39462

Answers (1)

Morvader
Morvader

Reputation: 2313

Depending on your needs you could use a HashTable or a Dictionary.

like this:

Dim dictionary As New Dictionary(Of String, Integer)
dictionary.Add("Dot", 20)
dictionary.Add("Net", 1)
dictionary.Add("Perls", 10)
dictionary.Add("Visual", -1)

Dim Hashtable As New Hashtable()
hashtable.Add("Area", 1000)
hashtable.Add("Perimeter", 55)
hashtable.Add("Mortgage", 540)

Have a look at this and this for more usage examples.

UPDATE:

But, as @Konrad Rudolph says, its better to use a Dictionary for multiple reasons. (On .NET 2.0 and obove)

Thanks for the comment!

Upvotes: 23

Related Questions