foreach loop over a hashtable?

View: New views
3 Messages — Rating Filter:   Alert me  

foreach loop over a hashtable?

by G-14 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you do a "foreach" loop over a hashtable? And if so, are you required to loop over the KEYS, instead of the value objets?

Haven't worked much with hashtables.

Thanks,

Brian


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49

Message: http://www.houseoffusion.com/lists.cfm/link=i:44:1601
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/44
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:44
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.44
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

RE: foreach loop over a hashtable?

by Charlie Arehart :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, the quick answer is yes. Since you say "foreach" versus "for each",
I'll assume you're interested in C# (versus VB.NET). Here's an example for
you. Of course, there are many ways to skin a cat in any .NET solution, so
others might offer still different approaches:

<%@ Page language="c#" %>

<%
System.Collections.Hashtable hash = new System.Collections.Hashtable();
hash.Add( "name1", "value1" );
hash.Add( "name2", "value2" );

Response.Write("Count:" + hash.Count + "<br>");

foreach (string key in hash.Keys)
        {
                Response.Write(key + '=' + hash[key] + "<br>");
        }

%>

Yet another way to look would be to use a DictionaryEntry. This code could
be stuck into the bottom above to produce the same result another way:

Response.Write("<p>");
       
foreach (DictionaryEntry myDE in hash)
        {
                Response.Write(myDE.Key + "=" + myDE.Value  + "<br>");
        }


As a reminder, a google search of simply "system.collections.hashtable
foreach" does result in hits that still more offer examples and info.

Charlie Arehart
CTO, New Atlanta Communications, makers of BlueDragon
(678) 256-5395 charlie@...
www.newatlanta.com/bluedragon/

 

> -----Original Message-----
> From: G [mailto:brian.grant@...]
> Sent: Thursday, May 19, 2005 11:34 AM
> To: Net-Talk
> Subject: foreach loop over a hashtable?
>
> Can you do a "foreach" loop over a hashtable? And if so, are
> you required to loop over the KEYS, instead of the value objets?
>
> Haven't worked much with hashtables.
>
> Thanks,
>
> Brian
>
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:44:1603
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/44
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:44
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.44
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Re: foreach loop over a hashtable?

by G-14 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ended up using the DictionaryEntry method, as that's a truer "for each"
where I can access every aspect of the hashtable at each "node".

Thanks all!


> Well, the quick answer is yes. Since you say "foreach" versus "for each",
> I'll assume you're interested in C# (versus VB.NET). Here's an example for
> you. Of course, there are many ways to skin a cat in any .NET solution, so
> others might offer still different approaches:
>
> <%@ Page language="c#" %>
>
> <%
> System.Collections.Hashtable hash = new System.Collections.Hashtable();
> hash.Add( "name1", "value1" );
> hash.Add( "name2", "value2" );
>
> Response.Write("Count:" + hash.Count + "<br>");
>
> foreach (string key in hash.Keys)
> {
> Response.Write(key + '=' + hash[key] + "<br>");
> }
>
> %>
>
> Yet another way to look would be to use a DictionaryEntry. This code could
> be stuck into the bottom above to produce the same result another way:
>
> Response.Write("<p>");
>
> foreach (DictionaryEntry myDE in hash)
> {
> Response.Write(myDE.Key + "=" + myDE.Value  + "<br>");
> }
>
>
> As a reminder, a google search of simply "system.collections.hashtable
> foreach" does result in hits that still more offer examples and info.
>
> Charlie Arehart
> CTO, New Atlanta Communications, makers of BlueDragon
> (678) 256-5395 charlie@...
> www.newatlanta.com/bluedragon/
>
>
>
>> -----Original Message-----
>> From: G [mailto:brian.grant@...]
>> Sent: Thursday, May 19, 2005 11:34 AM
>> To: Net-Talk
>> Subject: foreach loop over a hashtable?
>>
>> Can you do a "foreach" loop over a hashtable? And if so, are
>> you required to loop over the KEYS, instead of the value objets?
>>
>> Haven't worked much with hashtables.
>>
>> Thanks,
>>
>> Brian
>>
>>
>>
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:44:1604
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/44
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:44
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.44
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54