|
-
Jan 8th, 2015, 10:49 PM
#1
Thread Starter
Addicted Member
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
-
Jan 8th, 2015, 11:28 PM
#2
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
 
-
Jan 8th, 2015, 11:51 PM
#3
Thread Starter
Addicted Member
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.)
-
Jan 9th, 2015, 03:30 AM
#4
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).
-
Jan 9th, 2015, 04:14 AM
#5
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.
-
Jan 9th, 2015, 01:23 PM
#6
Thread Starter
Addicted Member
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.
-
Jan 9th, 2015, 01:50 PM
#7
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
 
-
Jan 10th, 2015, 07:29 PM
#8
Re: Deleting Items In A List In A For...Next Block
You can also use your existing code with:
vb Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|