How to call a method defined in grandparent class?

How to call a method defined in grandparent class?

am 26.11.2007 02:52:58 von Fir5tSight

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?

Re: How to call a method defined in grandparent class?

am 26.11.2007 07:58:55 von 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...

Re: How to call a method defined in grandparent class?

am 26.11.2007 13:44:37 von skeet

On Nov 26, 6:58 am, "bj7lewis" wrote:



> 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

Re: How to call a method defined in grandparent class?

am 26.11.2007 14:03:19 von lasse

Jon Skeet [C# MVP] wrote:
> On Nov 26, 6:58 am, "bj7lewis" wrote:
>
>
>
>> 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:lasse@vkarlsen.no
http://presentationmode.blogspot.com/

Re: How to call a method defined in grandparent class?

am 26.11.2007 14:28:16 von skeet

On Nov 26, 1:03 pm, Lasse V=E5gs=E6ther Karlsen wrote:



> > 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

Re: How to call a method defined in grandparent class?

am 27.11.2007 00:30:50 von 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