You can add your own features to built-in classes by creating
subclasses of them. But this is not always convenient. For example, say
you want to add a SelectAll method to the Editfield class. You could
create a special subclass of the Editfield class that has this feature.
But then you would have to go through your project and set the super
property of all of your editfields to your new subclass. Wouldn't it be
great if you could just add a SelectAll method to the regular Editfield
class so that all of your editifields would automatically have this new
feature? Well, you can.
Using the "extends" keyword (which was added in REALbasic 5.0) you can
create methods that add themselves to existing classes. You are in
effect, "extending" them. Let's consider the SelectAll example again.
Rather than create a subclass, you simply add a module with a global
method called SelectAll. Then to extend the Editfield class you use the
extends keyword as part of the first parameter in your declaration. So
your SelectAll method looks like this:
Sub SelectAll(Extends E as Editfield)
e.SelStart = 0
e.SelLength = len(e.text)
End Sub
Now at first glance it may appear that you have to pass a parameter to
the SelectAll method but in fact you don't. REALbasic knows that when
it sees the "extends" keyword to simply add this method to the class
indicated (Editfield in this case). With this method in your project,
all of your editfields gain a SelectAll method. For example, to call it
for Editfield1, do this:
Editfield1.SelectAll
That's it. And if you want to add this SelectAll method to editfields
in other projects, just drag your module into the project and you're
all set to go.
--
Geoff Perlman
President and CEO
REAL Software, Inc.
- - - - - - - - - -
Got a useful tip to share? Send it to us at:
<REALbasic-tips at lists dot realsoftware dot com>
Click here to unsubscribe:
<http://support.realsoftware.com/listmanager/>
|