Hashtable enumeration

J

JezB

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.
 
S

Sanjay Vyas

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

JezB

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

JezB

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.
 
M

mikeb

JezB said:
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).
 
G

Guest

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
 
B

Ben S. Stahlhood II

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
 
J

J.Marsch

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

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

JezB

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

J.Marsch said:
Maybe you could loop through the keys collection instead of the main
collection:

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





JezB said:
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.
 
B

Ben S. Stahlhood II

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 said:
Tried that but no ... you have to copy the keys to an arraylist first - this
seems to be the best solution.

J.Marsch said:
Maybe you could loop through the keys collection instead of the main
collection:

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





JezB said:
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.
 
J

JezB

Yes it is - I like your approach the best - thanks.

Ben S. Stahlhood II said:
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 said:
Tried that but no ... you have to copy the keys to an arraylist first - this
seems to be the best solution.

J.Marsch said:
Maybe you could loop through the keys collection instead of the main
collection:

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





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.
 
B

Ben S. Stahlhood II

No problem, anytime =]

Take care,
Ben S. Stahlhood II


JezB said:
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:[email protected]...
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 said:
Tried that but no ... you have to copy the keys to an arraylist
first -
this
seems to be the best solution.

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

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





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.
 
M

mikeb

mikeb said:
JezB said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top