Trying to create a collection class for my custom class and it's failing - Help!

I have a Class called DataExtractTable which matches a database table and I want to have another class that I can use to hold a collection of DataExtractTable records.  In my days as a VB programmer I had a template for creating my own Collection objects so that it implemented all the add remove iterate type functionality over a collection of my custom class objects. However now I am moving to use C# I decided that I ought to be able to use Generics and simplify things. This is what I have come up with as my collection class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCollector
{
    public class DataExtractTablesColl : IEnumerable<DataExtractTable>
    {
        public List<DataExtractTable> dets = new List<DataExtractTable>();

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

        public void Add(DataExtractTable d) 
        { 
            this.dets.Add(d); 
        }

 
    }
}

Open in new window


Trouble is I am getting this error:
Error      1      'DataCollector.DataExtractTablesColl' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'DataCollector.DataExtractTablesColl.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.      

I cannot see why it doesn't like my GetEnumerator, any help gratefully accepted.  If you think I am going about this the wrong way then please identify what I should be doing.

Siv
SivAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Since your data is stored in a List, do not inherits from IEnumerable.

Simply inherits from List<DataExtractTable> and your collection will be a List with everything already built-in, including the implementation of GetEnumerator.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SivAuthor Commented:
Jacques,

Jacques, thanks for your prompt reply.

One of the reasons I hadn't done that was on reading up on various posts on the web it was explained that if you want to do a "foreach" across your collection List<T> would not work? Hence why I was going down the IEnumerable route.  I have made the change so that the code looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCollector
{
    public class DataExtractTablesColl : List<DataExtractTable>
    {
        public List<DataExtractTable> dets = new List<DataExtractTable>();
 
    }
}

Open in new window


And that has no problems but I suspect if I do want to do a foreach across my collection it may not work.  

Is there a fix that makes what I had work?

Siv
Jacques Bourgeois (James Burger)PresidentCommented:
I do not know where you got your information, but its wrong. I regularly inherits from List and foreach works with it.

Just look at the official documentation for List. The documentation often tells you more than what you read here and there, and you can usually count on it to be right. The Internet is full of false statements about everything.

Look at the Syntax portion of the documentation page, and you will see that List implements IEnumerable<T>, meaning that it already did the job for you.

If you still do not believe me, simply try a foreach on your class. Even without data in it, you will see that the syntax does not generate an error. It would give you an error just as your initial class if it did not implement GetEnumerator.

But change something in your last code. Because you inherits from List, your class is a List. You do not need to an internal list, you do not need to add anything:

    public class DataExtractTablesColl : List<DataExtractTable>
    {
    }

You would add something if you wanted special constructors, supplementary methods and properties, or change the way some of the properties and methods work. Otherwise, inheriting from a class usually automatically gives you all its features. That is the first aim of inheritance.
SivAuthor Commented:
Jacques,

Thanks very much for the solution, I also note your second post and you are correct that it does allow foreach over the collection.

Siv
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.