Abnormal behavior of Hashtable

  • Thread starter Thread starter Calvin Lai
  • Start date Start date
C

Calvin Lai

Hi all,

I have the following code which should work. However, an exception (
InvalidOperationException) was thrown which doesn't make sense to me.

line 1: foreach (string name in _fieldMaps.Keys)

line 2: _fieldMaps[name] = 1;

where _fieldMaps is a hashtable containing (string, int) pairs internally.

Does anyone know why this exception is thrown? I tried debug it and found
the exception was thrown during the 2nd loop.
i.e. execution steps:
line1, line2, line1, EXCEPTION.

Thanks for any inputs here.

-Cal
 
Calvin,

I would think that the second key in the hashtable is not a string, but
rather, another type. Have you debugged the code to see what the key is and
the type? If it can not be cast to a string, then this is what you will
get.

Hope this helps.
 
I have the following code which should work.

No, it shouldn't.
However, an exception (
InvalidOperationException) was thrown which doesn't make sense to me.

line 1: foreach (string name in _fieldMaps.Keys)

line 2: _fieldMaps[name] = 1;

where _fieldMaps is a hashtable containing (string, int) pairs internally.

Does anyone know why this exception is thrown? I tried debug it and found
the exception was thrown during the 2nd loop.
i.e. execution steps:
line1, line2, line1, EXCEPTION.

Thanks for any inputs here.

The key is to look at the message in the exception:

"Collection was modified; enumeration operation may not execute."

Basically, you can't modify a collection while you're enumerating it.

From the docs for IEnumerable:

<quote>
An enumerator remains valid as long as the collection remains
unchanged. If changes are made to the collection, such as adding,
modifying or deleting elements, the enumerator is irrecoverably
invalidated and the next call to MoveNext or Reset throws an
InvalidOperationException.
</quote>

Now, although you haven't *actually* changed the collection of keys,
you've changed the collection it's modelling (which includes the
values), which is why you're getting the problem.
 
Hi Calvin,
You cannot change the collection inside foreach loop. That exception is
thrown by IEnumerator.MoveNext method.
If you want ot modify the collection dont't use enumartaors (objects that
implement IEnumerator interface) Foreach internally uses enumerators to
iterate through collections.
Use another kind of loop. *for* and *while* both will do.

HTH
B\rgds
100
 
Nicholas Paldino said:
I would think that the second key in the hashtable is not a string, but
rather, another type. Have you debugged the code to see what the key is and
the type? If it can not be cast to a string, then this is what you will
get.

No - if the key wasn't a string, the OP would have got an
InvalidCastException, not an InvalidOperationException.
 
Thanks all for you input. It helps a lot. It was driving me crazy...



100 said:
Hi Calvin,
You cannot change the collection inside foreach loop. That exception is
thrown by IEnumerator.MoveNext method.
If you want ot modify the collection dont't use enumartaors (objects that
implement IEnumerator interface) Foreach internally uses enumerators to
iterate through collections.
Use another kind of loop. *for* and *while* both will do.

HTH
B\rgds
100

Calvin Lai said:
Hi all,

I have the following code which should work. However, an exception (
InvalidOperationException) was thrown which doesn't make sense to me.

line 1: foreach (string name in _fieldMaps.Keys)

line 2: _fieldMaps[name] = 1;

where _fieldMaps is a hashtable containing (string, int) pairs internally.

Does anyone know why this exception is thrown? I tried debug it and found
the exception was thrown during the 2nd loop.
i.e. execution steps:
line1, line2, line1, EXCEPTION.

Thanks for any inputs here.

-Cal
 
You can't alter the hashtable in a foreach loop -- the enumerator becomes
invalid, and then on your next
enum.MoveNext() (which is generated by the c# compiler from your
foreach(...) code) the InvalidOperationException is thrown.

What you _can_ do is place an object in the hashtable, and alter values of
that object:

class Holder{
public int SomeField;
public Holder(int val){ SomeField = val;}
}

....
Hashtable h = new Hashtable();
h.Add("bob", new Holder(1));
h.Add("charlie", new Holder(2));
h.Add("jim", new Holder(3));
foreach(string name in h.Keys)
{
((Holder)h[name]).SomeField = 4;
}
 
Back
Top