473,473 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hashtable enumeration

How can I loop thru a hashtable changing the Value of each entry ? Whatever
I try I always seem to get the error about modifying the collection within
the loop.
Nov 22 '05 #1
18 3094
Check out the following article, it might help shed some light on things...

http://msdn.microsoft.com/vcsharp/de...res_guide2.asp

Take care,
Ben S. Stahlhood II
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within
the loop.

Nov 22 '05 #2
foreach (DictionaryEntry de in blahblah)
{
}
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within
the loop.

Nov 22 '05 #3
From the docs:
The foreach statement is a wrapper around the enumerator, which only allows
reading from, not writing to, the collection.

<di********@discussion.microsoft.com> wrote in message
news:u$**************@tk2msftngp13.phx.gbl...
foreach (DictionaryEntry de in blahblah)
{
}
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ?

Whatever
I try I always seem to get the error about modifying the collection within the loop.


Nov 22 '05 #4
Yes I know but you try within that loop modifying de.Value - it wont let you
change the collection.

<di********@discussion.microsoft.com> wrote in message
news:u$**************@tk2msftngp13.phx.gbl...
foreach (DictionaryEntry de in blahblah)
{
}
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ?

Whatever
I try I always seem to get the error about modifying the collection within the loop.


Nov 22 '05 #5
Well it seems crazy but to do this I have actually implemented two loops -
one to store all the keys of the hashtable in an arraylist, then another to
loop thru that arraylist and set each hashtable entry value by its key.

I'm thinking there must be a better way - but maybe not.

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within
the loop.

Nov 22 '05 #6
Cant you just get the current item, remove it and replace it if it wont let
u update it?
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
Well it seems crazy but to do this I have actually implemented two loops -
one to store all the keys of the hashtable in an arraylist, then another to loop thru that arraylist and set each hashtable entry value by its key.

I'm thinking there must be a better way - but maybe not.

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ?

Whatever
I try I always seem to get the error about modifying the collection within the loop.


Nov 22 '05 #7
JezB wrote:
How can I loop thru a hashtable changing the Value of each entry ? Whatever
I try I always seem to get the error about modifying the collection within
the loop.


This might not be the best solution, but it works:

// Create and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add( "one", "The" );
myHT.Add( "two", "quick" );
myHT.Add( "three", "brown" );
myHT.Add( "four", "fox" );

// get the keys
ICollection keys = myHT.Keys;

// copy the key collection so we can iterate over it without
// the requirement that the hashtable not change.
object [] keyArray = new object [keys.Count];
keys.CopyTo( keyArray, 0);

// now change all the hashtab;e values
foreach (object k in keyArray) {
string newval = (string) myHT[k] + " changed";
myHT.Remove( k);
myHT.Add( k, newval);
}

Eric Gunnerson's article here:

http://msdn.microsoft.com/library/en...rp09182003.asp

has some examples that provide a more object-oriented way of doing this
(see the IterIsolate class).

--
mikeb
Nov 22 '05 #8
Jez,

Just do the following:

ArrayList arrayList = new ArrayList(hashtable.Keys);
IEnumerator listEnumerator = arrayList.GetEnumerator();
while (listEnumerator.MoveNext()) {
hashtable[listEnumerator.Current] = "value";
} // while

HTH

Take care,
Ben S. Stahlhood II

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
Well it seems crazy but to do this I have actually implemented two loops -
one to store all the keys of the hashtable in an arraylist, then another to loop thru that arraylist and set each hashtable entry value by its key.

I'm thinking there must be a better way - but maybe not.

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ?

Whatever
I try I always seem to get the error about modifying the collection within the loop.


Nov 22 '05 #9
Maybe you could loop through the keys collection instead of the main
collection:

foreach(object key in TheHashTable.Keys)
TheHashTable[key] = newValue;

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within
the loop.

Nov 22 '05 #10
<<.>> wrote:
Cant you just get the current item, remove it and replace it if it wont let
u update it?


Removing it counts as changing the collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #11
Tried that but no ... you have to copy the keys to an arraylist first - this
seems to be the best solution.

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
Maybe you could loop through the keys collection instead of the main
collection:

foreach(object key in TheHashTable.Keys)
TheHashTable[key] = newValue;

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ?

Whatever
I try I always seem to get the error about modifying the collection within the loop.


Nov 22 '05 #12
Jez,

I posted the solution a few entries back... You do not have to do two loops
like you are doing... Please check the post and let me know if this is what
you were looking for

Take care,
Ben S. Stahlhood II
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:e0**************@TK2MSFTNGP12.phx.gbl...
Tried that but no ... you have to copy the keys to an arraylist first - this seems to be the best solution.

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
Maybe you could loop through the keys collection instead of the main
collection:

foreach(object key in TheHashTable.Keys)
TheHashTable[key] = newValue;

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ?

Whatever
I try I always seem to get the error about modifying the collection within the loop.



Nov 22 '05 #13
OMG REALLY! You are a genius!
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<<.>> wrote:
Cant you just get the current item, remove it and replace it if it wont let u update it?


Removing it counts as changing the collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #14
Yes it is - I like your approach the best - thanks.

