Re: Removing an object from an Enumerated Collection

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



gee-dub wrote:
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!

The For Each uses an enumerator, so unless the enumerator is crap, there is no problem in using a For Each.

As Stephany suggested, to remove the items you just put them in a new list, and remove them in a separate loop.

--
Göran Andersson
_____
http://www.guffa.com
.



Relevant Pages

  • Re: Interface features implemented without using Interface
    ... public object Clone() {... ... Enumerator (IEnumerable and IEnumerator). ... complain 'cause the class "does not implement the GetEnumerator() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: recursive GetEnumerator()
    ... var enumerator = self.GetEnumerator; ... public IEnumeratorGetEnumerator() ... thats the nature of yield I beleive, however this is the first time ... the code inside the foreach loop with the value in expresion ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Check for correct multithreading
    ... //which implements IEnumerableand uses "yield return" inside its GetEnumerator method ... they still have values at the time the enumerator is constructed. ... I would be curious to see a code example where it makes sense for you to have some side-effect that happens as part of the creation of the IEnumerator instance. ... In the code below, with two enumerable instances, and two enumerator instances extracted from each, I have each enumerator take a name from its parent enumerable to be used as a prefix when it emits each of its values. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: VS2005/c#2.0: enum and user control problems
    ... >> an IEnumerator. ... > Simply add a GetEnumerator() method that returns this: ... just returning IEnumerable from the generator works, ... doing it this way as opposed to constructing a new enumerator ...
    (microsoft.public.dotnet.languages.csharp)