foreach and IEnumerator

G

Guest

I implemented the IEnumerator interface in some classes to enable the use of the foreach statement. As you probalbly know, the interface asks for the implementation of

object IEnumerator.Curren
bool IEnumerator.MoveNext() an
void IEnumerator.Reset()

The help to IEnumerator.MoveNext describes that "after the end of the collection is passed, subsequent calls to MoverNect return false until reset is called"

When, however, I have two foreach() loops after another in one method, the Reset() interface is gets called. Thus the second loop never returns an element!!

Can anybody tell me why? Is this the 'normal' implementation

Thanx for any help
Han

in code

foreach(CEntry Entry in MyObject

// do stuf

...
.... // Other operations, NOT MODIFYING MyObject !!
...
foreach(CEntry Entry in MyObject) // MyObject NEVER returns an objec

// do stuf
 
N

Nicholas Paldino [.NET/C# MVP]

Hans,

I think that you meant to say that it doesn't get called.

When you return the enumerator from the implementation of IEnumerable,
are you passing back the same enumerator? If you are, then you have to
manually call the Reset method when the same instance is returned.
Otherwise, if you use a different implementation of IEnumerator each time,
you should not have a problem.

If you could provide a code sample, that would help (specifically, the
implementation of the enumerator and the IEnumerable interface).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hans said:
I implemented the IEnumerator interface in some classes to enable the use
of the foreach statement. As you probalbly know, the interface asks for the
implementation of
object IEnumerator.Current
bool IEnumerator.MoveNext() and
void IEnumerator.Reset().

The help to IEnumerator.MoveNext describes that "after the end of the
collection is passed, subsequent calls to MoverNect return false until reset
is called".
When, however, I have two foreach() loops after another in one method, the
Reset() interface is gets called. Thus the second loop never returns an
element!!!
 
K

Kevin P. Fleming

Hans said:
I figured it out myself: I implemented the IEnumerator directly in my class, which limits the number of concurrend enumeration processes to ONE.
Now I have a seperate enumerator class that implements IEnumerator. The class is instantiated by calling the IEnumerator.GetEnumerator() interface, implemented in my class.

It's ok to implement the enumerator directly in your class, if you know
that you'll never need to have two open enumerators for the same object.
The only thing you need to do to fix your original problem is to
forcibly call your Reset() method from within GetEnumerator() (or just
do the same steps your Reset() method does).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top