title: See if something is of type class
body: <p>I am trying to check to see if something is a certain type of class in c#. It prints out certain labels on a form depending on what class it is a type of. This is what I have so far and it works for the "if" statement. However, I get an error of "unable to cast object of type". Is it possible to use an if-else statement in this scenario?</p>
  
  <pre><code>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 =&gt; 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();
      }
    }
  }
  </code></pre>