C# OOP problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I psoted a problem yesterday and I made a typo. I wish I can rephrase the question again correctly today.
Here is the sample code. I got error message: invalid argument of test method.
I also output the type of object e1 with e1.GetType() , and it is Manager.

Can someone give any advice ?

Class Employee
{ .....
}
Class Manager : Employee
{.....
}

public void test(Manager obj)
{.....
}

//Declare a new object.
Employee e1 = new Manager();

//Call test method
test(e1) ;
 
us a brute force:

test((Manager) e1);

Vlastik



Alison said:
Hi,

I psoted a problem yesterday and I made a typo. I wish I can rephrase the
question again correctly today.
 
Alison....

You pass references, not objects to methods. And you do so by value. In
your
sample code you are trying to pass a reference of type Employee when the
method expects a reference of Type Manager. It _is_ true that the actual
object on the heap is of class Manager, but that is not what counts
here. The
naming of the variable obj is misleading as it implies the reference is
of Type
object.

try changing the line
Employee e1 = new Manager();
to
Manager m1= new Manager();

Regards,
Jeff

Class Employee
{ .....
}
Class Manager : Employee
{.....
}

public void test(Manager obj)
{.....
}

//Declare a new object.
Employee e1 = new Manager();

//Call test method
test(e1) ;
 
Back
Top