Modules - more information

Erel

B4X founder
Staff member
Licensed User
Longtime User
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).
B4X:
Sub Globals
  Public num, state
  Public Type (x,y) Point
  Private counter
  Dim a,b
  c = 4 'Private variable
End Sub
You can also add Public or Private before a sub declaration:
B4X:
Public Sub ThisIsAPublicSub (minValue, maxValue)
...
End Sub
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.
B4X:
Module1.Form1.Show
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.
 

Attachments

  • Examples.zip
    3.9 KB · Views: 189

agraham

Expert
Licensed User
Longtime User
Control events subs should be located in the same module of the parent form.
I have been playing and it seems that automatic compiler allocated event Subs "must be located" rather than "should be located" as presumably the compiler wires ModuleName.ControlName events to ModuleName.ControlName_EventName.

However for event Subs added at runtime by AddEvent it doesn't seem to matter where the Sub is declared

I assume that at runtime a Control can have it's parent changed with no ill effect as the compiler allocated event Subs will now be wired to the Control and will not be changed.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
However for event Subs added at runtime by AddEvent it doesn't seem to matter where the Sub is declared.
That is correct. You can use AddEvent to wire an event to a sub which is located in another module.

I assume that at runtime a Control can have it's parent changed with no ill effect as the compiler allocated event Subs will now be wired to the Control and will not be changed.
Do you mean with something like FormLib.ChangeParent?
It will probably work.
 
Top