Results 1 to 8 of 8

Thread: Deleting Items In A List In A For...Next Block

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Deleting Items In A List In A For...Next Block

    Hi!
    I am trying to delete items in a list that have a location outside an 80x80 rectangle. Either if I create a temporary list or not, I get this error:
    Collection was modified; enumeration operation may not execute.
    It looks like the tempList.Count = 0, but I'm not sure why. (The DeleteExtra() sub is used in a timer.tick event.)

    Code:
        Private Sub DeleteExtra()
    
            bmpList(0).LocationX = 2000 'Just to test!
    
            Dim TempList As List(Of bmp) = bmpList
            For Each bmp In TempList
    
                If bmp.LocationX < 0 Or bmp.LocationY < 0 Or bmp.LocationX > 80 Or bmp.LocationY > 80 Then
                    bmpList.Remove(bmp)
                End If
    
            Next
    
        End Sub
    Thanks in advance. ~NinjaNic

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Deleting Items In A List In A For...Next Block

    You can't use a For Each loop for that. Instead, use a For Next loop and iterate backwards:
    Code:
    For x = tempList.Count-1 To 0 Step -1
     'etc
    Next
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Deleting Items In A List In A For...Next Block

    Thank you, your code works, but can you explain why? (I don't understand why mine doesn't work and yours does.)

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Deleting Items In A List In A For...Next Block

    Because if you start removing items from the list starting at the beginning using For Each, when you delete the first item the index of all the other items behind that item in the list essentially change (i.e. the items shift down), so your iterator value is no longer correct (i.e. the items you are trying to index through are no longer where they were when you started the loop).
    You have to loop from the back of the list, so you don't try to access a part of the list that has moved because of the deletion of an element of the list (none of the items in front of the item you removed have changed position).

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Deleting Items In A List In A For...Next Block

    The problem is that you think you've created a temporary list, but you haven't. You've created a second way of referring to the same list. TempList is the same list as bmpList, so iterating over TempList and removing from bmpList still results in TempList being modified.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Deleting Items In A List In A For...Next Block

    Thank you. I did not realize TempList would be changed if it was outside the For...Each block.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Deleting Items In A List In A For...Next Block

    The variable TempList isn't the list itself, it is a variable that just holds the address of the list in memory (a reference, to be technically correct, but still essentially just an address). When you assign bmpList to it, you just copy the contents of the variable in bmpList into the variable TempList. Since the contents is just that address, that is all you are copying. This is consistent. The = operator copies the contents of the right side into the left side. If the right side is a value type, then the variable holds the actual value, whereas if the right side is a reference type (like all classes, including lists), then the variable holds a reference to the actual value. In both cases, though, the contents of the variable is all that is copied.
    My usual boring signature: Nothing

  8. #8
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Deleting Items In A List In A For...Next Block

    You can also use your existing code with:

    vb Code:
    1. For Each bmp In TempList.ToArray()

    Performance may be a little slower, you generally will not notice the difference ... but I use this all of the time unless:
    • This itself is looped or run regularly (massive processing over time)
    • Likely to contain a heap of items
    • Contains structures
    • If whatever I am doing is time critical - eg in a game


    Kris

Tags for this Thread

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