tags:

views:

138

answers:

3

I get this error message and I can't figure out why!

Error   1   'Exo5Chap12.ShortCollection<T>' does not implement interface member 
'System.Collections.IEnumerable.GetEnumerator()'. 
'Exo5Chap12.ShortCollection<T>.GetEnumerator()' cannot implement 
'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching 
return type of 'System.Collections.IEnumerator'.    
E:\MyFolders\Dev\c#\Chapter12\Exo5Chap12\Exo5Chap12\exo5.cs 9   18  Exo5Chap12

Here is the code with an implementation of GetEnumerator(). What is wrong?

 public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;
    public IEnumerator<T> GetEnumerator()
    {
        return (innerCollection as IEnumerator<T>).GetEnumerator();
    }
}
+2  A: 

IEnumerable and IEnumerable<T> are different interfaces and your code has to implement both. Your code only implements the GetEnumerator() of IEnumerable<T>, but not GetEnumerator() of IEnumerable. You should consider installing ReSharper which makes it easy to fix errors like this one.

Achim
+1 for Resharper (R#)... it's pretty amazing.
rockinthesixstring
Joel, thanks for the edit. Where do I find the documentation of formatting options?
Achim
@Achim: orange question mark above the textarea.
wRAR
Thanks a lot!!!
Bon_chan
A: 

You need to implement a few more things:

public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;

    #region IList<T> Members

    public int IndexOf(T item)
    {
        return innerCollection.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        innerCollection.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        innerCollection.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            return innerCollection[index];
        }
        set
        {
            innerCollection[index] = value;
        }
    }

    #endregion

    #region ICollection<T> Members

    public void Add(T item)
    {
        innerCollection.Add(item);
    }

    public void Clear()
    {
        innerCollection.Clear();
    }

    public bool Contains(T item)
    {
        return innerCollection.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        innerCollection.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return innerCollection.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        return innerCollection.Remove(item);
    }

    #endregion

    #region IEnumerable<T> Members

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

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

    #endregion
}

This code will compile... :)

code4life
Thank you very much!
Bon_chan
A: 

As others have said, you need to implement IEnumerable as well as IEnumerable<T>. However, since IEnumberable<T> itself implemets IEnumerable this is trivial, just call your generic GetEnumerator():

public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;
    public IEnumerator<T> GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

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

I'm assuming that you have methods for actually adding and removing from the innerCollection and just left them out for brevity since they didn't relate to the question at hand.

Donnie
Thank you very much! Yes I left the rest out. Thanks again!
Bon_chan