473,385 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# OOP problem

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) ;
Nov 15 '05 #1
2 1057
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) ;

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #2
us a brute force:

test((Manager) e1);

Vlastik

"Alison" <an*******@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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) ;

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.