Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip an immediate base class override function

Tags:

c#

oop

I have got the following class structure, and have plenty of classes like C derived from B, in some of the classes I dont want by B.OnShow() but I want A.OnShow() to be executed from C Any tricks?

abstract class A
{
   protected virtual void OnShow()
   {
        //some code
        base.OnShow();
   }
}

class B : A
{
    protected override void OnShow()
    {
      //some other code
      base.OnShow();
    }
}

class C : B
{
   protected override void OnShow()
   {
      //some other code
      base.OnShow();
   }
}
like image 714
Jobi Joy Avatar asked Oct 17 '25 11:10

Jobi Joy


1 Answers

Don't have C extend from B, instead make a new class "X" that extends from A and has the parts that are currently in B that you want for both B and C and then have C and B extend from "X". Then B will just have the bits in it that you want for B specifically (a bit abstract, but I think that makes sense).

Generally speaking, if you are trying to skip a parent method then you are doing something wrong.

like image 117
TofuBeer Avatar answered Oct 19 '25 00:10

TofuBeer