Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I wrote up a very simple bitmap manager using a hashtable, but when i use the remove function it eventually throws the error (at a seemingly random point) "An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Aditional information: Collection was modified; enumeration operation may not execute."

Well lets see here keywords MODIFIED, MAY.

MODIFIED of course it was modified, i just removed almost all of its members your dang right modified.

MAY i dont care if it may not continue it dang well should and will continue however.

If anyone can give me a hand that would be great

public void Flush(string KeyContains)
{
//hbitmaps is the hashtable in question, all keys are strings all values bitmaps.
    foreach(object t in hbitmaps.Keys)
          if(((string)t).IndexOf(KeyContains) != -1)
                hbitmaps.Remove(t);
}

  • *Experts*
Posted
You are trying to modify (delete in this case) the collection while it is being enumerated, which is not possible because it will mess up the enumeration process. Those keywords you just mentioned explain very well why there was an exception thrown :).
Posted
You are trying to modify (delete in this case) the collection while it is being enumerated' date=' which is not possible because it will mess up the enumeration process. Those keywords you just mentioned explain very well why there was an exception thrown :).[/quote']

Fair enough, i was just getting frustrated (and didnt understand what it ment be enumerated) Correct me if im wrong (and it will when i try it) but the problem is that its changing the hashtable in a foreach loop, so if i allready cache all of the keys that i need (dont use a foreach loop) it will work? (and thanks for your help.)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...