The question has to be - why?
Is the world going to stop turning if iterate over the collection with For
Each ... Next, or even, for that matter, For ... Next?
I can't imagine any valid reason for the API 'requiring' you to use an
Enumerator.
I can, however, imagine an 'in-house' policy that requires the use of a
Enumerator, but if that is the case then say so, rather than making some
vague statement that doesn't make any sense.
If you remove an item from a collection while you are iterating over it,
then, the rule of thumb is that the collection becomes changed and iterator
is rendered invalid.
This is regardless of whether you are using an Enumerator, a For Each ...
Next iterator or a For ... Next iterator.
If you use a 'backwards' For ... Next iterator then you can remove items
quite happily:
For _i = lobjs.Count - 1 To 0 Step -1
If condition Then lobjs.RemoveAt(_i)
Next
In your case, you could use another collection and do a 2 stage update:
Dim _remove = New List(Of Object)
With lobjs.GetEnumerator
.MoveNext()
If condition Then _remove.Add(.Current)
End With
For Each _obj In _remove
lobjs.Remove(_obj)
Next
"gee-dub" <up*****@shaw.cawrote in message
news:21**********************************@l42g2000 hsc.googlegroups.com...
Hi,
I need to remove an object from a collection that has been enumerated
with GetEnumerator similar to this...
With lobjs.GetEnumerator
.MoveNext()
if [some_condition] Then
lobjs.remove(.Current)
end if
end with
I realise you can't use the Remove with the GetEnumerator and bombs on
the next MoveNext. is there another way to do this? The API I'm
using requires GetEnumerator (as opposed to For...Each).
Cheers!