VB.net For Each through Hashtable?

  • Thread starter Thread starter Stefan Richter
  • Start date Start date
S

Stefan Richter

Hi,

I would like to go through a hashtable and take the next value

to save it into an ArrayList.

How to do that in the most easiest way???

I am looking for something like:



For Each Object value In clientsHashTable

dropDownList.Add(value)

End for | NEXT value

Return dropDownList.ToArray



Thx,



Stefan
 
Stefan Richter said:
I would like to go through a hashtable and take the next value

to save it into an ArrayList.

How to do that in the most easiest way???

I am looking for something like:

For Each Object value In clientsHashTable

dropDownList.Add(value)

End for | NEXT value

Return dropDownList.ToArray

Using For Each with a Hashtable gives you DictionaryEntry references -
you can then look at the Key and Value properties. Here's a sample:

Option Strict On

Imports System
Imports System.Collections

Public Class Test

Public Shared Sub Main()
Dim table As New Hashtable()

table.Add ("Key1", "Value1")
table.Add ("Key2", "Value2")

Dim entry As DictionaryEntry
For Each entry in table
Console.WriteLine ("{0}={1}", entry.Key, entry.Value)
Next
End Sub
End Class
 
Okay, that worked fine.
But how can I access a single entry,

like I want to get the key for a certain value or the value for a certain
key???

Stefan
 
Stefan Richter said:
Okay, that worked fine.
But how can I access a single entry,

like I want to get the key for a certain value or the value for a certain
key???

If you want the key for a certain value, that suggests you've got your
hashtable the wrong way round - I suggest you keep two hashtables, one
for key -> value, and one for value -> key.

For key -> value, use the indexer:

value = hashtable(key)
 
Back
Top