CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2004
    Posts
    61

    Question foreach backward?

    Does anyone know if there is a counterpart to the foreach statement?

    For example:
    foreach(BasePage page in middlePages) allows one to move sequentially forward through all pages in middlePages from the first page to the last.

    However, I'd like also to be able to move backward through all the pages in middlePages, but from the last to the first page.

    Is there a statement in C# that allows this or is there some way to fenagle foreach to do this for me?

    Thanks

    -Mike

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: foreach backward?

    foreach doesn't support this feature. You can
    - simply use for(int i = array.Lenght - 1; i >= 0; i--)
    - reverse your array before you pass it to foreach statement
    - create your own helper class. Something like
    Code:
    namespace WindowsApplication1
    {
    	public class Reversor : IEnumerable, IEnumerator
    	{
    		private IList collection = null;
    		private int position;
    
    		public Reversor(IList collection)
    		{
    			this.collection = collection;
    		}
    
    
    
    		public void Reset()
    		{
    			this.position = this.collection.Count;
    		}
    
    		public object Current
    		{
    			get
    			{
    				return this.collection[this.position];
    			}
    		}
    
    		public bool MoveNext()
    		{
    			this.position--;
    			return this.position > 0;
    		}
    
    		public IEnumerator GetEnumerator()
    		{
    			return this;
    		}
    	}
    }
    
    ...
    
    int[] arry = new int[]{1,2,3,4};
    			foreach (int i in new Reversor(arry))
    			{
    				System.Console.WriteLine(i);
    			}
    Sorry if something is wrong, but I cannot check it, my VS is in terrible condition
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Mar 2006
    Location
    You can guess, but it'll be wrong
    Posts
    12

    Re: foreach backward?

    From a personal perspective, foreach is to process a non-empty set's elements, and the cardinal order for output is not naturally determined beforehand in users'mind, you can look at what is inside foreach's brackets and imagine your output order is from behind or backward. It is a little odd. A more natural way of thinking about it is as random processing which quite fits our mind but would complicate the procedure.

    I like your reverse order code snip anyway. That's a very nice idea...
    Emiene Vous

  4. #4
    Join Date
    Apr 2005
    Posts
    576

    Re: foreach backward?

    Quote Originally Posted by Emiene
    From a personal perspective, foreach is to process a non-empty set's elements, and the cardinal order for output is not naturally determined beforehand in users'mind, you can look at what is inside foreach's brackets and imagine your output order is from behind or backward. It is a little odd. A more natural way of thinking about it is as random processing which quite fits our mind but would complicate the procedure.

    I like your reverse order code snip anyway. That's a very nice idea...
    Well, foreach can be done on any type of collection, ordered, sorted, or non-ordered.

    In C# 2.0 multiple iterators are supported, for instance look at this example: http://msdn2.microsoft.com/en-US/library/ee5kxzk0.aspx

    In C# 1.1 a class can have only one iterator that can be used by the for each statement, somethin like the reversor can be used (I too liked it). An alternative that I have used for some collections is to create inner classes with the sole purpose of providing alternate iterators. A 3rd solution is to implement your own iterators and use them the classic way in a while loop. In most cases though I use boudino's first solution.

  5. #5
    Join Date
    Mar 2006
    Location
    You can guess, but it'll be wrong
    Posts
    12

    Re: foreach backward?

    Quote Originally Posted by klintan
    Well, foreach can be done on any type of collection, ordered, sorted, or non-ordered.

    In C# 2.0 multiple iterators are supported, for instance look at this example: http://msdn2.microsoft.com/en-US/library/ee5kxzk0.aspx

    In C# 1.1 a class can have only one iterator that can be used by the for each statement, somethin like the reversor can be used (I too liked it). An alternative that I have used for some collections is to create inner classes with the sole purpose of providing alternate iterators. A 3rd solution is to implement your own iterators and use them the classic way in a while loop. In most cases though I use boudino's first solution.
    Yes, I am completely agree that they can be accomplished with the use of generic code and have no denying for the fact that they are user-defined functions to fit their personal purpose in this way or that.
    Emiene Vous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured