VB.NET HashMap equivalent

35,614

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!

Share:
35,614
Admin
Author by

Admin

Updated on March 10, 2020

Comments

  • Admin
    Admin over 4 years

    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

  • Konrad Rudolph
    Konrad Rudolph over 13 years
    Never use HashTable, it’s deprecated. Dictionary is a complete replacement, is more efficient and type safe.
  • AakashM
    AakashM over 13 years
    @Konrad asker didn't say what .NET version is being used; it's possible (though unlikely) that it's pre-2.0
  • Konrad Rudolph
    Konrad Rudolph over 13 years
    @AakashM Then the answer should at least reflect this. As it stands, the answer lists them as equal alternatives (and unfortunately the MSDN doesn’t clear this up either). And as you said, this is actually pretty unlikely.
  • Admin
    Admin over 13 years
    Thanks for this. I'm using VB 2010.