Re: How to call the grandparent's method
From: Ignacio Machin \( .NET/ C# MVP \) ("Ignacio)
Date: 01/24/05
- Next message: Julia: "problem with relative paths inside windows service?"
- Previous message: Ignacio Machin \( .NET/ C# MVP \): "Re: Array os Class"
- In reply to: ad: "How to call the grandparent's method"
- Next in thread: Bruce Wood: "Re: How to call the grandparent's method"
- Reply: Bruce Wood: "Re: How to call the grandparent's method"
- Messages sorted by: [ date ] [ thread ]
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");
> }
>
> }
>
>
- Next message: Julia: "problem with relative paths inside windows service?"
- Previous message: Ignacio Machin \( .NET/ C# MVP \): "Re: Array os Class"
- In reply to: ad: "How to call the grandparent's method"
- Next in thread: Bruce Wood: "Re: How to call the grandparent's method"
- Reply: Bruce Wood: "Re: How to call the grandparent's method"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|