By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,451 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,451 IT Pros & Developers. It's quick & easy.

A C# OOP problem

P: n/a
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("...");
}

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
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("...");
}

Nov 15 '05 #2

P: n/a
All should work right.
Please give all code.
Nov 15 '05 #3

P: n/a
Jax
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?
Nov 15 '05 #4

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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?
Nov 15 '05 #5

P: n/a
Hi, I'm sorry. The parameter test method accepts should be Manager Object instead of Employee object
Nov 15 '05 #6

P: n/a
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.

"Alison" <an*******@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
Hi, I'm sorry. The parameter test method accepts should be Manager Object instead of Employee object.

Nov 15 '05 #7

P: n/a
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.

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

"Alison" <an*******@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
Hi, I'm sorry. The parameter test method accepts should be Manager Object instead of Employee object.

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.