2

I have three types:

  1. Patient
  2. Inpatient : Patient
  3. Outpatient : Patient

I have a method that fills the base patient and all it's properties: PatientRepository.FillPatient() and returns a Patient object.

I then need to check and see what the Patient type is and downcast to either an Outpatient or Inpatient.

When I try to downcast, it is throwing Unable to cast object of type 'Patient' to type 'Inpatient'. This is a run-time error.

if (patient.Type == PatientType.Inpatient)
{
    var inpatient = (Inpatient)patient;             
    return inpatient;
}

public enum PatientType
{
    Inpatient, Outpatient
}

I have no idea why. Am I fundamentally doing something wrong here?

Brian
  • 5,069
  • 7
  • 37
  • 47
Chace Fields
  • 857
  • 2
  • 10
  • 20
  • Is this a runtime or compile-time error? – Daniel Brückner Jan 23 '13 at 18:31
  • Is Patient Type a property of your class? Or is it its Type? What is `PatientType`, an `enum` of Types? – Nate-Wilkins Jan 23 '13 at 18:31
  • http://stackoverflow.com/questions/729527/is-it-possible-to-assign-a-base-class-object-to-a-derived-class-reference-with-a – JonH Jan 23 '13 at 18:31
  • 1
    The key is FillPatient() It should create the correct Patient type not simply the base class – Steve Jan 23 '13 at 18:31
  • Possible duplicate: http://stackoverflow.com/questions/988658/unable-to-cast-from-parent-class-to-child-class – msmucker0527 Jan 23 '13 at 18:32
  • If you have a base `Patient` instantiated as `new Patient()`, it is not one of the derived types and that is why your cast fails. Your cast is not changing the type of the object, but merely the type of the reference. If your object is not actually that type, the cast will fail! But since you didn't provide the code that actual deals with your base class, I am merely speculating as to what you have done. – Anthony Pegram Jan 23 '13 at 18:36
  • Possible duplicate of [Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?](http://stackoverflow.com/questions/729527/is-it-possible-to-assign-a-base-class-object-to-a-derived-class-reference-with-a) – Lithium Mar 31 '17 at 08:39

2 Answers2

3

Your FillPatient should return the specific class right way:

public static Patient FillPatient()
{
    if (something) {
        return new InPatient();
    }
    else {
        return new OutPatient();
    }
}

Then you can downcast it to a specific class

Patient patient = PaitentRepository.FillPatient();

if (patient is InPatient) {
    ...
}
else {
    ...
}

Note: most of the times when a class is the base for N other classes, that base class (Patient at your case) would be abstract.

Joao
  • 7,366
  • 4
  • 32
  • 48
1

Because the question says "throwing" I will assume this is a runtime error. This in turn means that PatientRepository.FillPatient() returns an object of type Patient and somewhere contains new Patient(). You could mark the base class Patient as abstract and should then get some compiler errors.

If you want to cast to a subtype at some point PatientRepository.FillPatient() has to create instances of this subtypes. So instead of new Patient() you have to use new Inpatient()and new Outpatient() depending on the type of patient you are trying to populate.

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143