有人可以告诉我我做错了什么?我正在尝试有一个列表集合,如果新学生注册,可以动态添加。
这是我的代码
classStudent.cs
public class ClassStudent
{
public static List<_Student> st = new List<_Student>()
{
new _Student { StudentNumber="1234",
FirstName ="AAA",
LastName = "BBB",
UserName = "CCC",
Password ="DDD",
DateOfBirth = "",
Gender ="",
EmailAdress = "",
AwardID =""},
new _Student { StudentNumber="5678",
FirstName ="BBB",
LastName = "DDD" ,
UserName = "FFF",
Password ="GGG",
DateOfBirth = "",
Gender = "",
EmailAdress = "",
AwardID =""});
}
}
//然后在同一个.cs文件中,我编写了以下添加新学生的方法
public static void AddNewStudent(string user_name, string first_name, string last_name,
string gender, string award_id, string date_of_birth, string email_address, string password)
{
st.Add( new _Student
{
FirstName = first_name,
LastName = last_name,
UserName = user_name,
Gender = gender,
AwardID = award_id,
Password = password,
DateOfBirth = date_of_birth,
EmailAdress = email_address
});
}
//现在我在register.cs中调用AddNewStudent方法,如下所示
register.cs
protected void Button1_Click(object sender, EventArgs e)
{
string first_name = TextBoxFirstName.Text;
string last_name = TextBoxLastName.Text;
string date_of_birth = TextBoxDateOfBirth.Text;
string password = TextBoxPassword.Text;
string email_address = TextBoxEmail.Text;
string student_number = result.ToString();
string _gender = gender.SelectedValue;
string awardId = AwardList.Text;
ClassStudent.AddNewStudent(student_number, first_name, last_name, _gender,
awardId, date_of_birth, email_address, password); //method call
}
但这似乎没有做任何事情,我甚至没有得到任何错误,但同时它没有添加到列表集合。
您能否告诉我上述代码中的更改内容。我需要的是能够在新学生注册时添加到列表中。
谢谢
答案 0 :(得分:0)
我使用您的代码创建了一个示例应用程序,可以确认列表已填充并增加。
向表单添加一个新文本框,并在调用AddNewStudent后检索列表的计数器:
...
ClassStudent.AddNewStudent(student_number, first_name, last_name, _gender,
awardId, date_of_birth, email_address, password);
textBoxCount.Text = ClassStudent.st.Count().ToString();
您将看到每个按钮单击文本框会增加。