473,508 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call the grandparent's method

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

}
Nov 16 '05 #1
4 6463
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");
}

}

Nov 16 '05 #2
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: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");
}

}

Nov 16 '05 #3
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: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");
}

}

Nov 16 '05 #4
Note that Ignacio's example of how to do this will work only if you use
the "new" keyword to declare successive WhoAmI() methods in the Parent
and Child. If you use the "override" keyword in the Parent.WhoAmI() and
Child.WhoAmI() method declarations (which is the usual practice) then
his example will print out "I am Child" because even though the object
is cast to Grandparent, polymorphism will take over and invoke the
child class's override for the method.

Everyone is pointing out that this is bad practice because you never
explained what problem you are trying to solve with this technique.
What are you trying to do? Perhaps if we better understood your
original problem we could propose a better solution.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2685
by: shaun roe | last post by:
I have a document about 4 levels deep and in my XSLT I want to generate a unique string ID for each basic element based on its path through the hierarchy. If I use recursion, I am continually...
24
7625
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
12
2681
by: DrBonzo | last post by:
Given public class A { virtual void foo() { ... } } public class B : A { public override void foo() { ... } public Bthingy() { ... base.foo() ...} }
4
2105
by: Crutcher | last post by:
This is fun :) {Note: I take no responsibilty for anyone who uses this in production code} #!/usr/bin/env python2.4 # This program shows off a python decorator # which implements tail call...
1
1184
by: K B | last post by:
Hi, I'm using this... //Role/../../@Name but it returns more than one result. I need the FIRST grandparent @Name returned. I've tried: //Role/../../item/@Name and
10
5681
by: Martin Manns | last post by:
Hi, I have a class structure as follows and I would like to invoke the method A.m() from D.m class A(object): def m(self): class B(A): def m(self): class C(A):
2
1836
by: Jack | last post by:
Hello, I have a 3-level class hirarchy: 1=Grandparent 2=Parent 3=Child The Grandparent has a method called OnPaint which I want to override from the Child class. I can do this, but how...
9
5830
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
5
4600
by: Curious | last post by:
I'll need something like: base.base.Show(); However, this doesn't work. The grandparent class name is "BaseForm". What's the correct syntax to call the "Show" method defined in the "BaseForm"...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.