use (this as GrandParent) in the Child's call. i.e., change the Child's
WhoAmI function to
public override void WhoAmI()
{
(this as GrandParent).WhoAmI();
Console.WriteLine("I am Child");
}
What you are doing is not a good programming practice (as the compiler will
tell you with its warnings).
1.cs(27,21): warning CS0114: 'Parent.WhoAmI()' hides inherited member
'GrandParent.WhoAmI()'. To make the current member override that
implementation, add the override keyword. Otherwise add the new
keyword.
1.cs(16,21): (Location of symbol related to previous warning)
1.cs(38,21): warning CS0114: 'Child.WhoAmI()' hides inherited member
'Parent.WhoAmI()'. To make the current member override that
implementation, add the override keyword. Otherwise add the new
keyword.
1.cs(27,21): (Location of symbol related to previous warning)
You should override inherited virtual functions if you are implementing new
functionality.
Ranjan
--
http://dotnetjunkies.com/weblog/dotnut
"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:ud**************@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");
}
}