IEnumerator + MoveBack()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: IEnumerator + MoveBack()

  1. #1
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Question IEnumerator + MoveBack()

    i need something as simple as IEnumerator that also supports some method like MoveBack().
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: IEnumerator + MoveBack()

    The usual way to use an IEnumerator is through the 'foreach' construct

    Code:
    foreach (MyObject m in list) {
        DoStuff ();
    }
    This approach makes it impossible to 'move back'. If you wanted to be able to invoke MoveBack, you'd have to do something like:

    Code:
    CustomEnumerator e = (CustomEnumerator) list.GetEnumerator ();
    while (e.MoveNext ()) {
        MyObject m = e.Current;
        DoStuff (m);
        if (condition == true)
            e.MoveBack ();
    }
    Where customenumerator is:

    Code:
    interface ICustomEnumerator : IEnumerator
    {
        bool MoveBack ();
    }
    and you also implement a class which inherits this interface and fulfills the contract and you also implement a list which returns an instance of this custom enumerator when you call 'GetEnumerator'.

    It's far far easier to use a standard 'for' loop

    Code:
    for (int i=0; i < list.Count; i++) {
        MyObject m = list[i];
        DoStuff (m);
        if (condition == true)
            i--;
    }
    If you can't index the collection in question, you could always just do:
    List<MyObject> list = new List<MyObject> (existingEnumerable); and so gain indexing on the collection.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: IEnumerator + MoveBack()

    thanks
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: IEnumerator + MoveBack()

    Hi !
    In such cases when I need something like that I'm wrapping my list or dictionarry into a small class which allows to move forward and back step by step

    Code:
    public class ManualExaminationAdresses {
        private Dictionary<string, MirrorAdress> _allAdresses;
        private Dictionary<int, string> _allCodes;
        private int _actPosition = -1;
        private int _itemNo = 0;
    
        public ManualExaminationAdresses() {
           _actPosition = -1;
           _allCodes = new Dictionary<int, string>();
           _allAdresses = new Dictionary<string, MirrorAdress>();
        }
    
        public void Add(MirrorAdress mirror) {
            _allAdresses.Add(mirror.Code, mirror);
            _allCodes.Add(_itemNo, mirror.Code);
            _itemNo++;
        }
        public int Count() {
            return _allAdresses.Count;  
        }
        /// <summary>
        /// Returns the current Item in the collection without moving to another position
        /// </summary>
        /// <returns></returns>
        public MirrorAdress CurrentItem() {
            if (_actPosition >= 0 && _actPosition < _allCodes.Count) {
               if ( _allCodes.ContainsKey(_actPosition)){
                   string xcode = _allCodes[_actPosition];
                   if (_allAdresses.ContainsKey(xcode)){
                        return _allAdresses[xcode];
                   }
                }
            }
            return null;
        }
    
        public MirrorAdress GetNextItem() {
            _actPosition++;
            if (_actPosition >= _allCodes.Count - 1) _actPosition = _allCodes.Count - 1;
            return CurrentItem();
        }
    
        public MirrorAdress GetPreviousItem() {
            _actPosition--;
            if (_actPosition < 0) _actPosition = 0;
            return CurrentItem();
        }
    
        public MirrorAdress GetFirstItem() {
            _actPosition = 0;
            return CurrentItem();
        }
    
        public MirrorAdress GetLastItem() {
            _actPosition = _allCodes.Count - 1;
            return CurrentItem();
        }
    
    
        internal int ActualRecordNum() {
            return _actPosition;
        }
    }
    The above example is a bit more complex then needed for your case as I only copied it from a real world project and it allows to search customer adresses by a customers code and also stepping through them in the order they have been added to the list, because this may be different from the order where they are if I'm calling them sorted by customers code. As you see I'm handling the actualPosition number only and then always calling the CurrentItem method. Because adding Items should not change the CurrentItem position _actPosition ( and vice versa) we have a separate counter for adding Items to the class and for the actual Position value

    Hope that helps
    Last edited by JonnyPoet; February 8th, 2009 at 05:54 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: IEnumerator + MoveBack()

    thanks mutant and jonny; i also can think about N ways of implementation but my question asked about something as simple as IEnumerator with moveback(); i thought that it may exists something like that in .Net Library that i have missed it.

    i have some functiones that return IEnumerator i thought that it will be good if they return something with moveBack() possibility also.
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: IEnumerator + MoveBack()

    I had looked for the same before I did my own
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: IEnumerator + MoveBack()

    ok, now i am sure it does not exists.
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center