0

I don't think I understand IEnumerable and have done several google search.

I'm supposed to make an Container of some objects, where the class Container should handle the updating of the collection the Container represent (add, remove).

the collection Container should be and instance of IEnumerable<element>.

namespace ....
{
    public class Container : Ienumerable<Elements>
    { 
        List<Elements> _elm = new List<Elements>();
        List<Elements> _add = new List<Elements>();
        List<Elements> _rem = new List<Elements>();

        public voic PendAdd(Element elm)
        {
            _add.Add(elm);
        }

        public voic Pendrem(Element elm)
        {
            _rem.remove(elm);
        }

         ...

        public IEnumerator<Element> GetEnumerator()
        {
            return _elm.GetEnumerator();
        }

    }
}

My problem is that IEnumerable<Elements> is an abstract type and if i make _elm this type i can't handle the state of the Container even if i make it

IEnumerable<Element> _elm = new List<Element> ();

VS complains about the error:

'Container' does not implement interface member 'IEnumerable.GetEnumerator()'. 'Container.GetEnumerator()' cannot implement 'IEnumerable.GetEnumerator()' because it does not have the matching return of 'IEnumerator'.

I know that there are several answers about IEnumerable but they do not help me.

Roman
  • 11,966
  • 10
  • 38
  • 47
kam
  • 590
  • 2
  • 11
  • 1
    What is the difference between `Element` and `Elements`? You inherit `IEnumerable` but declare the return type of `GetEnumerator` as `IEnumerator` and your missing the non-generic `IEnumerator GetEnumerator()` method. – René Vogt Mar 14 '17 at 09:13
  • 1
    If you use the 'Implement Interface' shortcut in Visual Studio, it will create the methods that you are required to implement. As per xanatos's answer, that includes the missing GetEnumerator method together with its correct return type. – Adrian Wragg Mar 14 '17 at 09:14
  • Why are you inheriting from IEnumerable? And why do you have three different Lists? You can just have a wrapper class around an IEnumerable (such as a List) and have methods that add/remove from that list. – Steve Mar 14 '17 at 09:14
  • 1
    Searching google for `does not implement interface member 'IEnumerable.GetEnumerator()'.` brings up right away several SO posts of which one of them is ► [**Troubles implementing IEnumerable**](http://stackoverflow.com/questions/8760322/troubles-implementing-ienumerablet) – Nope Mar 14 '17 at 09:15
  • Thanks for all comments the elements / element was a typo. sry if my question wasn't that clear, I'm new to OO and C#. My expertise is primarely Functional programming like SML, F# and Ocaml. And can say that I got the same emotions as an OO guy trying to learn Functional programming. I simply hetes it.. I got some followups but i make them down in the answer section – kam Mar 15 '17 at 12:39
  • I have to create a customized IEnumerator innerclass because i have to expand the MoveNext, Reset and Dispose methods to do some more work. that's is not the questions. I got around most of it but when I try to make the object IEnumerator.Current method then it make the error 'Namespace.EnumeratorName.Current' in explicit interface declaration is not a member of interface. I have implimentet the `public element Current` method first and the object method looks like `object Ienumerator.Current { get { return Current; } }` – kam Mar 15 '17 at 12:48

1 Answers1

3

You are missing the IEnumerable.GetEnumerator(): IEnumerable<T> inherits from IEnumerable. so in your class you need two distinct GetEnumerator() with two distinct return types.

public class Container : IEnumerable<Element>
{
    List<Element> _elm = new List<Element>();
    List<Element> _add = new List<Element>();
    List<Element> _rem = new List<Element>();

    public void PendAdd(Element elm)
    {
        _add.Add(elm);
    }

    public void Pendrem(Element elm)
    {
        _rem.Remove(elm);
    }

    public IEnumerator<Element> GetEnumerator()
    {
        return _elm.GetEnumerator();
    }

    // This is called "explicit interface implementation", see
    // https://msdn.microsoft.com/en-us/library/ms173157.aspx
    IEnumerator IEnumerable.GetEnumerator()
    {
        // will call the "other" GetEnumerator()
        return GetEnumerator();
    }
}

The next time you have to implement an interface, I suggest you right click on the interface in Visual Studio and select the Implement Interface to get the skeleton of all the methods you need.

xanatos
  • 109,618
  • 12
  • 197
  • 280