4

I have this:

public class Base {
   public virtual void Connect()
   {
      // do stuff
   }
}

public class Derived1 : Base {
   public override void Connect()
   {
       base.Connect();
      // do other stuff
   }
}

public class Derived2 : Derived {
   public override void Connect()
   {
      base.Connect() // Here I want to call Base::Connect(), not Derived::Connect()
   }
}

Is there a way I can call Base::Connect from Derived2, because I want to skip the "do other stuff" part from Derived1::Connect() ?

edit: Its mandatory that I derived from Derived1.

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • I never tried to do something like this, but have you tried `base.base.Connect()`? – Nolonar Jun 19 '13 at 16:27
  • Can you change `Base` or `Derived1`? – Mike Zboray Jun 19 '13 at 16:28
  • @Nolonar: That won't work. – poy Jun 19 '13 at 16:30
  • I can change Derived1 and if there is no other way possible Base – Adrian Jun 19 '13 at 16:30
  • possible duplicate of [How to call base.base.method()?](http://stackoverflow.com/questions/2323401/how-to-call-base-base-method) – Mike Zboray Jun 19 '13 at 16:32
  • 1
    Why not just have Derived2 extend Base? It doesn't seem to naturally extend Derived1 here. If there is a lot of common code, you might need an abstract BaseDerived that both Derived1 and Derived2 extend. – brianestey Jun 19 '13 at 16:33
  • From Mr. Lippert's stackoverflow post on this topic (AKA horse's mouth): http://stackoverflow.com/a/2327821/426422 – Mike Cheel Jun 19 '13 at 16:31
  • possible duplicate of [How to call a second-level base class method like base.base.GetHashCode()](http://stackoverflow.com/questions/1006530/how-to-call-a-second-level-base-class-method-like-base-base-gethashcode) – nawfal May 02 '14 at 16:09

2 Answers2

3

Yo can try splitting Connect into two functions and call DoConnect from all derived classes, where it is needed:

public class Base {
   public virtual void Connect()
   {
      DoConnect();
   }

   protected void DoConnect()
   {
      // do stuff
   }
}

...

public class Derived2 : Derived1 {
   public override void Connect()
   {
      DoConnect();
      ...
   }
}

If you cant update the base class, you can do this splitting at Derived1

public class Derived1 : Base {
   public virtual void Connect()
   {
      DoConnect();
   }

   protected void DoConnect()
   {
      base.Connect();
   }
}
alex
  • 12,464
  • 3
  • 46
  • 67
  • I know the code is bad, but because I cannot change much of it, your solution is what I need. – Adrian Jun 19 '13 at 16:37
1

C# dose not provide direct way to call base.base.method() you need first to change your design.
kindly check this answer why-c-sharp-doesnt-support-base-base

So you need to change your design little to make little trick or workaround.

  1. Define a new method in your class Derived1 that it's only work is to call base.Connect()
  2. Then in your class Derived2() define your Connect() that 'll simply call the previous function defined in the previous step.

check this example:

public class Base {
    public virtual void Connect() {
        // do stuff
    }
}

public class Derived1: Base {
    public override void Connect() {
        base.Connect();
        // do other stuff
    }
    public void CallBaseConnnect() {
        //here make only one call to base.Connect(). 
        //that's how class Derived1 'll provide you away to call base.Connect().
        base.Connect();
    }
}

public class Derived2: Derived {
    public override void Connect() {
        //here just make a call to CallBaseConnnect() in base class Derived1
        //that 'll equivalent to base.base.Connect.
        base.CallBaseConnnect();
    }
}
Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60