Home All Groups Group Topic Archive Search About

How to call a method defined in grandparent class?

Author
26 Nov 2007 1:52 AM
Curious
I'll need something like:

           base.base.Show();

However, this doesn't work. The grandparent class name is "BaseForm".
What's the correct syntax to call the "Show" method defined in the
"BaseForm" class?

Author
26 Nov 2007 6:58 AM
bj7lewis
>           base.base.Show();
Well if you can send this ref to a function and the function can cast past
reference to any base type say Object like show in MSDN - Textbox Class
Page...

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Windows.Forms.Control
        System.Windows.Forms.TextBoxBase
          System.Windows.Forms.TextBox

So Here TextBox is like your derived class so lets access TextBox's
grandparent... TextBox.Text proptery is defined in base class Control so
calling...

....
    //in some function...
    TextBox TextBoxObj = new TextBox();
    ... // Setup other TextBoxObj members and add to parent control's
children collection...
    TextBoxObj.Text = "Something...";
....

[or we can do the same thing with this...]

....
    ...
    ((Control)TextBoxObj).Text = "Something...";
....

This works cause you cast TextBoxObj ref to a Control ref and access Text
thur Control ref...

So now to access your derived grandparent base class you can use...

....
    //in some function...
    //base.base.Show();
    ((GRANDPARENTBASECLASSHERE)TextBoxObj).Show();
....

Also there is nothing wrong with just calling Show() alone unless virtual
overrides or name ambiguity get in the way like...

....
    //base.base.Show();
    Show();
....

Hope that helps...
Author
26 Nov 2007 12:44 PM
Jon Skeet [C# MVP]
On Nov 26, 6:58 am, "bj7lewis" <bj7le***@rio.com> wrote:

<snip>

> So now to access your derived grandparent base class you can use...
>
> ...
>     //in some function...
>     //base.base.Show();
>     ((GRANDPARENTBASECLASSHERE)TextBoxObj).Show();

No, that won't do it, because overriding is done at runtime, not
compile time. If a method is overridden in a child class, the
grandchild cannot access the original behaviour.

Jon
Author
26 Nov 2007 1:03 PM
Lasse_Vågsæther_Karlsen
Jon Skeet [C# MVP] wrote:
Show quote
> On Nov 26, 6:58 am, "bj7lewis" <bj7le***@rio.com> wrote:
>
> <snip>
>
>> So now to access your derived grandparent base class you can use...
>>
>> ...
>>     //in some function...
>>     //base.base.Show();
>>     ((GRANDPARENTBASECLASSHERE)TextBoxObj).Show();
>
> No, that won't do it, because overriding is done at runtime, not
> compile time. If a method is overridden in a child class, the
> grandchild cannot access the original behaviour.
>
> Jon

Which, IMO, is a good thing. Otherwise, the contract that the
child-class guarantees with its implementation goes out the window if a
descendant could skip its code entirely.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Author
26 Nov 2007 1:28 PM
Jon Skeet [C# MVP]
On Nov 26, 1:03 pm, Lasse Vågsæther Karlsen <la***@vkarlsen.no> wrote:

<snip>

> > No, that won't do it, because overriding is done at runtime, not
> > compile time. If a method is overridden in a child class, the
> > grandchild cannot access the original behaviour.
>
> Which, IMO, is a good thing. Otherwise, the contract that the
> child-class guarantees with its implementation goes out the window if a
> descendant could skip its code entirely.

Absolutely - it's part of encapsulation, effectively.

Jon
Author
26 Nov 2007 11:30 PM
Alun Harford
Curious wrote:
> I'll need something like:
>
>            base.base.Show();
>
> However, this doesn't work. The grandparent class name is "BaseForm".
> What's the correct syntax to call the "Show" method defined in the
> "BaseForm" class?

You can't. And you shouldn't. But the IL hacker in me can't resist
pointing out that you can!

Basically, the idea is to make a non-virtual call to a virtual method
(eugh!). I've included some sample code, but be warned that I've not
tested it and it's late here.

Every time you use this code, an OO purist kills a puppy.

delegate void ShowDelegate();
public void GrandParentShow() {
    DynamicMethod show = new DynamicMethod(
        "_Show",
        null,
        new Type[0],
        typeof(Program).Module); //This is just a reference to
    // your module. You don't have to use typeof(Program)


    ILGenerator il = show.GetILGenerator();
    il.Emit(OpCodes.Ldarg_0);

    //You could get the current type and call
    //currentType.Parent.Parent instead of
    //hardcoding the grandparent
    il.Emit(OpCodes.Call, typeof(BaseForm).GetMethod("Show"));
    il.Emit(OpCodes.Ret);

    ShowDelegate del =
        (ShowDelegate)show.CreateDelegate(typeof(ShowDelegate));
    del();
}

Alun Harford

AddThis Social Bookmark Button