Re: How to call the grandparent's method
From: Ranjan (ranjan.listserv_at_gmail.com)
Date: 01/24/05
- Next message: Hans Kesting: "Re: Array os Class"
- Previous message: Leon Lambert: "Re: Testing C# interpreter"
- In reply to: ad: "How to call the grandparent's method"
- Next in thread: Ignacio Machin \( .NET/ C# MVP \): "Re: How to call the grandparent's method"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 24 Jan 2005 17:27:17 +0530
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: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: Hans Kesting: "Re: Array os Class"
- Previous message: Leon Lambert: "Re: Testing C# interpreter"
- In reply to: ad: "How to call the grandparent's method"
- Next in thread: Ignacio Machin \( .NET/ C# MVP \): "Re: How to call the grandparent's method"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|