How to pass a parameter of a derived class

G

Guest

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){}
 
J

Jon Skeet [C# MVP]

Steve Teeples said:
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
 
V

Vadim Berezniker

Steve said:
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){}

void DoSomething(A xxx) {}
will do
 
G

Guest

The input from both of you was very helpful. My issue was resolved - nicely.
Thanks.
 

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