[英]C# Foreach statement does not contain public definition for GetEnumerator
I'm having a problem with a Windows Form application I'm building in C#.我在使用 C# 构建的 Windows 窗体应用程序时遇到问题。 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'".错误指出“foreach 语句不能对类型为‘CarBootSale.CarBootSaleList’的变量进行操作,因为‘CarBootSale.CarBootSaleList’不包含‘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:这是 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;
}
Your CarBootSaleList
class is not a list.您的CarBootSaleList
类不是列表。 It is a class that contain a list.它是一个包含列表的类。
You have three options:您有三个选择:
Make your CarBootSaleList
object implement IEnumerable
使您的CarBootSaleList
对象实现IEnumerable
or要么
make your CarBootSaleList inherit from List<CarBootSale>
使您的 CarBootSaleList 从List<CarBootSale>
继承
or要么
if you are lazy this could almost do the same thing without extra coding如果你很懒,这几乎可以做同样的事情而无需额外的编码
List<List<CarBootSale>>
You don't show us the declaration of carBootSaleList
.您没有向我们展示carBootSaleList
的声明。 However from the exception message I can see that it is of type CarBootSaleList
.但是,从异常消息中我可以看到它的类型为CarBootSaleList
。 This type doesn't implement the IEnumerable
interface and therefore cannot be used in a foreach.此类型不实现IEnumerable
接口,因此不能在 foreach 中使用。
Your CarBootSaleList
class should implement IEnumerable<CarBootSale>
:您的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();
}
}
In foreach
loop instead of carBootSaleList
use carBootSaleList.data
.在foreach
循环而不是carBootSaleList
使用carBootSaleList.data
。
You probably do not need answer anymore, but it could help someone.您可能不再需要答案,但它可以帮助某人。
You should implement the IEnumerable interface (CarBootSaleList should impl it in your case).您应该实现 IEnumerable 接口(CarBootSaleList 应该在您的情况下实现它)。
http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator.aspx 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但通常更容易继承 System.Collections.ObjectModel.Collection 和朋友
http://msdn.microsoft.com/en-us/library/system.collections.objectmodel.aspx http://msdn.microsoft.com/en-us/library/system.collections.objectmodel.aspx
Your code also seems a bit strange, like you are nesting lists?您的代码似乎也有点奇怪,就像您在嵌套列表一样?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.