我在使用 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;
}