This is massively useful for simply adding properties to existing classes or to override some of a classes functions.
Also massively useful for backward compatibly as you can auto add or override things (read on for more info)
How you do it...
Lets show you a simple example...
Say you want the Timers in your application to also have a .Tag property.
So in your application make a new class and name it Timer.class
Add the following code to it...
' Gambas class file
Export
Public Const _Properties As String = "*,Tag"
Property Tag As Variant Use $vTag
That's it. Now you will find all your Timer objects also have a Tag property.Automatic inheritance will automatically make your Timer.class inherit the existing Timer.class as it has the same name.
As Timer can be a virtual IDE object i also added Tag to the _Properties string constant making it visible in the IDE form designer.
[hr] You can go much further than adding new properties/methods to existing classes, you can also override class methods by making your own versions.
Take this example where I want the Timer to have a Stopped event raised if the timer gets disabled. (again the class is just called Timer.class)
' Gambas class file
Export
Property Enabled As Boolean
Event Stopped
Private Sub Enabled_Write(Value As Boolean)
' Set the inherited Timer.class Enabled property.
Super.Enabled = Value
' Raise the Stopped event if not enabled
If Not Value Then Raise Stopped
End
Private Function Enabled_Read() As Boolean
Return Super.Enabled
End
Public Sub Stop()
Me.Enabled = False
End
In that example we are overriding the Enabled property and the Stop() method.Setting Timer.Enabled is now handled by our class, it sets the inherited Timer.Enabled property using Super.Enabled and raises the Stopped event if set to false.
The Stop() method is also overridden to set our own Enabled property and trigger the Stopped event.
[hr] This can also be very useful for backward compatibility.
I had a problem with one of my applications once where i had used the Collection.Keys property not realizing that Keys is new to gambas 3.17. so anyone using an older gambas got an error.
The solution...
I create a class in my application called Collection.class
add the following code..
' Gambas class file
Export
Property Read Keys As String[]
Private Function Keys_Read() As String[]
Dim sKeys As New String[]
For Each Me
sKeys.Add(Me.Key)
Next
Return sKeys
End
Now I am using my own Keys property with Collection.If an older gambas the Keys property will be added to Collection, if a new gambas then it will be overridden.
[hr] Note:
Properties and methods you override in a class will block the inherited classes properties/methods.
Use the Super keyword to access the inherited class things.
As i have in the second example where i set Super.Enabled, this sets the Enabled property of the inherited Timer.class not my one.
Happy coding.
Bruce