473,390 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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.Method(); //Error CS1041
(this as Root).Method() ; /Error CS1540
Root.Method(); //Error CS1540
}

Any idea?

Sebastiaan.
}
Nov 16 '05 #1
5 10532
Sebastiaan Olijerhoek <no*****@microsoft.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sebastiaan Olijerhoek <no*****@microsoft.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.com>
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**********@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.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*****@microsoft.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**********@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.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
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...
4
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 :...
7
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
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. ...
7
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 {...
6
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...
20
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...
2
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...

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.