Coding with Modules
Variables Scope
Generally, all variables in Basic4ppc are accessible from within their parent Sub only. Only variables declared in the Global sub are accessible from anywhere inside their module. This is called the variable's scope.
Access modifiers
By default, all variables' and subs' scope is limited to the containing module. In order for them to be accessible from other modules, their declaration must be preceded by the public access modifier. Variables can become public when declared in the Globals sub of any module with the public access modifier, for example, if the main module contains this declaration:
|
|
Sub
Globals
Public
someFlag
End Sub
|
|
In this example, the someFlag variable is public, hence accessible from different module. Accessing it will be done by writing the containing module's name followed by a period and the variable's name as follows:
|
|
Sub
someSub
Main.someFlag = 10
End Sub
|
|
Error message when trying to access a private Variable or Sub
When trying to access a variable or sub that was not declared with the public access modifier, you might encounter the following error message. In this case, add the public keyword to the relevant declaration. Note that the term "member" in this message is used to indicate either a variable, or a sub. See details about subs' scope later this page.

Private access modifier
When declaring variables in the Globals sub using the Dim keyword rather than the Public access modifier, variables declared will be treated as private - they will be accessible only from within their own module. The same behavior can be achieved using the private access modifier instead of the dim keyword. The following two code segments have the same exact functionality:
|
|
Sub
Globals
Dim
someFlag
End Sub
|
|
|
Sub
Globals
Private
someFlag
End Sub
|
The private access modifier is supported for readability only.
Duplicate names
Two modules can have variables with identical names. Accessing other module's variable when it has the same name is done by explicitly indicating the owner module's name. For example, in a sub-module of a project (not the Main module), we can write:
|
|
Public Sub
Third Msgbox(Main.b) 'show the Main module's variable "b" value Msgbox(b) 'show this current module's variable "b" value End Sub
|
Accessing Subs
Everything written above about variables apply for Subs as well. Subs do not have to be declared in the Globals sub, of course, but apart from it behave the same as variables, regarding scope:
By default, Subs are accessible only from within their module.
Adding the public access modifier to a Sub declaration will allow you to access it from a different module, thus exposing functionality. Having the following code in the Main module, for example:
|
|
Public Sub
someSub
MsgBox
("Some Sub
Calling")
End Sub
|
... will allow you to access someSub from within different module as follows:
|
|
Sub
otherModuleSub
Main.someSub
End Sub
|
Subs can be preceded by the private access modifier: this has the same functionality as not writing any access modifier as subs are private by default.
Accessing Controls and Objects
Controls and Objects are always Public. The cannot have access modifier and are accessible explicitly from within other modules using the standard syntax: [module name].[control name].
Forms and modules
A module can contain any number of forms. Like other controls, forms are always public. When adding a new form to a project, you can choose either to add it to the current module, or to a new one. A good habit is to keep each form in its own module. This makes it easier to replace the entire code later when demands change and improves readability.
After selecting the module in which the new form is added, you can always change its module from the Visual Designer window, by selecting from the main menu Tools >> Move Form To:

Next page: Special subs