Hi all
I have some little problems with inheriting ...
What I have and what I want to do is:
- Base class with one method "RunTest"
- Standard class inherits from Base class
- In Standard class I want to add more code to method "RunTest"
- I wnat to make sure method "RunTest" from Base class is also executed
without having "base.RunTest" in Standard class
I hope you know what I mean ... :-))
Is there any way to do this ??
Thanks for any comments !
Best regards
Frank
Here is my sample code:
public class clsBase
{
public virtual void RunTest()
{
Console.WriteLine("clsBase");
}
}
public class clsStandard : clsBase
{
public override void RunTest()
{
//base.RunTest(); I dont want to have this extra line !!
Console.WriteLine("clsStandard");
}
}
Peter Morris - 04 Mar 2008 16:04 GMT
In short. No you can't.
However, you could do this
public abstract class BaseClass
{
private void InternalRunTest()
{
//Code here
RunTest();
//Code here
}
public abstract void RunTest();
}
Then in BaseClass always use InternalRunTest().
Pete
Marc Gravell - 04 Mar 2008 16:04 GMT
Split the method into 2 parts; the public RunTest method runs the fixed
(base) parts, and the virtual bit - thus the subclass can only change the
virtual part; the fixed part must always run:
public class clsBase
{
public virtual void RunTest()
{
Console.WriteLine("clsBase");
RunTestInner();
}
protected virtual void RunTestInner() { }
}
public class clsStandard : clsBase
{
protected override void RunTestInner()
{
Console.WriteLine("clsStandard");
}
}
Jon Skeet [C# MVP] - 04 Mar 2008 16:09 GMT
> I have some little problems with inheriting ...
> What I have and what I want to do is:
[quoted text clipped - 7 lines]
> Is there any way to do this ??
> Thanks for any comments !
One way is to make RunTest non-virtual, but make it call a virtual (or
possibly even abstract) method which the derived class then overrides.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Paul E Collins - 04 Mar 2008 16:15 GMT
> [want to do base.Method() without writing that line]
You can't make the compiler magically perform the base call without
being told to.
However, you might be able to fake it by restructuring your code:
// in the base class:
public virtual void RunTest()
{
BeforeRunTest();
Console.WriteLine("base class");
}
protected abstract void BeforeRunTest();
// ...and in the inheriting class:
protected virtual void BeforeRunTest()
{
Console.WriteLine("inheriting class");
}
Eq.
Paul E Collins - 04 Mar 2008 16:17 GMT
> protected abstract void BeforeRunTest();
I just noticed that your base class isn't abstract, so you'd replace
that line with this one:
protected virtual void BeforeRunTest() { }
Eq.
Jeff Louie - 04 Mar 2008 18:37 GMT
As John suggest, this sounds like you are trying to defer the final
implementation to a more knowledgeable class.
http://www.geocities.com/jeff_louie/OOP/twisted2.htm
Regards,
Jeff