26

我在使用 C# 构建的 Windows 窗体应用程序时遇到问题。错误说明“foreach 语句无法对 'CarBootSale.CarBootSaleList' 类型的变量进行操作,因为 'CarBootSale.CarBootSaleList' 不包含 'GetEnumerator' 的公共定义”。

我似乎无法理解是什么原因造成的。

这是引发错误的代码:

        List<CarBootSaleList> Sortcarboot = new List<CarBootSaleList>();

        foreach (CarBootSale c in carBootSaleList)
        {
            if (c.Charity == "N/A")
            {
                Sortcarboot.Add(carBootSaleList);
                textReportGenerator.GenerateAllReport(Sortcarboot, AppData.CHARITY);
            }
        }

这是 CarBootSaleList 类,它说没有 GetEnumerator 定义:

public class CarBootSaleList
{

    private List<CarBootSale> carbootsales;

    public CarBootSaleList()
    {
        carbootsales = new List<CarBootSale>();
    }

    public bool AddCarBootSale(CarBootSale carbootsale)
    {
        bool success = true;
        foreach (CarBootSale cbs in carbootsales)
        {
            if (cbs.ID == carbootsale.ID)
            {
                success = false;
            }
        }
        if (success)
        {
            carbootsales.Add(carbootsale);
        }
        return success;
    }

    public void DeleteCarBootSale(CarBootSale carbootsale)
    {
        carbootsales.Remove(carbootsale);
    }

    public int GetListSize()
    {
        return carbootsales.Count();
    }

    public List<CarBootSale> ReturnList()
    {
        return carbootsales;
    }

    public string Display()
    {
        string msg = "";

        foreach (CarBootSale cbs in carbootsales)
        {
            msg += String.Format("{0}  {1}", cbs.ID, cbs.Location, cbs.Date);
            msg += Environment.NewLine;
        }
        return msg;
    }
4

4 回答 4

22

Your CarBootSaleList class is not a list. It is a class that contain a list.

You have three options:

Make your CarBootSaleList object implement IEnumerable

or

make your CarBootSaleList inherit from List<CarBootSale>

or

if you are lazy this could almost do the same thing without extra coding

List<List<CarBootSale>>
于 2013-03-01T13:41:58.997 回答
18

您没有向我们展示carBootSaleList. 但是,从异常消息中我可以看到它的类型为CarBootSaleList. 此类型不实现IEnumerable接口,因此不能在 foreach 中使用。

你的CarBootSaleList班级应该实现IEnumerable<CarBootSale>

public class CarBootSaleList : IEnumerable<CarBootSale>
{
    private List<CarBootSale> carbootsales;

    ...

    public IEnumerator<CarBootSale> GetEnumerator()
    {
        return carbootsales.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return carbootsales.GetEnumerator();
    }
}
于 2013-03-01T13:46:31.523 回答
4

foreach循环中而不是carBootSaleList使用carBootSaleList.data.

您可能不再需要答案,但它可以帮助某人。

于 2014-12-29T21:31:07.040 回答
1

You should implement the IEnumerable interface (CarBootSaleList should impl it in your case).

http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator.aspx

But it is usually easier to subclass System.Collections.ObjectModel.Collection and friends

http://msdn.microsoft.com/en-us/library/system.collections.objectmodel.aspx

Your code also seems a bit strange, like you are nesting lists?

于 2013-03-01T13:41:48.487 回答