Possible to call methods 2 levels up the inheritence tree possible(not just 'MyBase.X()')

J

Jack

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 can I call the
Grandfather's OnPaint() method inside this newly overrided one in the
child class.

I can't use: MyBase.OnPaint(..) because it expects the OnPaint(...) to
be in the Parent class immediately above.

How can call a method from a class 2 levels further up from this child
class? I'm playing with creating custom winform controls if you were
wondering.

Thanks,
Jack.
 
M

Mattias Sjögren

I can't use: MyBase.OnPaint(..) because it expects the OnPaint(...) to
be in the Parent class immediately above.

No it doesn't. If there's no OnPaint override in Parent then the
implementation in Grandparent will be called.


Mattias
 
P

Phill W.

Jack said:
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 can I call the
Grandfather's OnPaint() method inside this newly overrided one in the
child class.

Move the important code sideways into another, Protected method.
Have the [Grandparent].OnPaint call this new method.

The Parent can do what it likes, overriding OnPaint or not.
You don't actually care.

[Child].OnPaint will override anything in [Grandparent].OnPaint or
[Parent].OnPaint, unplugging their respective implementations and
replacing it with its own.
[Child].OnPaint /can/ call your extracted method, thereby bypassing the
Parent level entirely.
I can't use: MyBase.OnPaint(..) because it expects the OnPaint(...) to
be in the Parent class immediately above.

If there's an implementation of [Parent].OnPaint, then MyBase.OnPaint
will use that. If there /isn't/, then it will "cascade" up into
[grandParent].OnPaint. Of course, you have no way of knowing which.
How can call a method from a class 2 levels further up from this child
class?

With multiple, possible overrides involved, you can't.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top