how to invoke overriden base class members from an instance of a derived class  
Author Message
rgreene2006





PostPosted: Visual C# Language, how to invoke overriden base class members from an instance of a derived class Top

Is there a way in C# to obtain the behavior analogous to that shown below in C++

class MyBase
{
public:
virtual void foo() {printf("base class");}
};

class MyDerived : public MyBase
{
public:
virtual void foo() {printf("derived class");}
};

main()
{
MyDerived d;
d.foo(); // prints "derived class"
d.MyBase::foo(); // prints "base class"
}

In C#, I could use base.foo() within the definition of MyDerived class, but is there any way to invoke MyBase.foo() from an instance of MyDerived



Visual C#6  
 
 
OmegaMan





PostPosted: Visual C# Language, how to invoke overriden base class members from an instance of a derived class Top

Here is one way of doing it. Get a reference to the base object and call it directly. Note, I have not specified that the operation is virtual....otherwise you will need to expose a property in the derived class and call the base explicitly with base.Foo()




public class IAmABase
{
public string Foo() { return "Base"; }
}

class IamDerived : IAmABase
{
public new string Foo() { return "Derived";}
}

static void Main(string[] args)
{

IamDerived IAD = new IamDerived();

Console.WriteLine(IAD.Foo()); // Derived.

IAmABase IAB = IAD as IAmABase;

if (IAB != null)
Console.WriteLine(IAB.Foo()); // Prints Base
}





 
 
Peter Ritchie





PostPosted: Visual C# Language, how to invoke overriden base class members from an instance of a derived class Top

C# is different than C++ in the way that it deals with access to base classes. You don't "inherit" a virtual method, per se, in C#, you either override it completely (via the "override" keyword) meaning the only access to that method is through the derived class and only the derived class then has access to the base method; or, you use the "new" keyword (as OmegaMan detailed) to imply that all access of that method through a reference to the derive class will be used--access through the base will go through the base class...

Out of curiosity, why would you want to do this in C++ or C#



 
 
rgreene2006





PostPosted: Visual C# Language, how to invoke overriden base class members from an instance of a derived class Top

OmegaMan & Peter:

I don't have a specific need to do this right now, though I've used this construct in the past in C++ when I had a collection of instances of some hierarchy of derived classes and occasionally wanted to access their common base class’s behavior, instead of the specialized behaviors of the derived ones.

I'm just learning C# and noticed that simply casting a derived instance to the base class didn’t give the behavior I’d expected based on my C++ experience and that there doesn’t seem to be a C# equivalent to C++’s scope resolution operator (::), so I was wondering how I could achieve the same result in C#. Your answers make it clearer what I’d need to do, and yours, Peter, helps to clarify why that’s so.

Thanks!