System.InvalidOperationException and IDictionary

F

farseer

I am recieving the folllowing error with the code below: "A first
chance exception of type 'System.InvalidOperationException' occurred in
mscorlib.dll"

There are two items in _keyMap. after the first iteration (when it's
about to move to the 2nd item, the error above occurs. This error
happens on the "foreach" line itself. WOuld anyone have a clue why?

private IDictionary _keyMap = new Hashtable( );
....
foreach ( DictionaryEntry entry in _keyMap )
{
Key key = (Key) entry.Key;
KeyModifier km = ( KeyModifier ) entry.Value;
ProcessKey( key, Km);
}

I have tried changing IDictionary to HashTable and i have ried using
GetEnumerator and MoveNext, but the same error occurs.
 
A

Alex Feinman [MVP]

When debugger stops on error, take a look at the stack window. Right-click
on it and ensure that Show Non-user code option is checked. Post the top
line from the stack
 
F

farseer

thanks Alex. i did as you suggested...the error is occuring at:
"
mscorlib.dll!System.Collections.Hashtable.HashtableEnumerator.MoveNext()
+ 0x19 bytes"

Between that message and the earlier one i posted, it's difficult to
figure much out here...

could this me a HasTable bug in the compact framework itself or VSTO
2005 beta 2?
(ofcourse, there's always the possiblity it is my code, but i am not
doing anything extraordinary, it just doesn't want to do more than one
MoveNext() it seems).
 
F

farseer

actually, it's probably can't be .NET2.0 i think, since the project is
using v1.0 of the CompactFramework
 
S

Sergey Bogdanov

It seems that while iterating through _keyMap the function 'ProcessKey'
modifies your hashtable. If you are going to modify _keyMap in
ProcessKey you should make a copy of _keyMap before iterating through it.
 
A

Alex Feinman [MVP]

See Sergey's reply. This error means that the hastable has been modified
between calls to MoveNext
 
F

farseer

what is the best way of removing items from a HashTable? i think i had
similar problems trying to do the same thing using java HashMap
once..but it was Concurrent access exception i used to get. the
solution was to not use an Iterator, but loop thru using a
"for(i=0;..)" loop. but not sure how to do the same thing with C#
Hashtable. can you retrieve elments my an index using a C# Hashtable?
 
A

Alex Feinman [MVP]

You can't loop through hashtable using integer index. What you could do is
to iterate as you did orignally and copy only required elements into a new
hashtable, then drop the old one
 
F

farseer

thanks alex. what i did to solve this was to process the elements in
the foreach loop, but do a HashTable.clear at the end. this worked in
my case since the prupose of the routine in which the errors were being
raised was to "unregister" elements stored in the Hashtable and remove
them. just didn't like having to have duplicate code however...
 

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