Results 1 to 9 of 9

Thread: debug error with list

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    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?

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: debug error with list

    Quote Originally Posted by HanneSThEGreaT View Post
    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

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: debug error with list

    Quote Originally Posted by opus View Post
    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?

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  9. #9
    New Member
    Join Date
    Feb 2013
    Posts
    13

    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
  •  



Click Here to Expand Forum to Full Width