Crazy error
-
Hi all, I keep getting the following error message:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args)
does anybody know what this means or where it could be originating from? BlueBroadcaster -
Hi all, I keep getting the following error message:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args)
does anybody know what this means or where it could be originating from? BlueBroadcasterActually, it's not crazy. It's probably something like :-
foreach (Object obj in myCollection)
{
// some codemyCollection.Add(anotherobj); // some more code
}
and it's telling you that you can't add / remove / clear or whatever in the collection you're iterating over. If you only ever want to add new items, you can use a "for (int loop=0; loop<myCollection.Count; ++loop)", although that will iterate over the new items as well. Otherwise, create "Add" and "Remove" lists which can be processed after the main loop to add and remove as needed.
There are three kinds of people in the world - those who can count and those who can't...
-
Hi all, I keep getting the following error message:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args)
does anybody know what this means or where it could be originating from? BlueBroadcaster -
Hi all, I keep getting the following error message:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args)
does anybody know what this means or where it could be originating from? BlueBroadcasterBasically, you cannot alter a collection of items as you ForEach over it. You can make changes to the items inside the collection, but you cannot add or delete items to the collection itself.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Basically, you cannot alter a collection of items as you ForEach over it. You can make changes to the items inside the collection, but you cannot add or delete items to the collection itself.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Dave Kreskowiak wrote:
you cannot alter a collection of items as you ForEach over it
why does this happen?
-
Dave Kreskowiak wrote:
you cannot alter a collection of items as you ForEach over it
why does this happen?
I guess because the framework is keeping track of where you are in the collection, which assumes the collection will not change. If it does, what should foreach do ? you may have removed an item, how do you know if it has been iterated over ? Would you want it to be ? Easier just to say 'don't change this while I am working on it'. I suspect there's a more concrete reason than that, if you look into how it's implimented, but it seems obvious to me that it would create some situations where you could not predict the behaviour of foreach if you could alter the collection.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )