-
Nov 1st, 2013, 05:26 AM
#1
Thread Starter
Lively Member
debug error with list
Hi,
Im getting the follow error:
Code:
Collection was modified; enumeration operation may not execute
in this part of my code
Code:
Private Sub btnDeleteArea_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteArea.Click
Try
For Each area In ThisMap.Area
If area.Name = lstvArea.SelectedItems.Item(0).Text Then
ThisMap.Area.Remove(area)
End If
Next
lstvArea.SelectedItems.Item(0).Remove()
Catch ex As Exception
MsgBox("Error - Delete Area: " & ex.Message)
End Try
End Sub
when I remove "ThisMap.Area.Remove(area)" I won't get the error, so I guess thats the problem.
Can anybody help me to solve this error? Or explain me what im doing wrong?
-
Nov 1st, 2013, 05:33 AM
#2
Re: debug error with list
Any collection that you iterate over with a For Each loop are not allowed to be modified during the iteration. This means that while in the For Each loop, you cannot modify its elements
VB.NET MVP 2008 - Present
-
Nov 1st, 2013, 05:38 AM
#3
Thread Starter
Lively Member
Re: debug error with list
Originally Posted by HanneSThEGreaT
Any collection that you iterate over with a For Each loop are not allowed to be modified during the iteration. This means that while in the For Each loop, you cannot modify its elements
In that case, this would be the fix?
Code:
Try
Dim r As New Map.AreaRec
For Each area In ThisMap.Area
If area.Name = lstvArea.SelectedItems.Item(0).Text Then
r = area
End If
Next
ThisMap.Area.Remove(r)
lstvArea.SelectedItems.Item(0).Remove()
Catch ex As Exception
MsgBox("Error - Delete Area: " & ex.Message)
End Try
End Sub
-
Nov 1st, 2013, 05:43 AM
#4
Re: debug error with list
That sounds OK, although only if there can only be one CollectionItem matching the selected item.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 1st, 2013, 05:48 AM
#5
Thread Starter
Lively Member
Re: debug error with list
Originally Posted by opus
That sounds OK, although only if there can only be one CollectionItem matching the selected item.
Oh I see, its not removing the right area if there are 2 area's with the same name. Do you have any idea how to fix this?
I can give each area an initial code (e.g. "area1", "area2", etc.) Or do you have a better idea?
-
Nov 1st, 2013, 06:02 AM
#6
Re: debug error with list
Within your For Next create a "RemoveCollection" containing all items you want to remove. After the For Next remove all items that are in the "RemoveCollection" from your original collection.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 1st, 2013, 08:21 AM
#7
Re: debug error with list
use the REmoveAll method....
http://msdn.microsoft.com/en-us/library/wdka673a.aspx
Code:
Private Sub btnDeleteArea_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteArea.Click
Try
ThisMap.Area.RemoveAll(function(a) a.Name = lstvArea.SelectedItems.Item(0).Text)
lstvArea.SelectedItems.Item(0).Remove()
Catch ex As Exception
MsgBox("Error - Delete Area: " & ex.Message)
End Try
End Sub
It will remove all elements where the function returns true (ie, when the name matches)
-tg
-
Nov 1st, 2013, 08:27 AM
#8
Re: debug error with list
Since nobody mentioned it, the old standby solution is to use a For....Next loop in reverse:
For x = ThisMap.Count -1 To 0 Step -1
As long as you actually have a map count, and can reference the areas by an index, this solution will work. The problem with the For Each, as well as a For Next that is counting forwards, is that the number of items in the collection gets changed. The loop control variable is set only the first time the For line is encountered, so if it is set to 10, and you remove one item, it is still going to iterate to item 10 even though there no longer is an item 10. In other words, it will iterate right off the end of the collection, which would be a serious bug if it were allowed. By iterating in reverse, the problem goes away, since changing the size of the collection doesn't matter when you iterate in reverse as you will always be going to 0. However, iterating in reverse only works with For Next, as there is no reverse iteration option with For Each (though there could have been one written).
My usual boring signature: Nothing
-
Nov 1st, 2013, 06:37 PM
#9
New Member
Re: debug error with list
Like shaggy hiker said For x = ThisMap.Count -1 To 0 Step -1 it should work
but he only gave you one line of code and if you didnt know how to use it, heres the full code
for i as int32 = thismap.area.count -1 to 0 step -1
try
if this.map.area(i).name = lstvarea.selecteditems.item(0).text then
thismap.area.remove(thismap.area.item(i))
end if
next
lstvarea.selecteditems.item(0).remove()
catch ex as exception
msgbox("blah")
end try
I didnt try this right now but it should work :d
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|