我还是C#的新手,我目前仍陷在一个项目中。所以基本上我目前有一个包含5个文本框,一个复选框和一个按钮的表单。当用户填写信息并提交而未选中复选框时,信息将存储在数组列表中。如果用户注册为火车驾驶员,并标记了火车驾驶员复选框并提交,则需要将“ id”以及所有其他信息(例如名字和姓氏等)传递到数组列表中。
目前,我有一个Person类,可以正常工作。然后,我有一个名为traindrivers的子类,该子类包含一个“ id”变量,每次注册新的Traindriver时,该变量都会“递增”,因为它们的编号不能相同。那么,如果已注册的人被标记为火车司机,我该如何将该“ id”添加到“ personsArrayList”中?
我尝试使用添加新“个人”时所使用的过程。但是由于某种原因,我找不到包含ID的变量。
public class Persons
{
//instance variables
private string firstname;
private string lastname;
private string email;
private int age;
//constructor
public Persons(string firstname, string lastname, string email, int age)
{
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.age = age;
}
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public string Email
{
get { return email; }
set { email = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public override string ToString()
{
return FirstName + " " + LastName + " - " + Email + " - " + Age + ".";
}
}
public class Traindrivers : Persons
{
protected int id;
protected static int mycounter;
public Traindrivers(int id, string firstname, string lastname, string email, int age) : base(firstname, lastname, email, age)
{
mycounter++;
id = mycounter;
}
public int LicensNumber
{
get { return id; }
set { id = value; }
}
public override string ToString()
{
return LicensNumber + FirstName + LastName + Email + Age;
}
}
----------- aspx页面---------
protected void Button1_Click(object sender, EventArgs e)
{
Persons p1 = new Persons(TextBoxFirstName.Text, TextBoxLastName.Text, TextBoxEmail.Text, Convert.ToInt32(TextBoxAge.Text));
personsArrayList.Add(p1);
//Session["trainpersons"] = personsArrayList;
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
// When the checkbox is marked, add "id" to the arraylist
}
protected void ShowButton_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
for (int i = 0; i < personsArrayList.Count; i++)
{
ListBox1.Items.Add(personsArrayList[i].ToString());
Console.WriteLine(i);
}
}
}
protected void trainBtn_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
for (int i = 0; i < traindriversArrayList.Count; i++)
{
ListBox1.Items.Add(traindriversArrayList[i].ToString());
}
}
预期结果是,只要注册了火车司机,就添加“ id”。当然,它应该进入personArrayList。
答案 0 :(得分:0)
在此新修改的基础上,一切正常,只需稍作更改即可。 首先,您不需要任何计数器,静态ID就足够了,并且由于您要增加该ID,因此应该对其进行初始化。而且由于它将自动运行,因此无需将id用作参数
public class Traindrivers : Persons
{
protected int id=0;
// ^^
protected static int mycounter=0;
public Traindrivers(/*int id,*/ string firstname, string lastname, string email, int age) : base(firstname, lastname, email, age)
// ^^^^^^^^^
{
mycounter++;
id = mycounter;
}
public int LicensNumber
{
get { return id; }
set { id = value; }
}
public override string ToString()
{
return LicensNumber + FirstName + LastName + Email + Age;
}
}
当您要使用它时,请在Button1_Click方法中首先检查该复选框是否已选中,然后基于此进行操作,如下所示:
protected void Button1_Click(object sender, EventArgs e){
if(CheckBox1.checked==true){//information is about a traindriver
Traindrivers t1 = new Traindrivers (TextBoxFirstName.Text, TextBoxLastName.Text, TextBoxEmail.Text, Convert.ToInt32(TextBoxAge.Text));
traindriversArrayList.Add(t1);
}
else{//information in textboxes is not about traindriver
Persons p1 = new Persons(TextBoxFirstName.Text, TextBoxLastName.Text, TextBoxEmail.Text, Convert.ToInt32(TextBoxAge.Text));
personsArrayList.Add(p1);
}
}