Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ForEach...next loop and hashtables

Status
Not open for further replies.

motte

Programmer
May 31, 2001
155
US
Hi all,

I'm new to C# (better at asp.net) and am having trouble with looping thru a hashtable. I can loop thru the keys but can't get the value if using a foreach loop. If I use a while loop with an enumerator I can get the keys and values. I want to do this in a foreach loop but can't. Here is some of what I've tried. Any help would be appreciated.

Code:
h.Add("aa", "apple");
h.Add("bb", "balloon");

//can get the keys but not the value at each key
foreach (string s in h.keys)
{
  //Console.WriteLine(s + ": " + h(s) + "\n");
  Console.WriteLine(s + ":" );
}

//works but want to do it in a foreach...
IDictionaryEnumerator myEnum = h.GetEnumerator();
while ( myEnum.MoveNext() ){
  Console.WriteLine("\t{0}:\t{1}", myEnum.Key, myEnum.Value);
  Console.WriteLine();
}

//not sure what to do here
foreach (myEnum.key in myEnum){
Console.WriteLine("\t{0}:\t{1}", myEnum.Key, myEnum.Key);
}

Thanks for your time, Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top