C# Foreach statement does not contain public definition for GetEnumerator

后端 未结 4 1579
孤街浪徒
孤街浪徒 2021-02-07 00:33

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.CarBootSale

4条回答
  •  孤街浪徒
    2021-02-07 00:49

    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:

    public class CarBootSaleList : IEnumerable
    {
        private List carbootsales;
    
        ...
    
        public IEnumerator GetEnumerator()
        {
            return carbootsales.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return carbootsales.GetEnumerator();
        }
    }
    

提交回复
热议问题