473,378 Members | 1,377 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,378 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 7080
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.