Search code examples
vb.netfor-loopeachinvalidoperationexception

VB.NET For-Each Loop Error


Came across an issue when writing some code to load my app's settings-- the following code produces an InvalidOperationException error:

For Each c As MapInfo In CurrentMaps
    AddMap(c, False)
Next

The debugger highlights the Next part, and from the MSDN page on the error, it looks like this is because the collection (CurrentMaps) has somehow changed from when it last bumped the enumerator (to process the For Each).

Not sure why that would happen, though, as AddMap takes the MapInfo ByVal and doesn't touch the input (so the collection is not changed).

I changed it to this, which works perfectly fine:

For i As Integer = 0 To CurrentMaps.Count - 1
    AddMap(CurrentMaps(i), False)
Next

Those should functionally work the same, right? Any idea why the For Each one doesn't work?

Edit:

Ah figured it out-- the problem is that I use CurrentMaps as a temporary variable (collection keeping track of current maps), and AddMap is usually used within the app to add a new map (which means both updating UI and adding it to CurrentMaps).

Here (when loading app), I'm just using AddMap to update the UI, but the sub call to add the item to CurrentMaps was still there, so it was indeed modifying CurrentMaps.

Thanks for the input, all!


Solution

  • No, they shouldn't work the same. You can't modify a collection while you're iterating over it, basically - and it looks like that's what's happening.

    From the List<T>.GetEnumerator() docs:

    An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

    You haven't shown what your AddMap method does, but I suspect it adds a map to CurrentMaps, thus invalidating the iterator.

    If you could give us more information about exactly what AddMap does, we can help you more. What does the False mean?