-1

First of all, at the beginning of my code I declare an ArrayList public ArrayList ArrivalsInApp = new ArrayList();. Then, much much later, I am downloading some data from XML file. For each XmlNode called "flight" I am creating a Panel.

While creating the Panel, I add its name into the ArrivalsInApp ArrayList. The generating of the panel is in a method. The method has several foreach cycles and IF conditions. Technically, all it does it decides if Panel should be created (if its in XML and not in app), updated (in in both) or deleted (in app but not in XML).

In the method, I get names of all flights in XML, save into ArrayList. Now using foreach, I check if all flights in the application (so in the ArrivalsInApp ArrayList) are ALSO in the XML. I do this using this code:

foreach (string y in ArrivalsInApp)
{
if (XmlArrivals.Contains(y) == false)
{
*code*
}
}

The "deciding" method is called on form1_load, and every 30 second using a Timer. When the timer reaches specified value, I reload the XML and then call the method. But, I get an InvalidOperationException with comment that "Collection was modified; enumeration operation may not execute." at line foreach (string y in ArrivalsInApp). I am totally desperate, I have no Idea what causes this.

What happens if I run the .exe file (if I just debug it, after the timer reaches the value, the program shuts down and shows this error) is, that the Panel is deleted regardless if it should be updated or deleted, and the unhandled exception error pops up. Sorry if the explanation is a bit confused. If anyone would like me to post the whole source file (maybe rather the whole project) just let me know. However, since it has now about 1800 lines of code, it would need quite a lot of explanation.

Marek Buchtela
  • 973
  • 3
  • 19
  • 42
  • ""Collection was modified; enumeration operation may not execute." " - either take a copy of the list before iterating, or iterate backwards, checking the last member of the collection... – Mitch Wheat Dec 30 '12 at 01:12
  • @MitchWheat I dont understand that, you mean like before the foreach create a new ArrayList with the same content, and in the foreach use this ArrayList? – Marek Buchtela Dec 30 '12 at 01:30
  • ArrayList? What version of .NET are you using? – Colin Mackay Dec 30 '12 at 01:32
  • .NET 4.0, and yes, I heard of List, but I am not sure even what it is, not speaking of how to use it, if you are asking because of this. – Marek Buchtela Dec 30 '12 at 01:35

1 Answers1

1

I get an InvalidOperationException with comment that "Collection was modified; enumeration operation may not execute."

The explanation is right in the error message. The collection (in this case 'ArrivalsInApp') was modified during a loop. Either you added something to it or removed something from it.

If you want to loop over a collection and modify it at the same time then you want to take a copy of the collection first and loop over the copy while you modify the original.

The easiest way to make a copy is to use Linq and ToArray() or ToList()

At the top of your file, if it does not already exist there:

using System.Linq;

Then in the foreach statment you can do something like this:

foreach(string y in ArrivalsInApp.ToArray())
{
  // code to do stuff.
}
Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
  • I just realised you are using ArrayList - I've no idea how LINQ will react to that as it was pre-generics and everything is an object. You may have to do something like ArrivalsInApp.Cast().ToArray() in order to allow it to be processed properly. – Colin Mackay Dec 30 '12 at 01:34