473,411 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

foreach and IEnumerator

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
Nov 16 '05 #1
3 7082
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]
- mv*@spam.guard.caspershouse.com

"Hans" <an*******@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
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!!!
Can anybody tell me why? Is this the 'normal' implementation?

Thanx for any help,
Hans
in code:

foreach(CEntry Entry in MyObject)
{
// do stuff
}
...
... // Other operations, NOT MODIFYING MyObject !!!
...
foreach(CEntry Entry in MyObject) // MyObject NEVER returns an object
{
// do stuff
}

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Hans" <an*******@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
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!!!
Can anybody tell me why? Is this the 'normal' implementation?

Thanx for any help,
Hans
in code:

foreach(CEntry Entry in MyObject)
{
// do stuff
}
...
... // Other operations, NOT MODIFYING MyObject !!!
...
foreach(CEntry Entry in MyObject) // MyObject NEVER returns an object
{
// do stuff
}

Nov 16 '05 #3
Hans wrote:
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).
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
0
by: Hans | last post by:
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...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: Steve Barnett | last post by:
I have a collection which I need to navigate through in several different ways. Is there any way to structure my class such that I can have several different iterators? Ideally, I'd like to do...
9
by: garyusenet | last post by:
I'm a bit confused about the differences of these two commands (what is the right word for commands here?) when used to enumerate the contents of an array. The below example uses both foreach...
11
by: Bit Byte | last post by:
I am writing a generic container class which I want to support the 'foreach' statement. My class looks something like this: class MyClass<T: CollectionBase { //Implementation for...
4
by: bg_ie | last post by:
Hi, What is wrong with the following - short arrfile = new short; foreach (short s in arrfile) { s = 10; }
8
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
An example of a slightly more complicated class might be to have a collection of first names, and a collection of last names in your class. The IEnumerable functions then could return the complete...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.