Monday 26 August 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...