473,545 Members | 529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cur ren
bool IEnumerator.Mov eNext() an
void IEnumerator.Res et()

The help to IEnumerator.Mov eNext 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 7098
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.co m

"Hans" <an*******@disc ussions.microso ft.com> wrote in message
news:9C******** *************** ***********@mic rosoft.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.Cur rent
bool IEnumerator.Mov eNext() and
void IEnumerator.Res et().

The help to IEnumerator.Mov eNext 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.co m

"Hans" <an*******@disc ussions.microso ft.com> wrote in message
news:9C******** *************** ***********@mic rosoft.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.Cur rent
bool IEnumerator.Mov eNext() and
void IEnumerator.Res et().

The help to IEnumerator.Mov eNext 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.Get Enumerator() 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
4102
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 Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:"...
0
311
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 bool IEnumerator.MoveNext() an void IEnumerator.Reset() The help to IEnumerator.MoveNext describes that "after the end of the collection is passed,...
104
7076
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 foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in...
3
2433
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 something like: // Iterate whole collection foreach (object in collection) // Iterate over all objects with the "open" flag set
9
2800
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 and for to enumerate the contents of the array. Also as well as explaining the differences could you explain why the foreach messagebox isn't working...
11
1852
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 MyClass<Tgoes here ... // ...
4
22203
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
1426
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 set of all possible combinations of first and last name.
8
273
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 other words that could fill in the blank though. "Or am I _______ as usual?"
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.