Foreach语句不能对类型的变量进行操作,因为它不包含GetEnumerator的公共定义

时间:2016-06-04 10:29:40

标签: c# foreach

我有一个简单的类Employee,另一个类是Company,它包含一系列员工。公司类有2个带迭代器的嵌套类。一个人从头到尾订购商品,第二个订单反转订单。 NormalEnumeration是默认值。当我尝试在foreach循环中使用ReverseOrder时,我收到此错误:

  

foreach语句不能对类型的变量进行操作   'Zadanie_1.Company.ReverseEnumeration'因为   'Zadanie_1.Company.ReverseEnumeration'不包含公开内容   'GetEnumerator'的定义

我的问题是:如何实现自定义迭代器?

public class Employee
{
    private string surname, position;
    public string Stanowisko { get { return position; } }
    public Employee (string nazwisko, string stanowisko)
    {
        this.surname = nazwisko;
        this.position = stanowisko;
    }
    public override string ToString()
    {
        return string.Format("nazwisko: {0}, stanowisko: {1}", surname, position);
    }
}

public class Company:IEnumerable
{
    string name;
    private Employee[] employeeArray;
    public Company(string nazwa, Employee[] pracownicy)
    {
        this.name = nazwa;
        this.employeeArray = pracownicy;
    }
    public IEnumerator GetEnumerator()
    {
        //foreach (Pracownik item in pracownicy)
        //{
        //    yield return item;
        //}

      return new NormalEnumeration(this);
    }
    public IEnumerator Ordered()
    {
        return new NormalEnumeration(this);
    }

    public override string ToString()
    {
        return string.Format("Nazwa firmy: {0}, liczba pracowników: {1}", name, employeeArray.Length);
    }
    public class ReverseEnumeration : IEnumerator
    {
        Company f;
        int counter;
        public ReverseEnumeration(Company f)
        {
            this.f = f;
            counter = f.employeeArray.Length;
        }

        public object Current
        {
            get
            {
                Console.WriteLine("Current");
                return f.employeeArray[counter];
            }
        }

        public bool MoveNext()
        {
            if (counter > 0)
            {
                Console.WriteLine("Move next:");
                counter--;
                return true;
            }
            else
                return false;
        }

        public void Reset()
        {
            counter = f.employeeArray.Length;
        }
    }
    public class NormalEnumeration : IEnumerator
    {
        Company f;
        int counter = -1;
        public NormalEnumeration(Company f)
        {
            this.f = f;
        }
        public object Current
        {
            get
            {
                Console.WriteLine("Current");
                return f.employeeArray[counter];
            }
        }

        public bool MoveNext()
        {
            if (counter >= f.employeeArray.Length-1)
                return false;
            else
            {
                counter++;
                return true;
            }

        }

        public void Reset()
        {
            counter = -1;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题下的评论:

  

我试着用这种方式调用反向迭代器:

     

@Component() //configuration avoided for brevity class Component { constructor(private PredictionService) { this.PredictionService.Prediction.subscribe((placesResult)=>{ ... //This is where you get your data. }); } search(){ this.PredictionService.getPredictions(options); } }

为了实现这一目标,foreach (Employee item in new Company.ReverseEnumeration(instancenamehere)) { Console.WriteLine(item); }必须实施ReverseEnumration。如果您希望能够修改枚举器,只需将新属性添加到IEnumerable类。

Company

您可以在构造函数中设置默认枚举器。

public class Company : IEnumerable
{
    public IEnumerator Enumerator { get; set; }

    public IEnumerator GetEnumerator()
    {
        return Enumerator;
    }

    // other members
}

您应该从public Company(string nazwa, Employee[] pracownicy) { Enumerator = new NormalEnumeration(this); // other stuff } 课程中提取您的普查员,因为他们无论如何都不会使用它的内部人员。这将使您能够轻松地从外部更改枚举器。

Company