Sunday, 20 November 2016

c# - Tag a Class object containing a list



I have a Class Student with



string wholeName;
List < int > scoresList;



I compiled an object students from text boxes into this:




    private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
if (IsValidData())
{
List result = txtScores.Text.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
Student student = new Student(txtName.Text, result.Select(int.Parse).ToList());
this.Tag = student;
this.Close();

}


On the Form1 side I have this:



    private void btnAddNew_Click(object sender, EventArgs e)
{
AddNewStudent addStudentForm = new AddNewStudent();
DialogResult selectedButton = addStudentForm.ShowDialog();
addStudentForm.Tag = new Student();

if (selectedButton == DialogResult.OK)
student = addStudentForm.Tag as Student;
students.Add(student);

}


I should be taking the Tag from the user information in the other form to construct a Student student and passing it back to Form1 and adding it to my students collection. Once it's added it should function like my manually created students when the form is loaded. Instead the list is null during runtime. Can I even move an entire object that contains a list through the Tag and if I can where did I go wrong?


Answer



you are overiding Tag value in addStudentForm.Tag = new Student(); line



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...