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.

Re: Hashtable enumeration by Ben

Ben
Fri Jan 23 09:23:42 CST 2004

Check out the following article, it might help shed some light on things...

http://msdn.microsoft.com/vcsharp/default.aspx?pull=/library/en-us/dv_vstechart/html/datastructures_guide2.asp

Take care,
Ben S. Stahlhood II


"JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:uAp8GRc4DHA.2276@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.
>
>



Re: Hashtable enumeration by discussion

discussion
Fri Jan 23 09:37:46 CST 2004

foreach (DictionaryEntry de in blahblah)
{
}


"JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:uAp8GRc4DHA.2276@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.
>
>



Re: Hashtable enumeration by Sanjay

Sanjay
Fri Jan 23 09:36:16 CST 2004

From the docs:
The foreach statement is a wrapper around the enumerator, which only allows
reading from, not writing to, the collection.

<discussion@discussion.microsoft.com> wrote in message
news:u$LpuXc4DHA.1704@tk2msftngp13.phx.gbl...
> foreach (DictionaryEntry de in blahblah)
> {
> }
>
>
> "JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> news:uAp8GRc4DHA.2276@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.
> >
> >
>
>



Re: Hashtable enumeration by JezB

JezB
Fri Jan 23 10:01:03 CST 2004

Yes I know but you try within that loop modifying de.Value - it wont let you
change the collection.

<discussion@discussion.microsoft.com> wrote in message
news:u$LpuXc4DHA.1704@tk2msftngp13.phx.gbl...
> foreach (DictionaryEntry de in blahblah)
> {
> }
>
>
> "JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> news:uAp8GRc4DHA.2276@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.
> >
> >
>
>



Re: Hashtable enumeration by JezB

JezB
Fri Jan 23 10:03:32 CST 2004

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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:uAp8GRc4DHA.2276@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.
>
>



Re: Hashtable enumeration by >

>
Fri Jan 23 10:18:13 CST 2004

Cant you just get the current item, remove it and replace it if it wont let
u update it?


"JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:eyfZHqc4DHA.2168@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> news:uAp8GRc4DHA.2276@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.
> >
> >
>
>



Re: Hashtable enumeration by mikeb

mikeb
Fri Jan 23 10:09:31 CST 2004

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-us/dncscol/html/csharp09182003.asp

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

--
mikeb

Re: Hashtable enumeration by k_sashank

k_sashank
Fri Jan 23 10:11:07 CST 2004


H

This code seems to work fin

Dim ht As New Hashtabl

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa

For var As Integer = 0 To 1
ht.Add(var, var
ListBox1.Items.Add(var.ToString
Nex

ListBox1.Items.Add(ht.Values
End Su

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
For Each var As DictionaryEntry In H
var.Value =
ListBox2.Items.Add(var.ToString
Nex

End Su

----- Sanjay Vyas wrote: ----

From the docs
The foreach statement is a wrapper around the enumerator, which only allow
reading from, not writing to, the collection

<discussion@discussion.microsoft.com> wrote in messag
news:u$LpuXc4DHA.1704@tk2msftngp13.phx.gbl..
> foreach (DictionaryEntry de in blahblah
>
>
>>> "JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in messag
> news:uAp8GRc4DHA.2276@TK2MSFTNGP10.phx.gbl..
>> How can I loop thru a hashtable changing the Value of each entry
> Whateve
>> I try I always seem to get the error about modifying the collectio
withi
>> the loop
>>>>>>

Re: Hashtable enumeration by Ben

Ben
Fri Jan 23 10:15:33 CST 2004

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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:eyfZHqc4DHA.2168@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> news:uAp8GRc4DHA.2276@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.
> >
> >
>
>



Re: Hashtable enumeration by J

J
Fri Jan 23 10:20:03 CST 2004

Maybe you could loop through the keys collection instead of the main
collection:

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





"JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:uAp8GRc4DHA.2276@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.
>
>



Re: Hashtable enumeration by Jon

Jon
Fri Jan 23 10:22:34 CST 2004

<<.>> 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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Hashtable enumeration by JezB

JezB
Fri Jan 23 10:24:56 CST 2004

Tried that but no ... you have to copy the keys to an arraylist first - this
seems to be the best solution.

"J.Marsch" <jeremy@ctcdeveloper.com> wrote in message
news:O8w1Nzc4DHA.1428@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> news:uAp8GRc4DHA.2276@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.
> >
> >
>
>



Re: Hashtable enumeration by Ben

Ben
Fri Jan 23 10:32:12 CST 2004

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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:e0BZI4c4DHA.1636@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" <jeremy@ctcdeveloper.com> wrote in message
> news:O8w1Nzc4DHA.1428@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> > news:uAp8GRc4DHA.2276@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.
> > >
> > >
> >
> >
>
>



Re: Hashtable enumeration by >

>
Fri Jan 23 10:53:29 CST 2004

OMG REALLY! You are a genius!


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a7b5aa17b84a76d989f60@msnews.microsoft.com...
> <<.>> 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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Re: Hashtable enumeration by JezB

JezB
Fri Jan 23 10:45:50 CST 2004

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:e3ac35c4DHA.1672@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> news:e0BZI4c4DHA.1636@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" <jeremy@ctcdeveloper.com> wrote in message
> > news:O8w1Nzc4DHA.1428@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> > > news:uAp8GRc4DHA.2276@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.
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Re: Hashtable enumeration by 100

100
Fri Jan 23 10:53:49 CST 2004

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/default.asp?url=/library/en-us/dncscol/html/csharp01212002.asp

HTH
B\rgds
100

"JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:uAp8GRc4DHA.2276@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.
>
>



Re: Hashtable enumeration by Ben

Ben
Fri Jan 23 10:53:04 CST 2004

No problem, anytime =]

Take care,
Ben S. Stahlhood II


"JezB" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
news:evjiqBd4DHA.488@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:e3ac35c4DHA.1672@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> > news:e0BZI4c4DHA.1636@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" <jeremy@ctcdeveloper.com> wrote in message
> > > news:O8w1Nzc4DHA.1428@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" <jeremy.bradshaw@blueyonder.co.yk> wrote in message
> > > > news:uAp8GRc4DHA.2276@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.
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Re: Hashtable enumeration by mikeb

mikeb
Fri Jan 23 11:25:54 CST 2004

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-us/dncscol/html/csharp09182003.asp

oops... I meant to send you here:

http://msdn.microsoft.com/library/en-us/dncscol/html/csharp01212002.asp


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


--
mikeb

Re: Hashtable enumeration by Jon

Jon
Fri Jan 23 11:45:22 CST 2004

<<.>> wrote:
> OMG REALLY! You are a genius!

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

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