Mam problem z aplikacją Windows Form, którą buduję w C#. Błąd polega na tym, że "instrukcja foreach nie może działać na zmiennych typu" CarBootSale.CarBootSaleList ", ponieważ" CarBootSale.CarBootSaleList "nie zawiera publicznej definicji" GetEnumerator ".C# Foreach nie zawiera publicznej definicji GetEnumerator
Nie mogę zrozumieć, co to powoduje.
Jest to kod, który rzuca się błąd:
List<CarBootSaleList> Sortcarboot = new List<CarBootSaleList>();
foreach (CarBootSale c in carBootSaleList)
{
if (c.Charity == "N/A")
{
Sortcarboot.Add(carBootSaleList);
textReportGenerator.GenerateAllReport(Sortcarboot, AppData.CHARITY);
}
}
i jest to klasa CarBootSaleList gdzie ona mówi, że nie jest to definicja 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;
}
foreach (CarBootSaleList c w Sortcarboot) – Phil
możesz znaleźć odpowiedź tutaj (implementacja foreach): http://stackoverflow.com/questions/10929586/what-is-the-difference-between-for-and-foreach/14812801 # 14812801 – polkduran
Czy te dwie linie są powiązane?Lista Sortcarboot = nowa lista (); foreach (CarBootSale c w carBootSaleList) –