C# Foreach statement does not contain public definition for GetEnumerator
Asked Answered
P

4

26

I'm having a problem with a Windows Form application I'm building in C#. The error is stating "foreach statement cannot operate on variables of type 'CarBootSale.CarBootSaleList' because 'CarBootSale.CarBootSaleList' does not contain a public definition for 'GetEnumerator'".

I can't seem to understand what is causing this.

This is the code that is throwing up the error:

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

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

and this is the CarBootSaleList class where it's saying there isn't a GetEnumerator definition:

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;
    }
Penton answered 1/3, 2013 at 13:32 Comment(7)
foreach (CarBootSaleList c in Sortcarboot)Aerotherapeutics
you may find an answer here (implementation of foreach): #10930086Sagittal
Are these two lines related? List<CarBootSaleList> Sortcarboot = new List<CarBootSaleList>(); foreach (CarBootSale c in carBootSaleList)Identify
Where are carBootSaleList variable declared?Cardinal
@Marcus It's at the top of the class "CarBootSaleList carBootSaleList = new CarBootSaleList();"Penton
@Penton No, it's not. I'm asking for the VARIABLE carBootSaleList. You have not posted the code that declares it.Cardinal
Is this answered yet please @Danny?Pollinosis
C
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>>
Casiano answered 1/3, 2013 at 13:41 Comment(1)
Did you really just suggest the author use a List which contains a List of CarBootSales? Only the first two options actually make sense to actually do.Sidky
R
18

You don't show us the declaration of carBootSaleList. However from the exception message I can see that it is of type CarBootSaleList. This type doesn't implement the IEnumerable interface and therefore cannot be used in a foreach.

Your CarBootSaleList class should implement IEnumerable<CarBootSale>:

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

    ...

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

    IEnumerator IEnumerable.GetEnumerator()
    {
        return carbootsales.GetEnumerator();
    }
}
Ripple answered 1/3, 2013 at 13:46 Comment(0)
V
5

In foreach loop instead of carBootSaleList use carBootSaleList.data.

You probably do not need answer anymore, but it could help someone.

Versed answered 29/12, 2014 at 21:31 Comment(0)
C
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?

Cannular answered 1/3, 2013 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.