473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 15 '05 #1
18 8796
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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '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 15 '05 #19

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

Similar topics

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.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.