C# Inheritance & Casting
Solution 1
You can cast a subtype to its base type. But you are casting an instance of the base type to the subtype.
An EmployeeProfile is-an Employee. Not necessarily the other way around.
So this would work:
EmployeeProfile prof = new EmployeeProfile();
Employee emp = prof;
However, this model reeks of bad design. An employee profile is not a special kind of an employee, is it? It makes more sense for an employee to have a profile. You are after the composition pattern here.
Solution 2
All the answers are correct...just providing a no frills simple explanation...
class Employee
class Female : Employee
class Male: Employee
Just because you are an Employee
does not make you a Female
...
Solution 3
Maybe my brain is burned out, but I thought you can cast a subtype to its base type?
You are attempting to cast a basetype to its subtype. Exactly the opposite of what you say:
Employee emp = new Employee();
EmployeeProfile prof = emp;
Steven Striga
.NET & Sitecore Developer that loves to design well engineered websites.
Updated on July 21, 2022Comments
-
Steven Striga almost 2 years
I get the following exception:
InvalidCastException: Unable to cast object of type 'Employee' to type 'EmployeeProfile'.
I have the following code:
private class Employee { public string Name { get; private set; } public Employee() { this.Name = "employee"; } public override string ToString() { return this.Name; } } private class EmployeeProfile : Employee { public string Profile { get; private set; } public EmployeeProfile() : base() { this.Profile = string.Format("{0}'s profile", this.Name); } public override string ToString() { return this.Profile; } } public void RunTest() { Employee emp = new Employee(); EmployeeProfile prof = (EmployeeProfile)emp; // InvalidCastException here System.Console.WriteLine(emp); System.Console.WriteLine(prof); }
Maybe my brain is burned out, but I thought you can cast a subtype to its base type? What am I missing here? Maybe it is a vacation... thank you!
-
Steven Striga over 13 yearsI made a small mistake in my post - please see the updated like: EmployeeProfile prof = (EmployeeProfile)emp; // InvalidCastException here
-
cdhowie over 13 years@WeekendWarrior: That doesn't matter. You still can't cast an instance of the base type to the derived type. You could cast in the other direction.
-
Asongfac Roland over 13 yearsWhat cdhowie says still stands, you are still trying to cast a basetype into a subtype.
-
Steven Striga over 13 yearsThis is an excellent way of thinking about it... this helps a lot. thanks so much.
-
Steven Striga over 13 yearsComposition pattern makes sense. EmployeeProfile has a one-to-one relationship with an Employee - it just contains additional properties. thank you for the tip!
-
cdhowie over 13 years@WeekendWarrior: Sure, no problem.
-
Alex almost 6 yearsif you had a list of Employee and Male and Female had unique properties, how would you count for example, how many Females have given birth? or how many Males have had a vasectomy? LoL trying to come up with examples following the classes mentioned. I need to know how to cast appropriately down to their type to get to the unique properties. THANKS!
-
JasonInVegas over 5 yearsIs is not strictly true that you "need" a method in the derived class which assigns all properties from the base class. This is a long-winded way of performing the assignment of properties whereas an object-level assignment will work for all available, matching properties.
-
A.R.SEIF over 2 yearsnot worked for me