locked
IEnumerable issue RRS feed

  • Question


  • Hello,
     I am trying to compile my code(a console application) with the following class, and it gives me the error (CS0536)  as shown below. However, if the CityCollection class is implemented as a regular class without any inheritance from the IEnumerable interface, it compiles OK.

    Any ideas/thoughts?? Thanks!!









    Code:
    CityCollection.cs

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

    namespace TestIEnum
    {
        public class CityCollection : IEnumerable<string>
        {
            string[] m_Cities = { "New York", "Paris", "London" };

            public IEnumerator<string> GetEnumerator()
            {
                for (int i = 0; i < m_Cities.Length; i++)
                    yield return m_CitiesIdea;
                //return new MyEnumerator(this);
            }
        }
    }

    Program.cs
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestIEnum
    {
        class Program
        {
            static void Main()
            {
                CityCollection SampleOne = new CityCollection();
                foreach (string s in SampleOne)
                    Console.WriteLine(s);
            }
        }
    }

    Error:
    'TestIEnum.CityCollection' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'TestIEnum.CityCollection.GetEnumerator()' is either static, not public, or has the wrong return type.   


    Wednesday, October 4, 2006 7:23 PM

Answers

  • Hi

    System.Collections.Generic.IEnumerable -interface declaration look like this:

    public interface IEnumerable<T> : IEnumerable
    So, you need implement System.Collections.IEnumerable.GetEnumerator() method.
    CityCollection.cs could look like this:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Collections;

    namespace TestIEnum

    {

    public class CityCollection : IEnumerable<string>

    {

    string[] m_Cities = { "New York", "Paris", "London" };

    public IEnumerator<string> GetEnumerator()

    {

    for (int i = 0; i < m_Cities.Length; i++)

    yield return m_CitiesIdea;

    }

    IEnumerator IEnumerable.GetEnumerator()

    {

    return null;

    }

    }

    }

     

    Hope this help.

    Yours Markku

     
    Wednesday, October 4, 2006 9:16 PM

All replies

  • You need 'new' keyword. Line of defining GetEnumerator() should be:
    public new IEnumerator<string> GetEnumerator()
    Wednesday, October 4, 2006 7:50 PM
  • the solution does not work...
    Wednesday, October 4, 2006 9:11 PM
  • Hi

    System.Collections.Generic.IEnumerable -interface declaration look like this:

    public interface IEnumerable<T> : IEnumerable
    So, you need implement System.Collections.IEnumerable.GetEnumerator() method.
    CityCollection.cs could look like this:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Collections;

    namespace TestIEnum

    {

    public class CityCollection : IEnumerable<string>

    {

    string[] m_Cities = { "New York", "Paris", "London" };

    public IEnumerator<string> GetEnumerator()

    {

    for (int i = 0; i < m_Cities.Length; i++)

    yield return m_CitiesIdea;

    }

    IEnumerator IEnumerable.GetEnumerator()

    {

    return null;

    }

    }

    }

     

    Hope this help.

    Yours Markku

     
    Wednesday, October 4, 2006 9:16 PM
  • thanks buddy!!  works fine
    Wednesday, October 4, 2006 9:23 PM