473,909 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling base of base

How can I call the Root.Method() from B.Method()

class Root
{
public Root()
{
}

private virtual void Method()
{
}
}

class A : Root
{
public A()
{
}

private virtual void Method()
{
base.Method();
}
}

class B : A
{
public B()
{
}

private virtual void Method()
{
// How to call base of base?
base.base.Metho d(); //Error CS1041
(this as Root).Method() ; /Error CS1540
Root.Method(); //Error CS1540
}

Any idea?

Sebastiaan.
}
Nov 16 '05 #1
5 10566
Sebastiaan Olijerhoek <no*****@micros oft.com> wrote:
How can I call the Root.Method() from B.Method()


You can't. For one thing you've actually defined three entirely
separate methods in the code, because they're private. (There's no
point in making a virtual method private.)

Assuming you *actually* meant to make them non-private methods, with
A.Method overriding Root.Method, and B.Method overriding A.Method, you
still can't - you can only call "one level higher".

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Yes I *actually* meant that. I obviously didn't compile the code.
Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Sebastiaan Olijerhoek <no*****@micros oft.com> wrote:
How can I call the Root.Method() from B.Method()


You can't. For one thing you've actually defined three entirely
separate methods in the code, because they're private. (There's no
point in making a virtual method private.)

Assuming you *actually* meant to make them non-private methods, with
A.Method overriding Root.Method, and B.Method overriding A.Method, you
still can't - you can only call "one level higher".

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Sebastiaan,

I am not sure why you would need to call up the hierarchy like this, whilst
missing part of the hierarchy out. It is much more common to see something
like the following, which passes a call up the hierarchy:

class Root
{
public Root()
{
}

protected virtual void Method()
{
}
}

class A : Root
{
public A()
{
}

protected virtual void Method()
{
base.Method();
}
}

class B : A
{
public B()
{
}

protected virtual void Method()
{
base.Method();
}
}

Are you sure the above cannot be adapted to suit your purposes? Perhaps with
a parameter to control which levels code executes?

If you absolutely must call just the root method, you could add a special
method into A, such as:
protected void CallBaseMethod( )
{
base.Method();
}
and call that from Method() in class B. Not pretty though!

Cheers,
Chris.
Nov 16 '05 #4
Chris,
You are right it should be protected override methods.

Your solution doesn't work for me as class A is a class in a 3rd party
library and class Root is of the .NET framework.

Kind regards,
Sebastiaan.

"Chris Ballard" <Ch**********@d iscussions.micr osoft.com> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
Sebastiaan,

I am not sure why you would need to call up the hierarchy like this, whilst missing part of the hierarchy out. It is much more common to see something
like the following, which passes a call up the hierarchy:

class Root
{
public Root()
{
}

protected virtual void Method()
{
}
}

class A : Root
{
public A()
{
}

protected virtual void Method()
{
base.Method();
}
}

class B : A
{
public B()
{
}

protected virtual void Method()
{
base.Method();
}
}

Are you sure the above cannot be adapted to suit your purposes? Perhaps with a parameter to control which levels code executes?

If you absolutely must call just the root method, you could add a special
method into A, such as:
protected void CallBaseMethod( )
{
base.Method();
}
and call that from Method() in class B. Not pretty though!

Cheers,
Chris.

Nov 16 '05 #5
What is Root?, what is the 3rd party library?

"Sebastiaan Olijerhoek" <no*****@micros oft.com> wrote in message
news:10******** *******@ernani. logica.co.uk...
Chris,
You are right it should be protected override methods.

Your solution doesn't work for me as class A is a class in a 3rd party
library and class Root is of the .NET framework.

Kind regards,
Sebastiaan.

"Chris Ballard" <Ch**********@d iscussions.micr osoft.com> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
Sebastiaan,

I am not sure why you would need to call up the hierarchy like this,

whilst
missing part of the hierarchy out. It is much more common to see
something
like the following, which passes a call up the hierarchy:

class Root
{
public Root()
{
}

protected virtual void Method()
{
}
}

class A : Root
{
public A()
{
}

protected virtual void Method()
{
base.Method();
}
}

class B : A
{
public B()
{
}

protected virtual void Method()
{
base.Method();
}
}

Are you sure the above cannot be adapted to suit your purposes? Perhaps

with
a parameter to control which levels code executes?

If you absolutely must call just the root method, you could add a special
method into A, such as:
protected void CallBaseMethod( )
{
base.Method();
}
and call that from Method() in class B. Not pretty though!

Cheers,
Chris.


Nov 16 '05 #6

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

Similar topics

1
1437
by: Prithvis | last post by:
My class hierarchy is something like C1 -> C2 -> C3 and in C3 I override a method from C2 which in turn has overriden this method from C1, is there a way to call C1's version of the method? This would be something like base.base.MethodName() or C1.MethodName(). It would be a great help if anyone can suggest anything on this.
4
1272
by: Frank Rizzo | last post by:
In some examples with inheriting, I see the overloaded function always call its base. For instance, in this case where the ListView control is being extended. public class ListViewEx : System.Windows.Forms.ListView { protected override void OnColumnClick(ColumnClickEventArgs e) { ...various code to extend the control
7
37726
by: Jo Vermeulen | last post by:
Hello, I was wondering how I could call the base class constructor from a derived class constructor. In Java I could do something like super(parameter); I tried base(parameter);
3
2243
by: esafran | last post by:
I've defined a class, but now I want to Define a Finalizer (destructor)... How do I call the Base Finalizer???, void Finalize() is a protected override method and Type.GetType Does not work. Hoe do I call it? private void DefineFinalizer(TypeBuilder typeBuilder) {
7
2370
by: Lloyd Dupont | last post by:
I create 2 ManagedC++ class inherithing from each other. I can't see how do I call super destructor or ensure it's called :-/ template <class T> public ref class CArray : System::IDisposable { protected: ~CArray() { if( ptr ) // I'm not sure of what IDisposable will do... free( ptr );
6
4144
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected functions of another (same type) - but not via a base class pointer.... I've checked the FAQs, Meyers etc but nothing obvious I can find explains it.
20
2882
by: Jorgen Bodde | last post by:
Hi All, Now that I am really diving into Python, I encounter a lot of things that us newbies find difficult to get right. I thought I understood how super() worked, but with 'private' members it does not seem to work. For example; .... def __baseMethod(self): .... print 'Test'
2
1220
by: DaTurk | last post by:
Just a syntax question, how do I call the parent's method I've overridden from the child in syntax. I notice there is no "base" Keyword. Thanks.
0
11346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10919
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10538
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7248
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5938
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3357
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.