2013-04-03 70 views
0

我想檢查是否有某種類型的類在c#中。它在表單上打印出某些標籤,具體取決於它是哪類。這是我迄今爲止所做的,它適用於「if」聲明。但是,我收到「無法投射類型對象」的錯誤。在這種情況下可以使用if-else語句嗎?看看是否有什麼類型的類

public void ShowStaffData(string pName) 
{ 
    //Gets Staff Details from the name slected int he list box in the form 
    people.CreatePeople(); 
    var currentPerson = 
    people.person.Where(p => p.Forename + " " + p.Surname == pName); 

    // How the info is printed out if person selected in 
    // a member of Accademic Staff 
    AccademicStaff accademic = currentPerson as AccademicStaff; 

    if (currentPerson!=null) 
    { 
    foreach (AccademicStaff accStaff in currentPerson) 
    { 
     label9.Text = accStaff.Forename + " " + accStaff.Surname; 
     label10.Text = accStaff.IdentificationNumber.ToString(); 
     label11.Text = accStaff.DateOfBirth; 
     label12.Text = accStaff.Address; 
     label13.Text = accStaff.Office; 
     label14.Text = accStaff.School; 
     label15.Text = accStaff.ModuleLeaderOf; 
     label16.Text = accStaff.ProgramLeaderOf; 
    } 
    }  
    else 
    { 
    // How the info is printed out if person selected in 
    // a member of Admin Staff 

    foreach (AdminStaff admin in currentPerson) 
    { 
     label9.Text = admin.Forename + " " + admin.Surname; 
     label10.Text = admin.IdentificationNumber.ToString(); 
     label11.Text = admin.DateOfBirth; 
     label12.Text = admin.Address; 
     label13.Text = admin.Office; 
     label6.Text = "Job Role"; 
     label14.Text = admin.JobRole; 
     label7.Dispose(); 
     label8.Dispose(); 
     label15.Dispose(); 
     label16.Dispose(); 
    } 
    } 
} 
+0

你得到的錯誤是哪一行? –

+0

也許['is'](http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.71%29.aspx)運算符有幫助嗎? – phg

回答

7

首先,currentPersion是項目的集合。使用Single方法,使之成爲單一的項目:

var currentPerson = 
    people.person.Where(p => p.Forename + " " + p.Surname == pName).Single(); 

(如果這是可能的,你沒有得到比賽的一切,你會用SingleOrDefault,然後檢查是否currentPersion爲空)。

其次,試圖投你正在檢查,而不是accademic變量currentPerson變量之後:

AccademicStaff accademic = currentPerson as AccademicStaff; 
if (accademic != null) 

現在你不需要循環無論是。使用accademic變量在第一部分,並在第二投的參考AdminStaff

AdminStaff admin = currentPerson as AdminStaff; 
+0

+1表示鑄造後的空檢查。 – Clint

+2

這就是爲什麼這個傢伙代表是220K + –

+0

只有在pName是唯一的情況下使用Single()。如果可能有多個具有相同名稱的人,則不要使用Single()並使用foreach處理每個記錄(或者使用FirstOrDefault()並檢查null)是否需要單個記錄。如果pName是唯一的但可能不匹配記錄使用SingleOrDefault()而不是Single() –

0
if (instance.GetType().IsClass) 
{ 

} 

應該做的伎倆。

但是你不應該這樣做,如果你需要不同的類來打印不同的東西,那麼我建議沿着創建接口的路線,並在每種類型上分別實現它。

2

if ... else ...位移入foreach循環,因爲您的where選擇器正在返回一個集合,而不是單個人。

var currentPerson = people.person.Where(p => p.Forename + " " + p.Surname == pName); 

foreach (var person in currentPerson) 
{ 

    AccademicStaff accStaff = person as AccademicStaff; 
    if (accStaff != null) 
    { 
     label9.Text = accStaff.Forename + " " + accStaff.Surname; 
     label10.Text = accStaff.IdentificationNumber.ToString(); 
     label11.Text = accStaff.DateOfBirth; 
     label12.Text = accStaff.Address; 
     label13.Text = accStaff.Office; 
     label14.Text = accStaff.School; 
     label15.Text = accStaff.ModuleLeaderOf; 
     label16.Text = accStaff.ProgramLeaderOf; 
    } 
    else 
    { 
     // How the info is printed out if person selected in a member of Admin Staff 
     label9.Text = person.Forename + " " + person.Surname; 
     label10.Text = person.IdentificationNumber.ToString(); 
     label11.Text = person.DateOfBirth; 
     label12.Text = person.Address; 
     label13.Text = person.Office; 
     label6.Text = "Job Role"; 
     label14.Text = person.JobRole; 
    }  
} 

我去掉了調用Dispose()上的標籤,他們就沒有任何意義。

編輯你也許可以縮短它有點像這樣:

var currentPerson = people.person.Where(p => p.Forename + " " + p.Surname == pName); 

foreach (var person in currentPerson) 
{ 

    label9.Text = person.Forename + " " + person.Surname; 
    label10.Text = person.IdentificationNumber.ToString(); 
    label11.Text = person.DateOfBirth; 
    label12.Text = person.Address; 
    label13.Text = person.Office; 

    AccademicStaff accStaff = person as AccademicStaff; 
    if (accStaff != null) 
    { 
     label14.Text = accStaff.School; 
     label15.Text = accStaff.ModuleLeaderOf; 
     label16.Text = accStaff.ProgramLeaderOf; 
    } 
    else 
    { 
     label6.Text = "Job Role"; 
     label14.Text = person.JobRole; 
    }  
} 
0

它看起來像currentPerson是一個單一的實體不是一個集合。所以SingleFirstOrDefault也可以工作; (取決於currentPerson是否唯一)然後測試我喜歡使用is的類型,因爲它處理空檢查。因此,如果沒有任何標籤被填充,則意味着從linq語句返回的人員都不是這種類型。

people.CreatePeople(); 
    var currentPerson = people.person.Where(p => p.Forename + " " + p.Surname == pName).Single(); 

     if(currentPerson is AcademicStaff) 
     { 
      label9.Text = accStaff.Forename + " " + accStaff.Surname; 
      label10.Text = accStaff.IdentificationNumber.ToString(); 
      label11.Text = accStaff.DateOfBirth; 
      label12.Text = accStaff.Address; 
      label13.Text = accStaff.Office; 
      label14.Text = accStaff.School; 
      label15.Text = accStaff.ModuleLeaderOf; 
      label16.Text = accStaff.ProgramLeaderOf; 
     } 
     else if(currentPerson is AdminStaff) 
     { 
      label9.Text = admin.Forename + " " + admin.Surname; 
      label10.Text = admin.IdentificationNumber.ToString(); 
      label11.Text = admin.DateOfBirth; 
      label12.Text = admin.Address; 
      label13.Text = admin.Office; 
      label6.Text = "Job Role"; 
      label14.Text = admin.JobRole; 
      label7.Dispose(); 
      label8.Dispose(); 
      label15.Dispose(); 
      label16.Dispose(); 
     } 
相關問題