C# Foreach 语句不包含 GetEnumerator 的公共定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15159014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Foreach statement does not contain public definition for GetEnumerator
提问by Danny
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'".
我在使用 C# 构建的 Windows 窗体应用程序时遇到问题。错误指出“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;
}
回答by Grubl3r
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?
您的代码似乎也有点奇怪,就像您在嵌套列表一样?
回答by Guish
Your CarBootSaleList
class is not a list. It is a class that contain a list.
你的CarBootSaleList
班级不是一个列表。它是一个包含列表的类。
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>>
回答by pescolino
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.
您没有向我们展示carBootSaleList
. 但是,从异常消息中我可以看到它的类型为CarBootSaleList
。此类型不实现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();
}
}
回答by Jonhtra
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.
您可能不再需要答案,但它可以帮助某人。