Click here to Skip to main content
15,911,035 members
Home / Discussions / C#
   

C#

 
GeneralRe: UserControls at design time(VS designer) Pin
Heath Stewart18-Mar-04 10:22
protectorHeath Stewart18-Mar-04 10:22 
GeneralRe: UserControls at design time(VS designer) Pin
Russell Morris18-Mar-04 9:38
Russell Morris18-Mar-04 9:38 
GeneralRe: UserControls at design time(VS designer) Pin
Festus Redelinghuys18-Mar-04 17:59
Festus Redelinghuys18-Mar-04 17:59 
GeneralHashtable, enumerator, etc Pin
Anonymous18-Mar-04 7:09
Anonymous18-Mar-04 7:09 
GeneralRe: Hashtable, enumerator, etc Pin
Jeremy Kimball18-Mar-04 7:26
Jeremy Kimball18-Mar-04 7:26 
GeneralRe: Hashtable, enumerator, etc Pin
Heath Stewart18-Mar-04 7:31
protectorHeath Stewart18-Mar-04 7:31 
GeneralRe: Hashtable, enumerator, etc Pin
Jeremy Kimball18-Mar-04 7:33
Jeremy Kimball18-Mar-04 7:33 
GeneralRe: Hashtable, enumerator, etc Pin
Heath Stewart18-Mar-04 7:28
protectorHeath Stewart18-Mar-04 7:28 
If you read the documentation for IEnumerator interface and its members, it is documented that an exception is thrown when you modify the underlying collection. This is by design for a reason. If you modify the collection that you're enumerating, there's a good chance that enumerated data was changed which makes your enumerator invalid. Note that the foreach keyword is nothing more than a compiler "alias" for getting the enumerator and looping through it much as you're doing.

One workaround is to clone the Hashtable (see Hashtable.Clone) and enumerate it, removing the elements from the original. Since cloning the hashtable creates a shallow copy, the keys and values themselves will reference the same object (unless they are value types like int, long, DateTime, etc.).

You could also copy the keys to an array (using Hashtable.Keys.CopyTo), enumerate that, and call Hashtable.Remove on the keys you want to remove. There's several other workarounds that would work along these lines as well.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Hashtable, enumerator, etc Pin
Judah Gabriel Himango18-Mar-04 8:51
sponsorJudah Gabriel Himango18-Mar-04 8:51 
GeneralRe: Hashtable, enumerator, etc Pin
Heath Stewart18-Mar-04 8:59
protectorHeath Stewart18-Mar-04 8:59 
GeneralRe: Hashtable, enumerator, etc Pin
Judah Gabriel Himango18-Mar-04 9:23
sponsorJudah Gabriel Himango18-Mar-04 9:23 
GeneralRe: Hashtable, enumerator, etc Pin
Werdna18-Mar-04 11:06
Werdna18-Mar-04 11:06 
GeneralPrint .Net Bitmap with Win32 API Pin
Shaun Becker18-Mar-04 6:42
Shaun Becker18-Mar-04 6:42 
GeneralRe: Print .Net Bitmap with Win32 API Pin
Heath Stewart18-Mar-04 9:08
protectorHeath Stewart18-Mar-04 9:08 
GeneralRe: Print .Net Bitmap with Win32 API Pin
Shaun Becker18-Mar-04 9:32
Shaun Becker18-Mar-04 9:32 
GeneralRe: Print .Net Bitmap with Win32 API Pin
Heath Stewart18-Mar-04 9:40
protectorHeath Stewart18-Mar-04 9:40 
GeneralRe: Print .Net Bitmap with Win32 API Pin
Shaun Becker19-Mar-04 10:28
Shaun Becker19-Mar-04 10:28 
GeneralRe: Print .Net Bitmap with Win32 API Pin
Anonymous18-Mar-04 11:04
Anonymous18-Mar-04 11:04 
GeneralRe: Print .Net Bitmap with Win32 API Pin
Shaun Becker19-Mar-04 10:29
Shaun Becker19-Mar-04 10:29 
GeneralGoogle deskbar Pin
profoundwhispers18-Mar-04 2:40
profoundwhispers18-Mar-04 2:40 
GeneralRe: Google deskbar Pin
Dave Kreskowiak18-Mar-04 3:18
mveDave Kreskowiak18-Mar-04 3:18 
Generalchar conversions. Help! Pin
jbshodan18-Mar-04 2:23
jbshodan18-Mar-04 2:23 
GeneralRe: char conversions. Help! Pin
John Fisher18-Mar-04 3:45
John Fisher18-Mar-04 3:45 
GeneralRe: char conversions. Help! Pin
jbshodan18-Mar-04 12:44
jbshodan18-Mar-04 12:44 
GeneralWebBrowser help Pin
Simon Wren18-Mar-04 1:45
professionalSimon Wren18-Mar-04 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.