How to convert object from class B to class A?

I get InvalidCastException when trying to convert Employee object to Manager object? See attached code snippet.

How should I write Employee and Manger classes, so they are convertible to both directions?


class Program
{
    static void Main(string[] args)
    {
        // This gives you InvalidCastException...
        Manager mgr = (Manager)new Employee("Johnsson");
        Console.ReadLine();
    }
}
 
public interface IEmployee
{
    string Name { get; set; }
}
 
public class Employee : IEmployee
{
    public string Name { get; set; }
    public Employee(string name)
    {
        Name = name;
    }
}
 
public class Manager : Employee
{
    public Manager(string name) : base(name) { }
}

Open in new window

j_lainioAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

philipjonathanCommented:
You cannot cast a superclass into its subclass, unless the actuall object type is the subclass.

However, most of the time what you need is to cast the subclass into its superclass:
Employee emp = new Manager("Johnson");

This way you can treat all employee as a whole.
Let's say you want to increase salary, then:
foreach (Employee emp in employees)
  emp.IncreaseSalary();

Only if you really know that the actual object is the subclass, then you can cast to the subclass:
foreach (Employee emp in employees)
{
  if (emp is Manager)
  {
    Manager mgr = (Manager) emp; // this will not generate exception
    mgr.GiveBiggerCubicle();
  }
}

If you tell us what you really need, probably we can help you better
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quincydudeCommented:
I think it's not really possible as you inherit Manager from  Employee and Employee from IEmployee. So in other words, Manager is a kind of employee and employee is a kind of IEmployee. But you cannot say employee is a kind of manager.

You can type cast them both employee and manager to iemployee , though.
0
Anurag ThakurTechnical ManagerCommented:
you cannot get the Manager object by calling the Employee constructor and its correct you will get the invalid cast exception

// This gives you InvalidCastException...
Manager mgr = (Manager)new Employee("Johnsson");
Console.ReadLine();

this is correct
Manager mgr = new Manager("Johnsson");

or
Employee mgr = new Manager("Johnsson");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.