How to call a method defined in grandparent class?

C

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

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

Jon Skeet [C# MVP]

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
 
L

Lasse Vågsæther Karlsen

Jon said:
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.
 
J

Jon Skeet [C# MVP]

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
 
A

Alun Harford

Curious said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top