hasthable question

C

Colin McGuire

Hi, I am just playing with the Hashtable class and, correct me if I am
wrong please, but is it correct to say that I have to retrieve a
specific value for a given key by creating an enumerator and process
everything until I find the key (as in my routine below getValue()) -
ie I just can't say here it the key, now give me the value? If this is
the case I would appreciate a suggestion on an alternative to class
Hashtable that I could use.

Thank you
Colin


Dim ht As New Hashtable

Private Sub populate()
ht.Add("key1", "the")
ht.Add("key2", "quick")
ht.Add("key3", "brown")
ht.Add("key4", "fox")
ht.Add("key5", "jumps")
End Sub

Private Function getValue(ByVal key As String)
Dim valueFound As String = ""
Dim en As IDictionaryEnumerator = ht.GetEnumerator()
While en.MoveNext() And valueFound <> ""
If en.Key = key Then valueFound = en.Value
End While
Return valueFound
End Sub


'And somewhere else in my program
'
Call populate()
'
'
MsgBox getValue("key4")
 
J

Jay B. Harlow [MVP - Outlook]

Colin,
Its by far easier to use the Indexer (default Item property) to return a
value given a key

MessageBox.Show(ht("key4"))

is the same as:

MessageBox.Show(ht.Item("key4"))

The above will also perform better. As a hash table stores values so they
are easily retrieved by key.

Hope this helps
Jay
 
F

Fergus Cooney

Hi Colin,

As Jay says (but without the pun) the "key" is the key to getting your
value.

The idea that you picked up of needed an enumerator (or a for loop, either
one'll do) is when you want the reverse - you know what data you put in the
HashTable, but can't for the life of you remember what the key is.

Then you use the loop that you showed but swap all instances of 'key' and
'value' around. Oh, and change its name to getKey. ;-)

Just to give you a bit of background (and simplified at that). Every key
value is converted into an index into the HashTable's internal list. This
index is then used to retrieve the value. That's why it's so much quicker to
find a value using the key than it is use a loop. This difference in time can
be considerable when using large HashTables.

The conversion of a key into an index is called 'hashing' and all objects
know how to do this (some better than others) as there's a property called
GetHashCode inherited from Object. That's allows you to use anything as a key,
but numbers and strings are best.

You were using this to add your values:
ht.Add("key1", "the")
ht.Add("key2", "quick")

but you could have done this instead:

ht ("key1") = "the"
ht ("key2") = "quick"

which looks just like an array indexed with a string.

And that's why
OhMyWord = ht ("key1")
can retrieve it.

Regards,
Fergus
 
R

Robin Tucker

Yes, you can just say "here is the key, get me the value", like this:

Dim myValue As myValueType
Dim myKey As myKeyType

myValue = theHashTable(myKey)
 

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

Top