Re: How to call the grandparent's method

From: Ignacio Machin \( .NET/ C# MVP \) ("Ignacio)
Date: 01/24/05


Date: Mon, 24 Jan 2005 09:06:59 -0500

Hi,

 You cannot do it directly, you only have access to your parent.
It's considered a BAD practice doing what you want, you should use your
parent version , if it was redefined there it was done for a reason
afterall. Remember that it may be possible that the method does not even
exist in your grandparent definition.

 IF for some reason you need to access it, there are two ways of doing it.
1- If you know your grandparen type you could do a cast:
       ((GrandParent)this).Method()

2- If you do not know it you may use reflection to know your grandparent
type and do a similar construction as in the above example

Anyway, remember it's a bad practice doing that.

Cheers,

-- 
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"ad" <ad@wfes.tcc.edu.tw> wrote in message 
news:udEx$fgAFHA.1084@tk2msftngp13.phx.gbl...
> base  only can call the parent 's method
> The result of the example below is
> I am Parent
> I am Child
>
> How can we call the grandparent's method in a inhreited class,
>
> I want the result is
>
> I am GrandParent
> I am Child
>
> --------------------------------------------------------------------------
> using System;
>
> class Test
> {
> static void Main()
> {
> Child aChild = new Child() ;
> aChild.WhoAmI();
> Console.Read() ;
> }
> }
>
> class GrandParent
> {
>
> public virtual void WhoAmI()
> {
> Console.WriteLine("I am GrandParent");
> }
>
> }
>
> class Parent : GrandParent
>
> {
>
> public virtual void WhoAmI()
> {
> Console.WriteLine("I am Parent");
> }
>
> }
>
> class Child : Parent
>
> {
>
> public virtual void WhoAmI()
> {
> base.WhoAmI();
> Console.WriteLine("I am Child");
> }
>
> }
>
> 


Relevant Pages

  • Re: How to call the grandparents method
    ... > public override void WhoAmI() ... You remembered to put that in the child, but it needed to be in the parent, ... > public virtual void WhoAmI() ...
    (microsoft.public.dotnet.languages.csharp)
  • Any Grandparents out there?
    ... fields: GP PP1 CC1 ... I basically want to have CC1+CC2+CC3+CC4 show up on the Grandparent ... textbox in the Child form and then in the Parent form using something ...
    (microsoft.public.access.formscoding)
  • Blocking virtual methods
    ... In pursuit of a child class blocking a parent's virtual method, ... : public Grandparent ... : public Parent ...
    (comp.lang.cpp)
  • Re: Any Grandparents out there?
    ... I realized that I could eliminate the parent ... altogether and get the total that directly from the child table to the ... grandparent table. ... >> footer textbox in child to bring it up to Grandparent? ...
    (microsoft.public.access.formscoding)
  • How to call the grandparents method
    ... I am Parent ... I am Child ... I am GrandParent ... public virtual void WhoAmI() ...
    (microsoft.public.dotnet.languages.csharp)