How to pass a parameter of a derived class

  • Thread starter Thread starter Guest
  • Start date Start date
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){}
 
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
 
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
 
Back
Top