"Ben S. Stahlhood II" <ben[.dot.]stahlhood[.at.]intellified[.dot.]com> wrote
in message news:e3**************@TK2MSFTNGP12.phx.gbl...
Jez,

I posted the solution a few entries back... You do not have to do two loops like you are doing... Please check the post and let me know if this is what you were looking for

Take care,
Ben S. Stahlhood II
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:e0**************@TK2MSFTNGP12.phx.gbl...
Tried that but no ... you have to copy the keys to an arraylist first -

this
seems to be the best solution.

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
Maybe you could loop through the keys collection instead of the main
collection:

foreach(object key in TheHashTable.Keys)
TheHashTable[key] = newValue;

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
> How can I loop thru a hashtable changing the Value of each entry ?
Whatever
> I try I always seem to get the error about modifying the collection

within
> the loop.
>
>



Nov 22 '05 #15
100
Hi JezB,
You cannot do that with foreach loop or at least not with the enumerator
object provided by Hashtable class.
You can find a way how to do this in the following article. It has a code
for enumerator class that can be used to iterate over and change a hashtable
collection
http://msdn.microsoft.com/library/de...rp01212002.asp

HTH
B\rgds
100

"JezB" <je*************@blueyonder.co.yk> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within
the loop.

Nov 22 '05 #16
No problem, anytime =]

Take care,
Ben S. Stahlhood II
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:ev*************@TK2MSFTNGP12.phx.gbl...
Yes it is - I like your approach the best - thanks.

"Ben S. Stahlhood II" <ben[.dot.]stahlhood[.at.]intellified[.dot.]com> wrote in message news:e3**************@TK2MSFTNGP12.phx.gbl...
Jez,

I posted the solution a few entries back... You do not have to do two

loops
like you are doing... Please check the post and let me know if this is

what
you were looking for

Take care,
Ben S. Stahlhood II
"JezB" <je*************@blueyonder.co.yk> wrote in message
news:e0**************@TK2MSFTNGP12.phx.gbl...
Tried that but no ... you have to copy the keys to an arraylist first -
this
seems to be the best solution.

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
> Maybe you could loop through the keys collection instead of the main
> collection:
>
> foreach(object key in TheHashTable.Keys)
> TheHashTable[key] = newValue;
>
>
>
>
>
> "JezB" <je*************@blueyonder.co.yk> wrote in message
> news:uA**************@TK2MSFTNGP10.phx.gbl...
> > How can I loop thru a hashtable changing the Value of each entry ?
> Whatever
> > I try I always seem to get the error about modifying the

collection within
> > the loop.
> >
> >
>
>



Nov 22 '05 #17
mikeb wrote:
JezB wrote:
How can I loop thru a hashtable changing the Value of each entry ?
Whatever
I try I always seem to get the error about modifying the collection
within
the loop.
This might not be the best solution, but it works:

// Create and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add( "one", "The" );
myHT.Add( "two", "quick" );
myHT.Add( "three", "brown" );
myHT.Add( "four", "fox" );

// get the keys
ICollection keys = myHT.Keys;

// copy the key collection so we can iterate over it without
// the requirement that the hashtable not change.
object [] keyArray = new object [keys.Count];
keys.CopyTo( keyArray, 0);

// now change all the hashtab;e values
foreach (object k in keyArray) {
string newval = (string) myHT[k] + " changed";
myHT.Remove( k);
myHT.Add( k, newval);
}

Eric Gunnerson's article here:

http://msdn.microsoft.com/library/en...rp09182003.asp


oops... I meant to send you here:

http://msdn.microsoft.com/library/en...rp01212002.asp


has some examples that provide a more object-oriented way of doing this
(see the IterIsolate class).

--
mikeb
Nov 22 '05 #18
<<.>> wrote:
OMG REALLY! You are a genius!


If it's so obvious that it wouldn't work, why did you suggest it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pickle | last post by:
When traversing an Enumeration of Hashtable keys, I know that modifying the HT can cause problems. But is doing lookups okay? E.g., in the following code: <assume ht is a reference to a...
5
by: Keith Langer | last post by:
I have a hashtable which is accessed by two threads. One thread does all writing and enumeration in the hashtable, and the other thread has read-only access to the table directly through keys but...
18
by: JezB | last post by:
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within the loop.
4
by: Vladimir C. | last post by:
Hashtable map = new Hashtable(); map = 10; map = 20; foreach(DictionaryEntry e in map) { e.Value = 100; Console.WriteLine("{0}: {1}", key, map); }
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
16
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
2
by: Tony | last post by:
I have this problem - I have a hashtable, containing a list of filenames. Every 60 seconds, I have a thread that enumerates thru this hashtable, and based on some simple logic, some of the items...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
4
by: Mark S. | last post by:
Much to my surprised the code below compiled and ran. I just don't know enough about threading to know for sure if this is too good to be true. I'm attempting to isolate the Hashtable lock to...
3
by: NagarajanS | last post by:
Hi, i need one help in HashTable. see the below example /* * HashTableTest.java * * Created on October 20, 2007, 12:48 PM * * To change this template, choose Tools | Template Manager *...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.