Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Enumerator question

Enumerator question

Scheduled Pinned Locked Moved C#
helpcsharpjavaquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jbricker77
    wrote on last edited by
    #1

    Long time Java programmer running into a C# issue I can not figure out. I have a class that contains a List. Other classes need iterate through the list. What I have below is not working. I get an excpetion at runtime about Current being outofRange. Should I just return an Enumerator from the List or is there a way to do it like this that I'm missing. thanks for the help. Class Foo { List list; list = fill_it_up(); // fills the list public class FooIterator : IEnumerator { protected int lastVisited; public FooIterator () { lastVisited = -1; } #region IEnumerator Members public object Current { get { return list[lastVisited].thisBar; } } public bool MoveNext() { return ++lastVisited < list.Count - 1; } public void Reset() { throw new Exception("The method or operation is not implemented."); } #endregion } } }

    J R J 4 Replies Last reply
    0
    • J jbricker77

      Long time Java programmer running into a C# issue I can not figure out. I have a class that contains a List. Other classes need iterate through the list. What I have below is not working. I get an excpetion at runtime about Current being outofRange. Should I just return an Enumerator from the List or is there a way to do it like this that I'm missing. thanks for the help. Class Foo { List list; list = fill_it_up(); // fills the list public class FooIterator : IEnumerator { protected int lastVisited; public FooIterator () { lastVisited = -1; } #region IEnumerator Members public object Current { get { return list[lastVisited].thisBar; } } public bool MoveNext() { return ++lastVisited < list.Count - 1; } public void Reset() { throw new Exception("The method or operation is not implemented."); } #endregion } } }

      J Offline
      J Offline
      Josh Smith
      wrote on last edited by
      #2

      You might as well just return the List's enumerator (accessed via GetEnumerator()) if you are not doing any special/custom processing in your enumerator. Josh

      J 1 Reply Last reply
      0
      • J jbricker77

        Long time Java programmer running into a C# issue I can not figure out. I have a class that contains a List. Other classes need iterate through the list. What I have below is not working. I get an excpetion at runtime about Current being outofRange. Should I just return an Enumerator from the List or is there a way to do it like this that I'm missing. thanks for the help. Class Foo { List list; list = fill_it_up(); // fills the list public class FooIterator : IEnumerator { protected int lastVisited; public FooIterator () { lastVisited = -1; } #region IEnumerator Members public object Current { get { return list[lastVisited].thisBar; } } public bool MoveNext() { return ++lastVisited < list.Count - 1; } public void Reset() { throw new Exception("The method or operation is not implemented."); } #endregion } } }

        R Offline
        R Offline
        Robin Panther
        wrote on last edited by
        #3

        looks like you get an error when lastVisited is -1. Probably you have to do something like this: public object Current { get { if (lastVisited == -1) return null; return list[lastVisited].thisBar; } } ____________________________________________ Robin Panther http://www.robinland.com

        J 1 Reply Last reply
        0
        • J Josh Smith

          You might as well just return the List's enumerator (accessed via GetEnumerator()) if you are not doing any special/custom processing in your enumerator. Josh

          J Offline
          J Offline
          jbricker77
          wrote on last edited by
          #4

          Just tried this and got an InvalidOperationException saying that the enumeration has either not started or finished.

          1 Reply Last reply
          0
          • R Robin Panther

            looks like you get an error when lastVisited is -1. Probably you have to do something like this: public object Current { get { if (lastVisited == -1) return null; return list[lastVisited].thisBar; } } ____________________________________________ Robin Panther http://www.robinland.com

            J Offline
            J Offline
            jbricker77
            wrote on last edited by
            #5

            Tried this but did not work. See following post for why and a new question.

            1 Reply Last reply
            0
            • J jbricker77

              Long time Java programmer running into a C# issue I can not figure out. I have a class that contains a List. Other classes need iterate through the list. What I have below is not working. I get an excpetion at runtime about Current being outofRange. Should I just return an Enumerator from the List or is there a way to do it like this that I'm missing. thanks for the help. Class Foo { List list; list = fill_it_up(); // fills the list public class FooIterator : IEnumerator { protected int lastVisited; public FooIterator () { lastVisited = -1; } #region IEnumerator Members public object Current { get { return list[lastVisited].thisBar; } } public bool MoveNext() { return ++lastVisited < list.Count - 1; } public void Reset() { throw new Exception("The method or operation is not implemented."); } #endregion } } }

              J Offline
              J Offline
              jbricker77
              wrote on last edited by
              #6

              I think I figured out why I'm having the problem but I still can not figure a way around it yet. FooIterator in an inner class so it can only acess list if list is a static attribute. So I made changes like so: public object Current { get { return Foo.list[lastVisited].thisBar; } But when checking the degugger, when I load Foo.list is is fine. When I access it via the Iterator inner class it is empty. I tried to get rid of the inner class and return the static list and that did not work. What I need to do is immitate the Java Iterator but this seems impossible because of the static attribute contrate on inner classes.

              1 Reply Last reply
              0
              • J jbricker77

                Long time Java programmer running into a C# issue I can not figure out. I have a class that contains a List. Other classes need iterate through the list. What I have below is not working. I get an excpetion at runtime about Current being outofRange. Should I just return an Enumerator from the List or is there a way to do it like this that I'm missing. thanks for the help. Class Foo { List list; list = fill_it_up(); // fills the list public class FooIterator : IEnumerator { protected int lastVisited; public FooIterator () { lastVisited = -1; } #region IEnumerator Members public object Current { get { return list[lastVisited].thisBar; } } public bool MoveNext() { return ++lastVisited < list.Count - 1; } public void Reset() { throw new Exception("The method or operation is not implemented."); } #endregion } } }

                J Offline
                J Offline
                jbricker77
                wrote on last edited by
                #7

                The problem is more complex than I wrote here but I will show my solution to this problem. Here is what we had. Class Foo { protected static List<T> list; list = fill_it_up(); // fills the list public class FooIterator : IEnumerator { protected int lastVisited; public FooIterator () { lastVisited = -1; _more enumeration code...._ } The FooIterator was neede by other class to enumerate through the List<>. The inner class was not working because the List has to be static. Class Bar is an object that contains 2 other objects. Here is the change: Class Foo { protected List<Bar> list; list = fill_it_up(); // fills the list public class FooIterator { private List<Bar> list; public FooIterator (List<Bar> anotherList) { list = anotherList; } public IEnumerator<FeFiFoe> GetEnumerator() { foreach (Bar b in list) { // returning the property FeFiFoe object from the Bar object yield return b.thisFeFiFoe; } } } Hope this helps someone else.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups