Re: How to call the grandparent's method

From: Rachel Suddeth (rachel_at_bldhound.com)
Date: 01/24/05


Date: Mon, 24 Jan 2005 09:08:21 -0600

First of all, you want the parent and the child definitions to start like
this....
> public override void WhoAmI()
Then, the first line in each should be
> base.WhoAmI();
You remembered to put that in the child, but it needed to be in the parent,
too, if you wanted the grandparent base method to be called.

-Rachel

"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
    ... use (this as GrandParent) in the Child's call. ... Console.WriteLine("I am Child"); ... > 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)
  • Generic List<> change events
    ... I'm frequently using generic lists to represent child lists in my current system, ... link between them (so a child knows it's parent as well as the parent having the list of children.) ... public virtual void OnAddList{ ...
    (microsoft.public.dotnet.languages.csharp)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)

Loading