简体   繁体   English

用C#中的对象填充列表

[英]Filling a List with Objects in C#

I'm trying to make a simple program with GUI in c# but unfortunately I have some difficulties. 我正在尝试使用c#中的GUI创建一个简单的程序,但是不幸的是我遇到了一些困难。 Now I'll try to explain the basic structure of my program. 现在,我将尝试解释程序的基本结构。 I have 3 classes for three different groups of people(University Professors, University Students and people who don't work or study either). 我为三个不同的人群(大学教授,大学生和既不工作也不学习的人)开设3个课程。 I have some methods for reading information from a file(professor's title, name, university name, student's faculty number, etc.). 我有一些从文件中读取信息的方法(教授的头衔,姓名,大学名称,学生的教职编号等)。 I read the file line by line and save the information in an object of type one of the 3 classes. 我逐行读取文件,并将信息保存在3个类之一的对象中。 After that I put that object in a List. 之后,我将该对象放入列表中。 So here comes my problem. 所以这是我的问题。 I want to read every object from the list and take its name and put in on a dynamically created labels on other windows form. 我想从列表中读取每个对象并取其名称,然后放在其他Windows窗体上动态创建的标签上。 Here it is a little part of my code: 这是我的代码的一小部分:

private void button1_Click(object sender, EventArgs e)
{
        ForeignPeople fPeople = new ForeignPeople();
        UniversityProfessors uProf = new UniversityProfessors();
        UniversityStudents uStudents = new UniversityStudents();
        if (radioButton1.Checked == true)
        {
            objList1 = loadList();
            Form2 f2 = new Form2();
            for (int i = 0; i < objList1.Count; i++)
            {
                if (objList1[i] is UniversityStudents)
                {
                    uStudents = (UniversityStudents)objList1[i];
                    tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
                    Label et_tag = new Label();
                    et_tag.Name = "label" + i.ToString();
                    et_tag.Text = uStudents.getFirstName().ToString() + " " + uStudents.getLastName().ToString();
                    et_tag.AutoSize = true;
                    f2.tableLayoutPanel1.Controls.Add(et_tag, 0, i);
                    Label op = new Label();
                    op.AutoSize = true;
                    op.Text = "description";
                    f2.tableLayoutPanel1.Controls.Add(op, 1, i);
                }
                else if (objList1[i] is UniversityProfessors)
                {
                    uProf = (UniversityProfessors)objList1[i];
                    tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
                    Label et_tag = new Label();
                    Label label = new Label();
                    et_tag.Name = "label" + i.ToString();
                    et_tag.Text = uProf.getFirstName().ToString() + " " + uProf.getLastName().ToString();
                    et_tag.AutoSize = true;
                    f2.tableLayoutPanel1.Controls.Add(et_tag, 0, i);
                    Label op = new Label();
                    op.AutoSize = true;
                    op.Text = "description";
                    f2.tableLayoutPanel1.Controls.Add(op, 1, i);
                }
                else if (objList1[i] is ForeignPeople)
                {
                    fPeople = (ForeignPeople)objList1[i];
                    String name, Name;
                    tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
                    Label et_tag = new Label();
                    et_tag.Name = "label" + i.ToString();
                    et_tag.Text = fPeople.getFirstName().ToString() + " " + fPeople.getLastName().ToString();;
                    et_tag.AutoSize = true;
                    f2.tableLayoutPanel1.Controls.Add(et_tag, 0, i);
                    Label op = new Label();
                    op.AutoSize = true;
                    op.Text = "description";
                    f2.tableLayoutPanel1.Controls.Add(op, 1, i);
                }

            }
            f2.FormClosed += new FormClosedEventHandler(childFormClosed);
            f2.Show();
            this.Hide();
}

But if I have two or more lines which belongs to one Object(for example I have two or more students, or two or more professors in the file) the text of all the labels becomes with the name of the last read object. 但是,如果我有两个或更多行属于一个对象(例如,文件中有两个或更多学生,或者文件中有两个或多个教授),则所有标签的文本将带有最后一个读取对象的名称。 I know that the problem is in the List or in the cast which I make but I can't figure out another way of doing what I want. 我知道问题出在清单或演员表中,但我想不出另一种方法来做自己想要的事情。 I'll be extremely grateful if someone can help. 如果有人可以提供帮助,我将非常感激。

In addition to the change that phoog mentioned in the comments, I would also move the instantiation of your "people" objects down into the section for each within the loop. 除了phoog在注释中提到的更改之外,我还将循环中每个对象的“ people”对象的实例移到该部分中。 That way you can be sure that the old one is being properly destroyed. 这样一来,您可以确保旧的被正确销毁。 I would also add a check to make sure that the cast worked. 我还要添加一个检查以确保强制转换正常。

private void button1_Click(object sender, EventArgs e)
{
    if (radioButton1.Checked == true)
    {
        objList1 = loadList();
        Form2 f2 = new Form2();

        for (int i = 0; i < objList1.Count; i++)
        {
            if (objList1[i] is UniversityStudents)
            {
                UniversityStudents uStudents = (UniversityStudents)objList1[i];

                if (uStudents != null)
                {
                    // do stuff
                }
                else
                {
                    // do something sensible with the error here
                }
            }
            // if clauses for the other "people" objects
            // ...
        }
        f2.FormClosed += new FormClosedEventHandler(childFormClosed);
        f2.Show();
        this.Hide();
    }
}

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

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