From now on all code is written inside code modules.
Each module can include code, forms and objects.
Adding new modules is done by choosing Modules (menu) - Add New Empty Module or Add Existing Module.
When you add an existing module, it will be copied to the source code folder.
Modules files are saved with .bas extension.
The main project file (with .sbp extension) is loaded as the Main module.
Access modifiers:
Each sub and each global variable can be declared as 'Private' - only accessible from their own module or 'Public' - accessible from other modules as well.
By default all subs and variables are private.
Instead of the Dim keyword you can use Private or Public to declare a global variable (Dim is equivalent to Private).
You can also add Public or Private before a sub declaration:
Note that declaring a private sub is equivalent to a default sub.
Accessing public subs, variables, objects and controls of another module is done by writing the module name followed by a period and the required field.
It is only necessary to write the module name when accessing fields in other modules. However it is legitimate to also access the current module fields in this way (the pop-up list will show both public and private fields in this case).
When you create a new form you can choose to create this form in a new module instead of the current one.
You can also move existing forms from one module to another from the designer: Tools - Move Form.
Control events subs should be located in the same module of the parent form.
I've attached two small examples.
Each module can include code, forms and objects.
Adding new modules is done by choosing Modules (menu) - Add New Empty Module or Add Existing Module.
When you add an existing module, it will be copied to the source code folder.
Modules files are saved with .bas extension.
The main project file (with .sbp extension) is loaded as the Main module.
Access modifiers:
Each sub and each global variable can be declared as 'Private' - only accessible from their own module or 'Public' - accessible from other modules as well.
By default all subs and variables are private.
Instead of the Dim keyword you can use Private or Public to declare a global variable (Dim is equivalent to Private).
B4X:
Sub Globals
Public num, state
Public Type (x,y) Point
Private counter
Dim a,b
c = 4 'Private variable
End Sub
B4X:
Public Sub ThisIsAPublicSub (minValue, maxValue)
...
End Sub
Accessing public subs, variables, objects and controls of another module is done by writing the module name followed by a period and the required field.
B4X:
Module1.Form1.Show
When you create a new form you can choose to create this form in a new module instead of the current one.
You can also move existing forms from one module to another from the designer: Tools - Move Form.
Control events subs should be located in the same module of the parent form.
I've attached two small examples.