473,324 Members | 2,548 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,324 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 22 '05 #1
18 3087
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.