Instead of using a For Each (foreach in C#) loop, iterate over the
collection using an index and *make sure* to start from the bottom up.
For i As Integer = myCollection.Count - 1 To 0
If something Then
myCollection.Remove(i)
End If
Next i
for(int i = myCollection.Count - 1; i>=0; i--)
{
If(something)
{
myCollection.RemoveAt(i);
}
}
I'm sure you see why I stressed on starting from the bottom :)
hope that helps..
Imran.
"STech" <ST***@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
How can I iterate a collection and conditionally remove items from the
collection using either C# or VB.NET?
When I try to remove an item, I get the error message that I am not
allowed
to remove an item.
Thanks