By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

Hashtable enumeration

P: n/a
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
Share this Question
Share on Google+
18 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
<<.>> 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
<<.>> 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 discussion thread is closed

Replies have been disabled for this discussion.