簡體   English   中英

C#確定列表項的對象類型

[英]C# determining object type of list item

我正在為我的C#類做一個項目,該項目將來自Student對象(父母)或DormStudent(child)的學生添加到為Student對象設置的列表中。 我需要根據學生ID讀取對象,並確定它是學生還是宿舍學生,並相應地填寫其余表格。

    int pos = MainMenu.myList.FindIndex(x => x.ID == validID);
    if (MainMenu.myList[pos] == Student)
    {
        Student tempStu = MainMenu.myList[pos];
        nameTextBox.Text = tempStu.Name;
    }
    else
    {
        DormStudent tempDorm = MainMenu.myList[pos];
        dormCheckBox.Checked = true;
        dormTextBox.Text = tempDorm.Dorm;
        if (tempDorm.MealType == "B")
        {
            basicRadioButton.Checked = true;
        }
        else if (tempDorm.MealType == "M")
        {
            mediumRadioButton.Checked = true;
        }
        else
        {
            highRadioButton.Checked = true;
        }
    }

這是列表和對象項

    public static List<Student> myList = new List<Student>();

    [Serializable]
    public class DormStudent : Student
    {
        public string Dorm{get; set;}
        public string MealType { get; set; }
        public DormStudent() : base()
        {
            Dorm = "No Dorm";
            MealType = "B";
        }
        public DormStudent(int i, string n, string d, string m) : base(i, n)
        {
            Dorm = d;
            MealType = m;
        }
    }

    [Serializable]
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<int> Grades;
        public Student()
        {
            ID = 0;
            Name = "No Student";
            Grades = new List<int>();
        }
        public Student(int i, string n)
        {
            ID = i;
            Name = n;
        }
    }

要確定對象是否為特定類型,請使用is運算符。

即。

if (MainMenu.myList[pos] is Student)
{
    ...
}
else if (MainMenu.myList[pos] is DormStudent)
{
    ...
}

現在,在這種特殊情況下,由於我上面的代碼編寫方式將無法正常工作,因為DormStudent繼承自Student ,因此第一個子句將同時捕獲這兩種類型。

要處理此問題,請反轉檢查:

if (MainMenu.myList[pos] is DormStudent)
{
    ...
}
else if (MainMenu.myList[pos] is Student)
{
    ...
}

這仍然存在一個問題,即如果繼承自DormStudentStudent任何其他類型出現,它將被上述if語句捕獲。 如果您不想這樣做,請按照以下方法僅識別已知類型:

if (MainMenu.myList[pos].GetType() == typeof(DormStudent))
{
    ...
}
else if (MainMenu.myList[pos].GetType() == typeof(Student))
{
    ...
}
else
{
    ... // other type?
}

由於DormStudend源自學生,因此您需要首先詢問對象是否為DormStudent。 (通常從特定到一般)

因此,您需要像這樣交換If-Statement:

if(MainMenu.myList[pos] is DormStudent)
{
  ...
}
else
{
   ...
}

回復您的評論:您可以改用as關鍵字來使其變得更容易。 基本上是一個“嘗試轉換”,它將返回已轉換的對象;如果無法轉換,則返回null。

Student student = MainMenu.myList[pos];
DormStudent dormStudent = student as DormStudent;

if(dormStudent!= null)
{
     dormTextBox.Text = dormStudent.Dorm;
}
else
{
    nameTextBox.Text = student.Name;
}

對於這種設計是否對其他人最佳,我留下評論,但是您正在尋找的是is運算符關鍵字。

var someStudent = MainMenu.myList[pos];
//Check for null here
if (someStudent is DormStudent )
{
    DormStudent tempDorm = someStudent as DormStudent ;
    dormCheckBox.Checked = true;
    dormTextBox.Text = tempDorm.Dorm;
    if (tempDorm.MealType == "B")
    {
        basicRadioButton.Checked = true;
    }
    else if (tempDorm.MealType == "M")
    {
        mediumRadioButton.Checked = true;
    }
    else
    {
        highRadioButton.Checked = true;
    }
    Student tempStu = someStudent ;
    nameTextBox.Text = tempStu.Name;
}
else
{
    nameTextBox.Text = someStudent.Name;
}

但是您也可以as其作為null檢查:

var someStudent = MainMenu.myList[pos];
//Null check here?
var dormStudent = someStudent as DormStudent;
if (dormStudent != null)
{
    DormStudent tempDorm = someStudent as DormStudent ;
    dormCheckBox.Checked = true;
    dormTextBox.Text = dormStudent.Dorm;
    if (dormStudent.MealType == "B")
    {
        basicRadioButton.Checked = true;
    }
    else if (dormStudent.MealType == "M")
    {
        mediumRadioButton.Checked = true;
    }
    else
    {
        highRadioButton.Checked = true;
    }
}
nameTextBox.Text = someStudent.Name;

如果(!(MainMenu.myList [pos]是DormStudent))

您需要的操作員來測試類型並測試后代。 您也可以從列表中檢索Student,然后使用as運算符將其轉換為DormStudent並檢查是否為null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM