Tôi gặp sự cố với ứng dụng Biểu mẫu Windows mà tôi đang xây dựng trong C#. Lỗi này cho biết "tuyên bố foreach không thể hoạt động trên các biến kiểu 'CarBootSale.CarBootSaleList' vì 'CarBootSale.CarBootSaleList' không chứa định nghĩa công khai cho 'GetEnumerator'".C# Tuyên bố Foreach không chứa định nghĩa công khai cho GetEnumerator
Tôi dường như không hiểu điều gì đang gây ra điều này.
Đây là mã được ném lên các lỗi:
List<CarBootSaleList> Sortcarboot = new List<CarBootSaleList>();
foreach (CarBootSale c in carBootSaleList)
{
if (c.Charity == "N/A")
{
Sortcarboot.Add(carBootSaleList);
textReportGenerator.GenerateAllReport(Sortcarboot, AppData.CHARITY);
}
}
và đây là lớp CarBootSaleList nơi nó nói không có một định nghĩa 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 trong Sortcarboot) – Phil
bạn có thể tìm thấy câu trả lời ở đây (thực hiện foreach): http://stackoverflow.com/questions/10929586/what-is-the-difference-between-for-and-foreach/14812801 # 14812801 – polkduran
Hai dòng này có liên quan không?Liệt kê Sortcarboot = new List (); foreach (CarBootSale c trong carBootSaleList) –