Ich habe ein Problem mit einer Windows Form-Anwendung, die ich in C# erstellen. Der Fehler besagt, dass foreach-Anweisung nicht auf Variablen vom Typ "CarBootSale.CarBootSaleList" angewendet werden kann, da "CarBootSale.CarBootSaleList" keine öffentliche Definition für "GetEnumerator" enthält.C# Foreach-Anweisung enthält keine öffentliche Definition für GetEnumerator
Ich kann nicht zu verstehen scheinen, was das verursacht.
Dies ist der Code, der den Fehler werfen wird:
List<CarBootSaleList> Sortcarboot = new List<CarBootSaleList>();
foreach (CarBootSale c in carBootSaleList)
{
if (c.Charity == "N/A")
{
Sortcarboot.Add(carBootSaleList);
textReportGenerator.GenerateAllReport(Sortcarboot, AppData.CHARITY);
}
}
und dies ist die CarBootSaleList Klasse, wo sie es sagt kein GetEnumerator Definition lautet:
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 in Sortcarboot) – Phil
Sie hier eine Antwort finden kann (Umsetzung von foreach): http://stackoverflow.com/questions/10929586/what-is-the-difference-between-for-and-foreach/14812801 # 14812801 – polkduran
Sind diese beiden Zeilen verwandt?Liste Sortcarboot = neue Liste (); foreach (CarBootSale c in der carBootSaleList) –