468,784 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,784 developers. It's quick & easy.

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 10245
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Prithvis | last post: by
4 posts views Thread by Frank Rizzo | last post: by
7 posts views Thread by Jo Vermeulen | last post: by
7 posts views Thread by Lloyd Dupont | last post: by
20 posts views Thread by Jorgen Bodde | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.