10

Possible Duplicate:
How to call base.base.method()?

I have some trouble with Inheritance in C#. I've sketched three classes: A, B and C. C inherits from B and B from A. Now the B class calls base.Method1 and works fine but I can't call A.method1 from the C class. If I call base.Method1 from C obviously that method will be method1 of B. Any advice?

P.S. in A class there are some fields marked private so you can access them only

class A
{    
    private instance;    
    public virtual void Method1 ()
    {       
        instance = this;
        do something;       
    }
}

class B : A
{
    public override void Method1()
    {
        base.Method1();
        do something;       
    }
}

class C : B
{   
    public override void Method1 ()
    {
        //need A Method1 then do something
    }
}
Community
  • 1
  • 1
Pasquale Sada
  • 327
  • 2
  • 4
  • 12
  • why would you want to do that? If you describe your problem it will be easier to help you. – Tomas Jansson Aug 21 '12 at 10:01
  • 2
    Well maybe then you should be more interested in `C : A` then ! – V4Vendetta Aug 21 '12 at 10:01
  • While .net allows this, C# does not. And it's a bad idea anyways. – CodesInChaos Aug 21 '12 at 10:05
  • `A c = new C(); c.Method1();` this calls `A.Method1()` but when you say `C c = new C(); c.Method1();` it calls `C.Method1()` – Mahdi Tahsildari Aug 21 '12 at 10:08
  • @Codes: A comment on an answer on the question I linked suggests that it's no longer possible to do this in .NET - do you know whether it is or not? (I'll readily admit I have no idea, I'm just curious.) – Rawling Aug 21 '12 at 10:09
  • @Rawling I think calling grand-parents is still possible, calling them on classes that aren't your ancestors isn't. There are some weird binary compatibility scenarios related to this. – CodesInChaos Aug 21 '12 at 10:12
  • @mahditahsildari I'm not sure exactly what you mean, but since `Method1` is _virtual_, the same implementation is called in both cases. Maybe your comment would be more relevant if `Method1` was `new` in the derived class, instead of `override`. – Jeppe Stig Nielsen Aug 21 '12 at 10:19
  • @MahdiTahsildari `A c = new C(); c.Method1(); this calls A.Method1()` -- to reinforce what Jeppe said, no, it doesn't, and that's the whole point of polymorphism and virtual functions. – Jim Balter Apr 21 '16 at 21:55

3 Answers3

6

This is a feature of C#. If you wish to expose this method to class C, consider refactoring A like so:

class A
{    
  private instance;    
  public virtual void Method1 ()
  {
    AMethod1();       
  }

  protected void AMethod1()
  {
    instance = this;
    do something;       
  }
}

This will enable you to call this method from within C:

class C : B
{   
  public override void Method1 ()
  {
    AMethod1();
    // do something;
  }
}
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
4

It is not possible to do this in C#, thought its possible to do this via IL.

For your case, you may do something like this:

class A
{
    private int instance;
    public virtual void Method1()
    {
        Method1Impl();
    }

    protected void Method1Impl()
    {
    }
}

Now you can call A.Method1Impl from your C class.

logicnp
  • 5,796
  • 1
  • 28
  • 32
1

You cannot skip directly to A using base.Method1, because that's just for the one above. However, you can use a protected delegate or reference to A to make a direct call. Make a protected field Action _A_Method1 and in the constructor of A, set it to Method1 (_A_Method1 = Method1). Now you can use a direct call to that from class C (_A_Method1();).

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Deathspike
  • 8,582
  • 6
  • 44
  • 82
  • You can enclose inline code between backticks `, so it gets formatted as code – CodesInChaos Aug 21 '12 at 10:07
  • That's clever, though clunky. – Jim Balter Apr 21 '16 at 21:59
  • The problem with this is, if _A_Method1() is virtual, you will get a stack overflow exception. – John C Jul 24 '20 at 20:09
  • So you might as well have a protected BaseMethod() and have _A_Method1() call BaseMethod(). Then, in your grandchild, you can just call BaseMethod() in _A_Method1().. And the parent, you can override _A_Method1() and do what it wants. – John C Jul 24 '20 at 20:16