In trying to understand the functionality of Extends, i am writing some simple examples.
When I got the idea of trying something else.
I am trying to extend the functionality of a RectControl, so this functionality is available for all UI-objects.
I can write the generic method for all these controls, the extends works great.
But is it then also possible to add a property to the RectControl, to save a value belonging to all these controls.
Probably my over imaginative mind, thinking up the impossible. Can’t seem to find example code.
Can anybody make any clarifying remarks on this subject?
Why not create a new object subclassed from a RectControl and add whatever properties and/or methods you need/want?
This is exactly how 80% of the Xojo built in controls are created.
If you just need a new method on something that is in the framework (say, the canvas class for instance) then extends is awesome because you don’t have to subclass anything, and the new method is available immediately to all canvases.
As soon as you need to add properties, you have to subclass.
If you subclass rectControl, how do you then make that subclass the superclass of built-in rectControls like ListBox, etc.?
It’s subclasses all the way down…
Thank you all for the replies.
[quote=76953:@Kimball Larsen]If you just need a new method on something that is in the framework (say, the canvas class for instance) then extends is awesome because you don’t have to subclass anything, and the new method is available immediately to all canvases.
As soon as you need to add properties, you have to subclass.[/quote]
Actually you can kludge properties with extends using a dictionary… but of course subclassing is much better as that kludge has WAY more overhead.
I think this is what Kimball meant, but just in case
In a module, if you:
Sub SomeMethod (Extends r As RectControl)
then SomeMethod will be available in every control that is based on RectControl throughout your project. Suddenly in, say, a button, you can call:
and it will work.
You cannot alter the inheritance tree of the classes in the framework.
Making this possible would be analogous to giving source code licenses to the framework & then trying to support several hundred (if not several thousand) variants & any/all the possible bugs introduced by end being able to modify the framework.
Having worked in a business that did so once upon a time (as both a licensee and licensor) I can tell you the first response becomes “replace your custom version of the framework with the original one - reproduce the error now” and of course with sufficient variation from the official sources this becomes a large task
Thank you, and yes the example you gave was what i discovered what the Extends does after trying it in some different ways.
But then I ran into the requirement/idea that I would like to be able to also create a property on the RectControl.
So that I will be able to check in my Extended method if this control adhere’s to certain values.
Since this value should be available for every RectControl I was hoping (dangerous, i know) that I was just missing
something somewhere in the manual about this possibility.
I certainly can handle this by subclassing every individual control, but lazy as I am, hoping that what was possible with the Method extension was also possible for a property.
It just shows how immature I still am with this OO part of Xojo, but in my experience thats how I learn best.
Try, Try, ask around and learn, so thank you all for the feedback
An interface might be the way to go here. Let’s say you want to store a time in conjunction with each control. Make a subclass of each (TimedLabel, TimedRectangle, TimedPushButton, etc.), and have each implement the interface Timed. That interface would have the methods to get and set the relevant time (dim’ed as a date object, probably). Then, in each of the subclasses, you would have the code that actually carries it out–the subclasses would have a property to store the relevant time.
You could fake that with a Dictionary. I’m just thinking out loud here, but I remember doing something similar with my HelpTagDisabler class.
In your module, you create a Dictionary whose values will be RectControls. You create a pair of methods like this:
Sub FakedProperty (Extends r As RectControl, Assigns v As Integer) // Setter
PropDict.Value( r ) = v
Function FakedProperty (Extends r As RectControl) As Integer // Getter
if PropDict.HasKey( r ) then
return PropDict.Value( r )
The only thing you must do is to remove the controls from the Dictionary when the window is closed. In the Window.Close event, you can pass the window to another method that cycles through its controls and removes them from the Dictionary if they exist.
Thank you for the example. I will definitely try this out and see if this is the solution that might work.
It sure looks that way on paper.
Let us know how it works out. And I should have written, “
a Dictionary whose keys will be RectControls
[quote=76973:@Norman Palardy]You don’t.
You cannot alter the inheritance tree of the classes in the framework.[/quote]
That was my point…
Can’t think of a single framework from a tool vendor where you CAN
It’d be a nightmare for us to support (ie/ darned close to impossible)
The dialog would go something like
you : I have this bug in your framework !
us : have you inserted code in the framework or overridden supers ?
you : yes - so please fix your bug
us : please remove your code and then see if the bug occurs
you : I can’t as my software depends on the changes I’ve made
us : so how are you sure its our bug
you : well its in the framework
us : which you altered
you : yes, but its still framework code so its your bug
and so on
You can ask but I put the chances of such a framework happening are 0
You seem to think I’m arguing in favor of such a thing. I’m NOT. I was just asking how Dave was suggesting that it was possible…
The short terse messages certainly led me to believe you were arguing in favor of being able to do that
What Dave suggested was “subclass rect control and build your own framework” more or less
Been there done that painful as hell since you get no help from what Xojo has already done & you have to reinvent ALL the wheels