Calling a class method 2 levels up in the hierarchy possible?

  • Thread starter Thread starter Jack
  • Start date Start date
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.

Thanks,
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.

You can cast to the grandparent type first

Thus
 
No I don't think that will work. Don't forget that the "Paint" will vector
back to the caller in this case - at least I don't think it would work (I
didn't test it) - I know in C++ you can write MYBASE::DoThis() to get that
effect though.

Whenever I need to do something like this, I ensure the base class has a
public method that does what I want it to and then use that instead of the
base class method. So,

Public Class MyBase

Public overridable Sub Paint ()

PaintGrandad ()

End Sub

Public Sub PaintGrandad ()

End Sub

End Class

Public Class MyChild
Inherits MyBase

Public overrides Sub Paint ()

... do my own thing

End Sub

End Class

Public Class MyGrandchild
Inherits MyChild

Public Overrides Sub Paint ()

' Do what grandad does.

Mybase.PaintGrandad ()

End Sub

End Class
 
No I don't think that will work. Don't forget that the "Paint" will
vector back to the caller in this case - at least I don't think it
would work (I didn't test it)

Yeah....well.... I was just testing you... yeah .... testing you ... that's
it ...honest

ok .. so I screwed up ...you're absolutely right :)

I believe I was confusing this scenario with an attempt to access a shadowed
method with differing signature in a base class.

In this case the casting would reveal the base class method.

Good catch :)
 
Back
Top