c#list.add()覆盖位置0的对象

时间:2016-07-07 17:34:41

标签: c# winforms

我正在更新我的问题 我创建了新班级学生

class Student
    {

        private string _firstName;
        private string _lastName;
        private int _exam1;
        private int _exam2;
        private int _exam3;
        private int _finalExam;


        // First Name Property
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        //Last Name Property
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        //Exam 1 Property
        public int Exam1
        {
            get { return _exam1; }
            set { _exam1 = value; }
        }

        // Exam 2 Property
        public int Exam2
        {
            get { return _exam2; }
            set { _exam2 = value; }
        }

        //Exam 3 Property
        public int Exam3
        {
            get { return _exam3; }
            set { _exam3 = value; }
        }

        //Final Exam Property
        public int FinalExam
        {
            get { return _finalExam; }
            set { _finalExam = value; }
        }
    }
}

这是我添加新学生表单类,其中一种方法是添加新学生

public class AddStudent : Form
    {

        StudentForm stu = null;

        public AddStudent()
        {
            InitializeComponent();
            stu = new StudentForm();
            stu.Show();

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            // split the name into first and last name
            string[] name = txtName.Text.Split(',');
            Student std = new Student();
            std.FirstName = name[0];
            std.LastName = name[1];
            std.Exam1 = Int32.Parse(txtExam1.Text);
            std.Exam2 = Int32.Parse(txtExam2.Text);
            std.Exam3 = Int32.Parse(txtExam3.Text);
            std.FinalExam = Int32.Parse(txtFinal.Text);
            stu.addItem(std);
            this.Hide();
        }
    }
}

这是我的主要表单,它有列表框来显示学生列表

public  class StudentForm : Form
    {            
        public StudentForm()
        {
            InitializeComponent();
        }

        public List<Student> students = new List<Student>();

        public void addItem(Student std)
        {

            students.Add(std);
            // it always show me 1 item in list
            MessageBox.Show(students.Count.ToString());

        }
}

4 个答案:

答案 0 :(得分:3)

以下是您的代码的进展情况,添加了评论并删除了不相关的代码:

private void btnSubmit_Click(object sender, EventArgs e)
{
    ...
    // create a _new_ Student form
    Student std = new Student();

    ...

    // Add the student form to itself (huh?)
    std.addItem(std);

    // hide this form
    this.Hide();

    // show the new form
    std.Show();
}

因此,您始终会显示一个包含一个项目的新表单 - 刚刚创建的表单。

答案 1 :(得分:1)

是的,每当您创建表单btnSubmit_Click的新实例并调用Student方法时,addItem()就会出现这种情况。

您宁愿将此字段移至单独的类,如

public class Data
{
    private string _firstName;
    private string _lastName;
    private int _exam1;
    private int _exam2;
    private int _exam3;
    private int _finalExam;
}

在启动时创建表单实例,如

public partial class AddStudent : Form
    {

Student stu = null;
public AddStudent()
{
    InitializeComponent();
    stu = new Student();
    stu.Show();
}

更改Form1中的列表

public List<Data> students = new List<Data>();

在按钮中单击,只需添加Data实例,如

   private void btnSubmit_Click(object sender, EventArgs e)
    {
        // split the name into first and last name
        string[] name = txtName.Text.Split(',');
        Data std = new Data();
        std.FirstName = name[0];
        std.LastName = name[1];
        std.Exam1 = Int32.Parse(txtExam1.Text);
        std.Exam2= Int32.Parse(txtExam2.Text);
        std.Exam3 = Int32.Parse(txtExam3.Text);
        std.FinalExam = Int32.Parse(txtFinal.Text);
        stu.addItem(std);
        this.Hide();
    }

答案 2 :(得分:0)

这是因为您每次使用此行时都会创建Student表单:

Student std = new Student();

因此,每当您点击提交时,您都会创建一个新的学生表单,用于创建新的空public List<Student>

您需要从您的用户界面(Student)和(StudentForm)中分离您的模型(AddStudentForm):

public class Student
{
    public string FirstName { set; get; }
    private string LastName { set; get; }
    private int Exam1 { set; get; }
    private int Exam2 { set; get; }
    private int Exam3 { set; get; }
    private int FinalExam { set; get; }
}

每次添加StudentForm时,您都不需要创建新的Student。相反,您可以在导航到添加屏幕时拥有一个StudentForm并使用ShowDialog(),这会返回到StudentForm的同一个实例。

答案 3 :(得分:-1)

尝试从活动中删除Class Student实例 - btnSubmit_Click event Student std = new Student();