virtual, override, new ... little question

F

Frank Uray

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");
}
}
 
P

Peter Morris

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
 
M

Marc Gravell

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");
}
}
 
J

Jon Skeet [C# MVP]

Frank Uray said:
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 !

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.
 
P

Paul E Collins

Frank Uray said:
[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.
 
P

Paul E Collins

Paul E Collins said:
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.
 

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