473,383 Members | 1,785 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,383 software developers and data experts.

How to pass a parameter of a derived class

I have a base class 'A' that contains most of the functionality that
I want. However, it is necessary to derive classes from the base for some
unique behavior I require. I want to call a routine that will accept a
reference of any derived class of A. I don't know in C# how this is done.
Can someone help me? What would be the parameter for the DoSomething(ref
xxx) below?

public class A {}
public class B : A {}
public class C : A {}

void DoSomething(ref xxx){}
------
Steve
Nov 16 '05 #1
3 1751
Steve Teeples <St**********@discussions.microsoft.com> wrote:
I have a base class 'A' that contains most of the functionality that
I want. However, it is necessary to derive classes from the base for some
unique behavior I require. I want to call a routine that will accept a
reference of any derived class of A. I don't know in C# how this is done.
Can someone help me? What would be the parameter for the DoSomething(ref
xxx) below?

public class A {}
public class B : A {}
public class C : A {}

void DoSomething(ref xxx){}


It sounds like you probably just want to make A abstract, and then just
declare DoSomething to accept an A. You probably *don't* want to use
"ref" though. Read the following to make sure you understand
pass-by-reference properly:

http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Steve Teeples wrote:
I have a base class 'A' that contains most of the functionality that
I want. However, it is necessary to derive classes from the base for some
unique behavior I require. I want to call a routine that will accept a
reference of any derived class of A. I don't know in C# how this is done.
Can someone help me? What would be the parameter for the DoSomething(ref
xxx) below?

public class A {}
public class B : A {}
public class C : A {}

void DoSomething(ref xxx){}
------
Steve


void DoSomething(A xxx) {}
will do
Nov 16 '05 #3
The input from both of you was very helpful. My issue was resolved - nicely.
Thanks.

"Steve Teeples" wrote:
I have a base class 'A' that contains most of the functionality that
I want. However, it is necessary to derive classes from the base for some
unique behavior I require. I want to call a routine that will accept a
reference of any derived class of A. I don't know in C# how this is done.
Can someone help me? What would be the parameter for the DoSomething(ref
xxx) below?

public class A {}
public class B : A {}
public class C : A {}

void DoSomething(ref xxx){}
------
Steve

Nov 16 '05 #4

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

Similar topics

3
by: Dave | last post by:
Hello all, Quoting from page 24 of "The Boost Graph Library; User Guide and Reference Manual": "It turns out that by the contravariance subtyping rule, the parameter type in the derived...
9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
8
by: Tom Wilson | last post by:
Is there a way to pass a derived object so that the called function thinks it is the base class? Here's the lowdown. class Hull { // .... virtual int DoIt() { TRACE("Hull"); } // ....
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
3
by: sloan | last post by:
How does one "pass thru" a Raised Event.... I am using the Adapter Pattern to sync up some different interfaces. http://www.dofactory.com/Patterns/PatternAdapter.aspx My Question is this:
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
3
by: alan | last post by:
Hello all, I'm wondering if it's valid for a templated class to use a template parameter as a base class. Basically I have two abstract types, one of which is derived from the other, and I have...
4
by: Andy Champ | last post by:
What I want to do (and can't...) is Template <typename Tclass base { // whatever } class derived: public base<derived::myVals> { Enum myVals
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.