Re: Question about Iteration and forEach
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 11 Jun 2007 20:54:15 -0700
On Mon, 11 Jun 2007 19:50:40 -0700, news.microsoft.com <billgower@xxxxxxxxxxx> wrote:
I am looping through an iteration and I would like to test the next item but
if its not the one that I want how do I put it back so that when my foreach
continues it is in the next iteration?
You can't. The "foreach" statement strictly enumerates one by one through the collection.
If you want the ability to adjust your position within the enumeration, you can usually just use a normal "for" loop with an index to access individual items within the collection. Then you can adjust the index as needed.
I will note that if you are truly enumerating a list, having a need to look at a specific item and then as a result of that inspection reverse course and go back to the previous item is generally a bad sign. It either means that you're not really enumerating the items in the list, and so enumeration semantics aren't appropriate, or you are enumerating the list in a manner indicative of poor design.
Hopefully you're in the former case, and you really don't want a true enumeration, but you should at least consider the possibility of the latter case. :) For more specific advice, you'd have to post the code for the actual loop you're talking about.
Pete
.
- Follow-Ups:
- Re: Question about Iteration and forEach
- From: Jeremy Shovan
- Re: Question about Iteration and forEach
- From: Bruce Wood
- Re: Question about Iteration and forEach
- References:
- Question about Iteration and forEach
- From: news.microsoft.com
- Question about Iteration and forEach
- Prev by Date: Re: Question about Iteration and forEach
- Next by Date: Re: Question about Iteration and forEach
- Previous by thread: Re: Question about Iteration and forEach
- Next by thread: Re: Question about Iteration and forEach
- Index(es):
Relevant Pages
|