Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing GetEnumerator

Status
Not open for further replies.

ChewDoggie

Programmer
Mar 14, 2005
604
US
Hello All,

I have a Custom Class that is bound to several DataGridView controls elsewhere in my forms and is defined as follows:

Code:
namespace Collections.MyCollections
{
    [DataObject]
    public class clsGenericList : BindingList<clsGeneric>
    {
        public clsGenericList()
        {
        }

        //....other methods here

        public IEnumerator<clsGeneric> GetEnumerator()
        {
            return this.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}

But this code barfs on the GetEnumerator method. Does anyone know how to implement GetEnumerator for this type of class definition ?

Thanks !

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
BindingList<T> already implements IEnumerator<clsGeneric>.GetEnumerator() so you should not need to implement it yourself.
IEnumerable.GetEnumerator() can either call this.GetEnumerator() or base.GetEnumerator()


Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks, Jason !

When I removed the methods for iterating over a collection, then it complained. It was probably b/c I was returning "this.GetEnumerator();" instead of "base.GetEnumerator();", as you pointed out.

In any event, I redefined clsGeneric to look like this:

Code:
    public class clsGenericList<T> : ICollection<T>
    {
        private int m_count = 0;
        private BindingList<T> ThisList = null;
        public clsGenericList()
        {
            ThisList = new BindingList<T>();
        }

        // Implementing ICollections methods here

        public IEnumerator<T> GetEnumerator()
        {
            return ThisList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ThisList.GetEnumerator();
        }
    }

And now all my collection classes inherit from this generic class:

Code:
    public class clsRegistrations : clsGenericList<clsRegistration>
    {
        #region Variables & Properties
        private int m_count = 0;
        private bool m_readonly = false;
        
        // Some custom methods here
    }

And all is hunky-dory again.

Thanks !

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
....although it does seem to perform poorly (slow).

C.

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top