Link to home
Start Free TrialLog in
Avatar of Siv
SivFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Siv

ASKER

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
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.
Avatar of Siv

ASKER

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