我正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是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);
} …Run Code Online (Sandbox Code Playgroud) 在我的表单加载,我有这个代码:
private void Form1_Load(object sender, EventArgs e)
{
CharityCyclists cyclist1 = new CharityCyclists();
CharityCyclists cyclist2 = new CharityCyclists("a", 1, "Finished", 0, 0, 0, "One Wheel", 1, 500);
cyclist1.Type = "Novelty Charity Cyclist";
cyclist1.Number = 1;
cyclist1.Finished = "Not Finished";
cyclist1.Hours = 0;
cyclist1.Mins = 0;
cyclist1.Secs = 0;
cyclist1.Bicycle = "Tricycle";
cyclist1.Wheels = 3;
cyclist1.FundsRaised = 300;
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条错误,说"'CycleEvent.CharityCyclists'不包含带0参数的构造函数",它说错误与代码的这一部分有关:
CharityCyclists cyclist1 = new CharityCyclists();
Run Code Online (Sandbox Code Playgroud)
这是我的CharityCyclists类:
class CharityCyclists : Cyclists
{
private string bicycle;
private int wheels;
private double fundsRaised;
public …Run Code Online (Sandbox Code Playgroud) 我的Delphi程序构建和编译很好,但是一旦它在调试模式下运行,我得到以下错误;
属性ClientHeight不存在
在查看了所有.DFM文件源之后,每种形式的代码都在那里;
ClientHeight = 111
我不明白我哪里出错了?
我已经创建了一个程序,并对它进行了大量测试,我收到一条错误消息"FormatException未处理,输入字符串格式不正确".当我将任一文本框留空并按下"完成"按钮但如果我输入低于0或高于59的任何值 - 这是我想要允许的数字范围,它会正常工作.我怎么办?当盒子空白时,我没有收到这个错误?这是'btnFinished'背后的代码:
private void btnFinished_Click(object sender, EventArgs e)
{
if (lstCyclists.SelectedIndex >= 0)
{
Cyclists currentCyc = (Cyclists)lstCyclists.SelectedItem;
//Decalre the minsEntered and secsEntered variables for txtMins and textSecs
int minsEntered = int.Parse(txtMins.Text);
int secsEntered = int.Parse(txtSecs.Text);
try
{
//If the status of a cyclist is already set to Finished, show an error
if (currentCyc.Finished.ToString() == "Finished")
{
MessageBox.Show("A time has already been entered for this cyclist");
}
else
{
//if a minute lower than 0 or greater than 59 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C#中的Windows窗体创建应用程序,但是我不断收到错误说明:
NullReferenceException未处理 - 对象引用未设置为对象的实例
它指向具有此代码的行:
carBootSaleList.AddCarBootSale(newCarBootSale);
Run Code Online (Sandbox Code Playgroud)
我在表单界面上的方法:
CarBootSaleList carBootSaleList;
public void AddCarBootSale()
{
AddNewCarBootSale addForm = new AddNewCarBootSale();
if (addForm.ShowDialog() == DialogResult.OK)
{
CarBootSale newCarBootSale = addForm.GetCarBootSaleData();
carBootSaleList.AddCarBootSale(newCarBootSale);
txtCarBootSale.Clear();
txtCarBootSale.Text = newCarBootSale.Display();
}
}
Run Code Online (Sandbox Code Playgroud)
CarBootSaleList类:
public class CarBootSaleList : IDisplay
{
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)
{ …Run Code Online (Sandbox Code Playgroud) c# ×4
unhandled ×2
arguments ×1
client ×1
constructor ×1
definition ×1
delphi ×1
dfm ×1
enumerator ×1
exception ×1
height ×1
input ×1
null ×1
properties ×1
public ×1
reference ×1
string ×1