Lets say I wish to make a new subclass of TEXTFIELD and a new subclass of TEXTAREA
and I want to add the SAME computed property to both of them…
Is there a way I can place the code ONE time, and add it to both objects…
Since it has SETTER/GETTER, I cannot see a way that makes it worth the effort (as opposed to duplicating the code in each subclass, which makes the overall code larger, and makes multiple maintenance points)
and NO, I don’t wish to make a new TEXTFIELD subclass from a TEXTAREA
I have a need to do this in a number of places, and TextField/TextArea is just one example.
I suppose you could make the backing store for the computed property a class instance
Then maybe you could lift all the common code into that class
Then adding the computed property to each class has it present in each but the code for whatever manipulations is in the class
What Norman describes I know of as the Composition pattern and used it several times. It’s the only (sic) way to effectively implement multiple inheritance in a single inheritance language. Basically instead of inheriting the functionality you compose it as a property and mirror it’s methods which just call through to the property. If you need the class to also be typed as it’s composed property you can add an interface, but if you don’t need that you could also forego mirroring the methods and just expose the property which would contain some reference back to your class to act on.
In one of my projects I use the CurrentMethodName() method inside the getter and setter to figure out the property name and then update a jsonObject automatically as the properties are modified. Its been effective. Perhaps you could do something similar and then use introspection to get its type and update things properly. (I might be completely off here, but I figured it couldn’t hurt too much)
thanks for the help… but either my question is being misunderstood, or I am grossly misunderstanding the responses.
computed property XYZ
SET
SELF.abc=something
GET
return SELF.abc
end property
simple psuedo code of a computed property (my real code is more complex in some cases)
and abc may a new subclass property, or manipluation of an existing property
I want to reuse that over dozens of subclass of various controls… but it seems the most effective way is to DUPLICATE this in every subclass. While the CODE is the same, the meaning of SELF is different based on SUPER
Why would I create a forum topic called “SUBCLASS” if I wasn’t talking about subclasses…
[quote]simple psuedo code of a computed property (my real code is more complex in some cases)
and abc may a new subclass property, or manipluation of an existing property[/quote]
The point being… in these new subclasses I may be adding properties beyond those of RECTCONTROL… and since these new properties are at a different level than the SUPER, they can’t be accessed via a common method.
Then you’re left with copying the code from subclass to subclass (which is a bit strange in my view, because you really could add it to their parent class, RectControl).
A dictionary in the module which contains the extension methods. Don’t forget to remove each instance when the window closes, or these controls will live forever and prevent the window becoming Nil.
Create a private property in each of the subclass and in the extension method have something like this:
Sub XYZ(Extends rc As RectControl, Assigns value As Integer)
Select Case rc
Case IsA MyTextField
MyTextField(rc).abc = value
Case IsA ...
...
Else
Raise New // TODO
End
End Sub
What do you expect the getter/setter to do? Simply access a property? Or do some additional, common processing? That additional processing can be abstracted out, but the property access cannot.