Inheritance and late binding

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi,
I have a class (ClassA) that passes a Widget to a public function
(FixWarts) in a member variable class (Doctor).
Doctor.FixWarts checks one of the Widgets public properties (Wartcount)
and then takes some action.
Works fine.

Then along came another class ( ClassB) with a need for a DeluxWidget. The
underlying behaviour of the DeluxWidget Wartcount property is incompatible
with its base class Widget, so it has a shadows property Wartcount.
(DeluxWidget gets / sets , Widget is readonly)

In ClassB I declared myWidget as a deluxWidget and passed it into member
instance of Doctor.FixWarts.

FixWarts tries to use the Widget Wartcount property even though it has been
passed a DeluxWidget. The IDE confirms that a DeluxWidget has been passed
in.

I can fix this using latebinding and declaring an object in Wartcount,
newing it to the appropriate class (Widget or DeluxWidget) and assigning
the WartCount input parameter to the object. Then I ask the object for its
wartcount property and it uses the correct one.

Have I missed something?
Should I be able to feed a Widget or DeluxWidget into FixWarts and get
FixWarts to correctly choose which version of the property to use?
thanks
Bob
 
In the base class, make Widget.WartCount "Overridable"

Overridable Public Property WartCount as Integer

In deluxWidget, declare the property as follows:

Overrides Public Property WartCount as Integer

Then, you will get the behavior you want. The term for this kind of
behavior is polymorphism.
 
Back
Top