Generic and Iterator

This is a discussion on Generic and Iterator within the C# Programming forums, part of the General Programming Boards category; Code: class EventAwareList<T> : System.Collections.Generic.List<T> { public delegate void ListHandler( object sender , EventAwareList<T>.ListEventArgs<T> e); public event ListHandler AddEvent; public ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Generic and Iterator

    Code:
    class EventAwareList<T> : System.Collections.Generic.List<T> 
    {
        public delegate void ListHandler(object sender , EventAwareList<T>.ListEventArgs<T> e);
        public event ListHandler AddEvent;
    
       
    
        public class ListEventArgs<U> : EventArgs
        {
            public int index;
            public U value;
        }
        
    
    
        public new void Add(T item)
        {
            base.Add(item);
            if (AddEvent != null)
            {
                ListEventArgs<T> addevArgs = new ListEventArgs<T>();
                addevArgs.value = item;
                addevArgs.index = base.Count - 1;
                AddEvent(this, addevArgs);
            }
        }
    
        public new T this[int i]
        {
    
            get
            {
                return base[i];
    
            }
    
            set
            {
                base[i] = value;
                if (AddEvent != null)
                {
                    ListEventArgs<T> addevArgs = new ListEventArgs<T>();
                    addevArgs.value = base[i];
                    addevArgs.index = i;
                    AddEvent(this, addevArgs);
                }
            }
        }
    
        public new System.Collections.Generic.IEnumerable<T> GetEnumerator()
        {
            
            for(int i = 0; i < base.Count ;++i)
            {
                
                yield return base[i];
            }
        }
    
    }
    In this class I have an iterator.
    It works like this:
    Code:
    EventAwareList<int> el = new EventAwareList<int>();
            
            el.Add(2);
            el.Add(43);
            System.Collections.Generic.List<int> s;
       
            foreach (int i in el.GetEnumerator())
            {
                Console.WriteLine(i);
            }
    But I see that you can write foreach(int i in el), but how?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You should be wondering why your GetEnumerator has the new keyword. The solution to your problem lies there.

    IEnumerable.GetEnumerator (as the name says) returns an IEnumerator, not an IEnumerable. Make this change, and you can use the list directly in the foreach.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You should be wondering why your GetEnumerator has the new keyword. The solution to your problem lies there.
    I know why. Because it is defined in the base class.
    I wanted to add this:
    Code:
    IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
    But it generates an error:Error 1 'EventAwareList<T>.IEnumerable.GetEnumerator()': containing type does not implement interface 'System.Collections.IEnumerable'

    [edit]
    This error means that my class does not implemented IEnumerable. But I thought it should be inherited from the base class.
    Last edited by siavoshkc; 10-03-2006 at 12:12 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    OK, I made it IEnumerator and it worked.
    Thanks a LOT CornedBee.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. Generic Resource Manager, 90% done! Boost enabled!
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2006, 06:37 PM
  3. Templated Generic Resource Manager, WIP..
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 02-19-2006, 05:29 PM
  4. Generic Resource_Manager WIP with lots TODO
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2006, 12:55 AM
  5. Help with defining an iterator class
    By Dragoon_42 in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2003, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21