Hi.
Can anybody suggest how I may invoke the base-class implementation of an interface method from a derived class that also implements (overrides) the same interface method
If I have this short and simplified situation:
interface IMyInterface
{
void MyMethod1();
void MyMethod2();
}
class HelperClass : IMyInterface
{
void IMyInterface.Method1()
{ /* Some default implementation */ }
void IMyInterface.Method2()
{ /* Some default implementation */ }
}
class MyMainClass : HelperClass, IMyInterface
{
void IMyInterface.Method1()
{
/* Some specific implementation */
How can I call the base class implementation of this interface
method (i.e. HelperClass.IMyInterface.Method1() )
// This generates a compiler error.
base.IMyInterface.MyMethod1();
// This calls the MyMainClass implementation of the method.
((IMyInterface)base).MyMethod1();
// This also calls the MyMainClass implementation of the method.
((IMyInterface)this).MyMethod1();
}
}
I know I should normally not use multiple inheritance when implementing interfaces. Helper classes, though, are commonly used in this respect, to provide default implementation for popular interfaces. One way to work around this, of course, is to have only the MyMainClass implement the interface, and delegate calls that it does not wish to implement itself into the helper class from which it derives. This, however, is a cumbersome solution which I would like to avoid.
So, does anybody know of a syntax that should enable me to call the base-class implementation of an interface method from within a derived class that also implements that interface method
Thanks,
Eudi.

Calling base-class implementation of interface methods
Vile
The methods in HelperClass that implement the interface are by default non-virtual. You have to explicitly mark it as virtual if you want it that way.
sunrunner
Simple solution would be, that you construct the helper class that implicitly implements the interface, with virtual methods. Then you have two options when building new classes: 1) Implement the interface directly and 2) Deriving from the helper class. Isn't this the functionality you seek
Ok. here's how I would do it:
interface IMyInterface
{
void Method1();
void Method2();
}
class HelperClass : IMyInterface
{
public virtual void Method1()
{ /* Some default implementation */ }
public virtual void Method2()
{ /* Some default implementation */ }
}
class MyMainClass : HelperClass
{
public override void Method1()
{
base.Method1();
base.Method2();
}
}
PS. Aren't the the helper classes ofter called as "Bases" in .NET For example if you have a class Wonder, the helper would be called the WonderBase.
pwhitaker
Dear rauhanlinnake,
Thank you for your quick and elaborate reply. After playing around with the code, I actually realized that the problem was that I implemented the interface explicitly in the helper class. This prefixed the method name with the interface name. By doing this, I syntactically prevented calling of the method from the derived class.
What I actually did was this (I seem to have over-simplified the sample code that I provided...):
class HelperClass : IMyInterface
{
void IMyInterface.Method1()
{ /* Default implementation */}
}
class MainClass : HelperClass : IMyInterface
{
void IMyInterface.Method1()
{
/* Specific implementation */
// The following line generated a compiler error.
base.IMyInterface.Method1();
}
}
So all I really had to do was remove the explicit declaration of the method in both classes:
class HelperClass : IMyInterface
{
void Method1()
{ /* Default implementation */}
}
class MainClass : HelperClass : IMyInterface
{
void Method1()
{
/* Specific implementation */
// The following line generated a compiler error.
base.Method1();
}
}
And this works. By the time I had this working, I received your reply, so I apologize for taking up your time.
By the way, as far as I am aware, interface methods are by definition virtual. Or am I misunderstanding this
Also, the explicit method declaration is important in case the class implements multiple interfaces, where two or more interfaces use the same method name. While this is no healthy practice, and not something I intend to do, the language syntax still seems to miss something if I cannot use the syntax base.IMyInterface.Method1().
Thank you again for your reply,
Eudi.