Results 1 to 5 of 5

Thread: implementing GetEnumerator() in a class

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    implementing GetEnumerator() in a class

    how do i implement this?

    o tryed the following:

    top
    PHP Code:
        public class devTab TabPageSystem.Collections.IEnumerator 
    code
    PHP Code:
            private int _enumerator 0;

            public 
    bool MoveNext()
            {
                if (
    this.Items.Count == _enumerator)
                {
                    return 
    false;
                }
                else
                {
                    
    _enumerator++;
                    return 
    true;
                }
            }

            public 
    void Reset()
            {
                
    _enumerator 0;
            }

            public 
    object Current
            
    {
                
    get
                
    {
                    return 
    this.Items[_enumerator];
                }
            } 
    (this has more code but it doesnt matter here i think)

    but it keeps saying that i cannot make a foreach statement because the class doesnt implement the getenumerator
    \m/\m/

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You have to implement the IEnumerable interface also. Here this might help you out.
    http://www.c-sharpcorner.com/code/20...InnerClass.asp
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm sorry i took too long but i am having problems with this..i can't understand good the example anyone has another example of this?

    dont bother making one just for this, if u have one show me up plz, otherwise just forget it lol

    \m/\m/

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    I wrote this one then took a look at the link previously posted, and essentially it's the same thing, maybe a little clearer.
    PHP Code:
    using System;
    using System.Collections;
    namespace 
    EnumerationExample
    {
        public class 
    MainApp
        
    {
            public static 
    void Main(string[] args)
            {
                
                
    Widgets widgets = new Widgets();
                
    widgets.Add("Chris");
                
    widgets.Add("Sally");
                
    //show the foreach support
                
    Console.WriteLine("Iterating collection using 'foreach'");
                foreach(
    Widget widget in widgets)
                {
                    
    Console.WriteLine(widget.Name);
                }
                
    //show GetEnumerator support
                
    Console.WriteLine("Iterating collection using 'GetEnumerator()'");
                
    IEnumerator myWidgetEnumerator widgets.GetEnumerator();
                while (
    myWidgetEnumerator.MoveNext())
                {
                    
    Console.WriteLine(((Widget)myWidgetEnumerator.Current).Name);
                }
                
    Console.WriteLine("Press <Enter> to exit ...");
                
    Console.ReadLine();
            }    
        }

        public class 
    Widget
        
    {
            private 
    string name;
            public 
    string Name
            
    {
                
    get { return this.name; }
                
    set this.name value; }
            }
            public 
    Widget(string Name)
            {
                
    this.name Name;
            }
            public 
    Widget(){}
        }

        public class 
    Widgets IEnumerable
        
    {
            private 
    ArrayList widgets = new ArrayList();
            public 
    IEnumerator GetEnumerator()
            {
                return new 
    WidgetEnumerator(widgets);
            }
            protected class 
    WidgetEnumerator IEnumerator
            
    {
                private 
    ArrayList widgets;
                private 
    int index;
                public 
    WidgetEnumerator(ArrayList widgets)
                {
                    
    this.widgets widgets;
                    
    this.Reset();
                }
                public 
    void Reset()
                {
                    
    this.index = -1;
                }
                public 
    bool MoveNext()
                {
                    return (++
    index widgets.Count);
                }
                public 
    object Current
                
    {
                    
    get
                    
    {
                        if ( (
    index 0) || (index >= widgets.Count))
                            throw new 
    IndexOutOfRangeException();
                        return 
    widgets[index];
                    }
                }
            }
            public 
    void Add(Widget w)
            {
                
    this.widgets.Add(w);
            }
            public 
    void Add(string widgetName)
            {
                
    Widget w = new Widget(widgetName);
                
    this.widgets.Add(w);
            }
        }


  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks now did it
    \m/\m/

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