简体   繁体   English

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

[英]c# list.add() overwrite the object at position 0

i am updating my question to this i created new class Student 我将我的问题更新为此,我创建了新班级的学生

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; }
        }
    }
}

this is my add new student form class with one method to add new student 这是我用一种添加新学生的方法添加新学生表格类

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();
        }
    }
}

and this is my main form it has listbox to display list of students 这是我的主要形式,它具有显示学生列表的列表框

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());

        }
}

Here's how your code is progressing, with comments added and irrelevant code removed: 这是代码的进度,添加了注释,并删除了不相关的代码:

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();
}

So you are always showing a new form with one item - the one that was just created. 因此,您始终会显示一个只有一个项目的新表单-刚刚创建的一个表单。

Yeah that's cause in your btnSubmit_Click every time you are creating a new instance of the form Student and calling the addItem() method. 是的,这是在您每次创建Student表单的新实例并调用addItem()方法时在btnSubmit_Click引起的。

You rather move this field to a separate class like 您宁愿将此字段移到一个单独的类,例如

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

have the form instance created in the start up like 在启动时创建了表单实例,例如

public partial class AddStudent : Form
    {

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

Change the list in Form1 在Form1中更改列表

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

In button click just add the Data instance like 在按钮中单击,只需添加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();
    }

This is because you're creating the Student form each time using this line: 这是因为您每次都使用以下行创建Student表单:

Student std = new Student();

So every time you're clicking submit, you're creating a new Student form which creates a new empty public List<Student> 因此,每次单击“提交”时,您都在创建一个新的“学生”表单,该表单将创建一个新的空public List<Student>

You need to seperate your model ( Student ) from your UI ( StudentForm ) and ( AddStudentForm ): 您需要将模型( Student )与UI( StudentForm )和( AddStudentFormAddStudentForm

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; }
}

You don't need to create a new StudentForm each time you add a Student . 您无需在每次添加Student时都创建新的StudentForm Instead, you can have one StudentForm and use ShowDialog() when navigating to the Addition Screen, this you go back to the same instance of StudentForm . 取而代之的是,您可以拥有一个StudentForm并在导航到“添加屏幕”时使用ShowDialog() ,然后返回到StudentForm的相同实例。

尝试从事件中删除Class Student实例化btnSubmit_Click event Student std = new Student();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM