简体   繁体   English

丢弃参数C#中的对象类型

[英]Discarding object type in argument c#

This is the portion of code I am having the problem with; 这是我遇到问题的代码部分;

List<People> people = new List<People>();

and this is being populated with 3 different types of objects, all derived from the People class; 并在其中填充了3种不同类型的对象,这些对象均源自People类;

people.Add(new Student(constructorarguments);
people.Add(new AcademicStaff(constructorarguments);
people.Add(new AdministrativeStaff(constructorarguments);

and this is the code which is giving me problems; 这是给我麻烦的代码;

private void studentCheckbox_CheckedChanged(object sender, EventArgs e)
{
    if (studentCheckbox.CheckState == CheckState.Checked)
    {
        foreach (Student student in people)
        {
            if (student.Compare(SearchTextBox.Text) == 0)
            {
                resultsListBox.Items.Add(student.Forename);
            }
        }
    }
    else
    {}
}

It's using a windows form, as you can see from the CheckState code. 从CheckState代码可以看到,它使用的是Windows窗体。 But the problem I'm having is that it's not restricting the compare to students. 但是我遇到的问题是,它不将比较对象局限于学生。 It tries to carry on past the students and tries to cast the AcademicStaff as Students, which is where my program faults. 它试图延续过去的学生,并试图将AcademicStaff转换为学生,这是我的程序有问题的地方。 I've been struggling for the past couple of hours on this and have made no leeway, any help would be much appreciated! 在过去的几个小时中,我一直在为此苦苦挣扎,并且没有任何余地,任何帮助将不胜感激!

I don't believe the problem to be with the form or the classes themselves, but I don't know why it's trying to move on to the AcademicStaff when I've restricted it to the Student type 我不认为问题出在表格或班级本身,但我不知道为什么我将其限制为“学生”类型时为何尝试继续使用“学术工作人员”

It is expected behaviour, compiler enumerates your collection and implicitly cast each item to Student type. 这是预期的行为,编译器枚举您的集合并将每个项目隐式转换为Student类型。
If you want to loop only Students you should filter your people collection first with OfType<>() : 如果只想循环Students ,则应首先使用OfType <>()过滤people集合:

foreach(var student in people.OfType<Student>())
{
  ..
}

You can't iterate using "Student" Type on a list that has people, this code shouldn't compile. 您不能在有人员的列表上使用“学生”类型进行迭代,因此不应编译此代码。 You need to iterate something like that : 您需要迭代这样的事情:

foreach (People person in people)

and if you want to cast the current person as student you should write : 如果您想将当前的人选为学生,您应该写:

Student s = person as Student;

and then you will be able the access the student methods. 然后您将能够访问学生方法。

You could use the linq-method OfType(): 您可以使用linq方法OfType():

private void studentCheckbox_CheckedChanged(object sender, EventArgs e)
{
    if (studentCheckbox.CheckState == CheckState.Checked)
    {
        foreach (Student student in people.OfType<Student>())
        {
            if (student.Compare(SearchTextBox.Text) == 0)
            {
                resultsListBox.Items.Add(student.Forename);
            }
        }
    }
    else
    {}
}

Hope this will help you in your quest. 希望这对您的追求有所帮助。

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

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