Basic4android v2.00 adds two new access modifiers: Public and Private.
This tutorial will cover the rules which determine when a variable or a sub can be accessed from outside.
Note that the default accessibility is Public.
Variables
- Activities, services and code modules: process global variables are public by default.
Activity global variables are always private.
You can hide process global variables by using the Private modifier:
- Classes: sames as above for variables declared in Class_Globals.
Subs
- Subs declared in Activity and Service modules are private.
- Subs declared in Code modules and Class modules are public by default.
You can hide such subs with the Private modifier:
The Public modifier can also be used (though it doesn't have any effect):
CallSub and CallSubDelayed can be used to call subs in other services, activities and classes. These methods can be used to access both private and public subs.
This tutorial will cover the rules which determine when a variable or a sub can be accessed from outside.
Note that the default accessibility is Public.
Variables
- Activities, services and code modules: process global variables are public by default.
Activity global variables are always private.
You can hide process global variables by using the Private modifier:
B4X:
Sub Process_Globals
Dim ThisIsAPublicVariable As String 'public
Public ThisIsAPublicVariableToo As String 'exactly the same as Dim.
Private ThisIsAPrivateVariable As String 'private
End Sub
- Classes: sames as above for variables declared in Class_Globals.
Subs
- Subs declared in Activity and Service modules are private.
- Subs declared in Code modules and Class modules are public by default.
You can hide such subs with the Private modifier:
B4X:
Private Sub ThisIsAPrivateSub(x As Int)
End Sub
B4X:
'Public subs
Sub S1
End Sub
Public Sub S2
End Sub
CallSub and CallSubDelayed can be used to call subs in other services, activities and classes. These methods can be used to access both private and public subs.