Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.NET For-Each Loop Error
    text
    copied!<p>Came across an issue when writing some code to load my app's settings-- the following code produces an <em>InvalidOperationException</em> error:</p> <pre><code>For Each c As MapInfo In CurrentMaps AddMap(c, False) Next </code></pre> <p>The debugger highlights the <em>Next</em> part, and from the <a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.movenext.aspx" rel="nofollow">MSDN page</a> on the error, it looks like this is because the collection (<em>CurrentMaps</em>) has somehow changed from when it last bumped the enumerator (to process the <em>For Each</em>).</p> <p>Not sure why that would happen, though, as <em>AddMap</em> takes the <em>MapInfo</em> ByVal and doesn't touch the input (so the collection is not changed).</p> <p>I changed it to this, which works perfectly fine:</p> <pre><code>For i As Integer = 0 To CurrentMaps.Count - 1 AddMap(CurrentMaps(i), False) Next </code></pre> <p>Those should functionally work the same, right? Any idea why the <em>For Each</em> one doesn't work?</p> <p><strong>Edit:</strong></p> <p>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).</p> <p>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.</p> <p>Thanks for the input, all!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload