A C# OOP problem

G

Guest

Hi, there,

I was doing experiment of C# OOP concepts and found a situation I can't figure it out.
I have a base class Employee and a derived class Manager. I also have a method away from the two classes, test, which takes one parameter:an instance of Employee. I instantiated an object with Employee emp1 = new Employee(); and called test(emp1), it worked perfectly. But as I instantiated another object emp2 with Employee emp2 = new Manager( ); and call test( emp2), I got error message invalid argument of test( ) method.

So I think the problem is associated with the difference between the two statements below. Can someone illuminate me?
Employee emp1 = new Employee();
Employee emp2 = new Manager();

Sample code:
Public class Employee
{
....;
}
public class Manager : Employee
{
......;
}

public void Test ( Employee emp )
{
Console.Write("...");
}
 
K

Klaus H. Probst

That code looks like it should run correctly. Maybe you're omitting
something? The Test() method should not throw an exception just on the
argument type, since both objects are a 'type of' Employee.


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Alison said:
Hi, there,

I was doing experiment of C# OOP concepts and found a situation I can't figure it out.
I have a base class Employee and a derived class Manager. I also have a
method away from the two classes, test, which takes one parameter:an
instance of Employee. I instantiated an object with Employee emp1 = new
Employee(); and called test(emp1), it worked perfectly. But as I
instantiated another object emp2 with Employee emp2 = new Manager( ); and
call test( emp2), I got error message invalid argument of test( ) method.
So I think the problem is associated with the difference between the two
statements below. Can someone illuminate me?
 
G

Guest

You could cast it into employee to be sure

e.

Manager myManager = new Manager()
test((employee) myManager)

But thats all i can think of, as the others said, it should work
What exactly is the error? Can we see the test method and employee and manager classes?
 
C

Champika Nirosh

you have taken it wrong Jax

it is not
Manager myManager = new Manager();
infact
Employee emp2 = new Manager();

As you said code has no error at all

using System;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Employee emp1 = new Employee();
Test(emp1);
Employee emp2 = new Manager();
Test(emp2);
Console.ReadLine();
}
public static void Test(Employee emp)
{
Console.Write("Hello .....");
}
}
public class Employee
{
}
public class Manager : Employee
{
}
}

Nirosh.

Jax said:
You could cast it into employee to be sure.

e.g

Manager myManager = new Manager();
test((employee) myManager);

But thats all i can think of, as the others said, it should work.
What exactly is the error? Can we see the test method and employee and
manager classes?
 
G

Guest

Hi, I'm sorry. The parameter test method accepts should be Manager Object instead of Employee object
 
C

Champika Nirosh

Please ask your question again .. if that is the case even "I instantiated
an object with Employee emp1 = new Employee(); and called test(emp1), it
worked perfectly." also not working

Nirosh.
 
K

Klaus H. Probst

That won't work, because your inheritance chain dictates that while a
Manager "is an" Employee, an Employee is not necessarily a Manager. C# won't
recognize the Employee instance as being also a manager, but it will work
the other way around, as your original example was specified.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top