Re: Question about Iteration and forEach



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
.



Relevant Pages

  • Re: enum type int or unsigned int?
    ... that have type int and may appear wherever such are permitted. ... values of all the members of the enumeration. ... "enum" types are declared via the following syntax: ... so the loop will continue to run. ...
    (comp.lang.c)
  • Re: Threading Issue with ArrayList
    ... Thanks for the quick response and the amount of effort you put into it. ... Code within my enumeration loop is sending a message object via an event to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: enum type int or unsigned int?
    ... that have type int and may appear wherever such are permitted. ... values of all the members of the enumeration. ... "enum" types are declared via the following syntax: ... so the loop will continue to run. ...
    (comp.lang.c)
  • Enumerators that need to be changed at runtime, avoiding exception
    ... enumeration operation may not execute. ... The problem is i have a timer, that checks at an interval for any changes to ... Another issue i have is a render loop that runs in a for each liek this: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Class Pointer
    ... In each class is an enumeration for that item which contains the properties ... Public Enum Properties ... 'Loop through enumeration for class ... and the display could would continue to work perfectly fine. ...
    (microsoft.public.dotnet.languages.vb)