Re: How to call the grandparent's method

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Ranjan (ranjan.listserv_at_gmail.com)
Date: 01/24/05


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");
> }
>
> }
>
>


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)
  • How to call the grandparents method
    ... I am Parent ... I am Child ... I am GrandParent ... public virtual void WhoAmI() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to call the grandparents method
    ... you only have access to your parent. ... exist in your grandparent definition. ... > I am Child ... > public virtual void WhoAmI() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: interprocess messaging to/from many grandchildren
    ... |> I want to have a large number of child processes communicating ... |> need to rule out having separate pipes to each child. ... |> since it is the grandparent doing the communications, ...
    (comp.unix.programmer)
  • Re: Grandparent troubles
    ... There are two possible scenarios here, one involving social services, ... In the case of b or c, the case will be referred to CAFCASS, who will ... best interests of the child are. ... which may involve the Grandparent as a suitable ...
    (uk.legal)