An object in Gambas is a data structure that provides properties, variables, methods and events.
Objects are accessed "by reference", i.e. by using a pointer to it, i.e. by using a variable whose value is the address of the object data structure in memory.
![]() |
You can see the address of an object by using the PRINT instruction:
DIM aStr AS NEW String[] PRINT aStr <hr>(String[] 0x80dccf8) |
The object data structure is described by a class.
So each Gambas object has a class that describes all its public properties, methods and events. This class is a Gambas object too, whose class is the class named Class.
A static class is a class whose all members are static (see below). In Gambas, a static class is also named a module.
A virtual class is an hidden pseudo-class that you cannot explicitely manipulate.
Properties and methods allow to manipulate the data structure.
A property, a method or a variable can be static:
Private symbols can only be used from the class inside. Public symbols can be used everywhere, provided you have a reference pointing at the object.
Events are signals sent by an object when something happens on it.
If an object raises events, it will hold a reference on its observer, or parent object.
This observer is another object that implements event handlers. An event handler is just a public method that is called each time the event is raised.
By default, the observer is the current object where the newly instanciated object is declared.
To raise events, an object must have an event name. This event name is assigned at object instanciation, when using the NEW instruction and the AS keyword, and is the prefix of all event handler methods.
DIM hButton AS Button hButton = NEW Button(ME) AS "ButtonEventName"
If no event name is specified, then the object won't raise events.
An object can be locked so that it stops raising events, and can be unlocked so that it raises them again. See the Object.Lock and Object.Unlock methods.
Some events can be cancelled by the event handler, by using the STOP EVENT instruction. The effect of this cancellation depends on the event.
There is no garbage collector in Gambas. So each object has a reference counter that is incremented each time the object is referenced by any variable, array, collection or other object, and decremented when it is released.
This reference counter is zero at object creation, and when it becomes zero again after a reference release, the object is freed.
An object can become invalid. Because, for example, it is linked to an internal object not managed by Gambas that was destroyed.
Trying to use an invalid object raises an error.
Special methods are methods declared in classes, whose name begins with an underscore character, and that are called by the interpreter in the following situations:
Inheritance is the way for a class to become a specialized version of another class.
The class inherits from its parent every method, property, constant and event.
![]() | You must use the ME keyword to access the inherited elements from the class inside. |
You can inherited any class, even a native one!
For example, you can create a custom MyListBox class that inherits ListBox but allows to associate a tag with each list item.
Note that you cannot use INHERITS in a form class file, because forms already inherits the Form class.
![]() | The inheritance tree depth cannot be greater than 16. This is a constant hard-coded inside the Gambas interpreter. |
When calling a method or accessing a property from an object reference, Gambas always uses virtual dispatching.
It means that the real class of the object is always used, and not the type of the variable that references the object - As it was in Gambas 1.0.
Contrary to all the object language I know, each class in the inheritance hierarchy consumes the parameters passed to the constructor.
Let's suppose we have the following inheritance tree:
MyListBox ---inherits--> ListBox ---inherits---> Control
So arguments must be specified in reverse order.
Then you will create a MyListBox control this way:
hMyListBox = NEW MyListBox("Name", hContainer)
Gambas components are external shared libraries written in C, C++ or in Gambas that add new functions and/or classes to the Gambas interpreter.
Classes are grouped according to the component they come from.
The interpreter includes an internal component named gb that defines all standard classes of the language.
This component is always loaded by default, and can be considered as part of the language.
Each component has its own private class symbol table, so that class names do not conflict.
So that components can work together, there is a global symbol table, where all classes exported by components and all classes exported by the current project are stored.
If there is a name conflict in this global symbol table, the last loaded class overrides the previous loaded class with the same name, by using inheritance. In other words, the overriding class extends the overriden one.
This last feature can be used for:
Your project has its own private symbol, like any component, and can export any of its classes to the global symbol table by using the EXPORT keyword.
The project classes are loaded after all components. So your exported class can override any exported classes declared in any component.