Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

virtual, override, new ... little question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank Uray - 04 Mar 2008 15:54 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.