HighTechTalks DotNet Forums  

How to call a method defined in grandparent class?

Dotnet General Discussions microsoft.public.dotnet.general


Discuss How to call a method defined in grandparent class? in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Curious
 
Posts: n/a

Default How to call a method defined in grandparent class? - 11-25-2007 , 08:52 PM






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?


Reply With Quote
  #2  
Old   
bj7lewis
 
Posts: n/a

Default Re: How to call a method defined in grandparent class? - 11-26-2007 , 01:58 AM






Quote:
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...



Reply With Quote
  #3  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: How to call a method defined in grandparent class? - 11-26-2007 , 07:44 AM



On Nov 26, 6:58 am, "bj7lewis" <bj7le... (AT) rio (DOT) com> wrote:

<snip>

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


Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: How to call a method defined in grandparent class? - 11-26-2007 , 08:28 AM



On Nov 26, 1:03 pm, Lasse Vågsæther Karlsen <la... (AT) vkarlsen (DOT) no> wrote:

<snip>

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


Reply With Quote
  #5  
Old   
Alun Harford
 
Posts: n/a

Default Re: How to call a method defined in grandparent class? - 11-26-2007 , 06:30 PM



Curious wrote:
Quote:
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(ShowDeleg ate));
del();
}

Alun Harford


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.