Android Tutorial Static Code Modules

Basic4android v1.2 includes a new type of module which is the static code module.
Adding a static code module is done by choosing Project - Add New Module - Code Module.

Unlike Activities and Services, code modules are not related in any way with Android process life cycle. These are just containers for code.
All of the subs in these modules are public and can be accessed from other modules.

Code modules use cases include:
- Avoiding duplicate code in multiple modules.
- Sharing code between projects. For example you can create a code module that parses some file. Later you can easily reuse this module in other applications.
- Separating your application logic. Each code module can be used for a specific task. This will make your program clearer and easier to maintain.

As a code module is not tied to an activity or service it uses the calling component context when required.
For example, calling a code module sub that shows a Msgbox from an activity will work. However if you call it from a service it will fail as services are not allowed to show dialogs.

Code modules cannot catch events.
While you can use a code module to initialize a button for example:
B4X:
Sub ButtonsCreator(Text As String) As Button
  Dim b As Button
  b.Initialize("Button")
  b.Text = Text
  Return b
End Sub
From the activity module you can call:
B4X:
Activity.AddView(CodeModule.ButtonsCreator("press here"), 10dip, 10dip, 200dip, 200dip)
Now in order to catch the click event you should create a sub named Button_Click.
This sub should be located in the Activity module, as Code modules cannot catch events.
CallSub which internally uses the events mechanism cannot be used to call code module subs (which can be called directly instead).
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Erel

Busy playing around with code modules. Why has ButtonsCreator been declared as type Button in your above example if ButtonsCreator is never used inside the sub as a variable (as is the case with a fuction in for eg VBA where the function name returns a value and therefore the type that the function has been declared as)?

Sub ButtonsCreator(Text AsString) As Button
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Klaus

So if we do the following - for whatever stupid reason whatsoever!:

Sub ButtonsCreator(Text As String) As Button
Dim answer as Double
...code
...code
...code
Return answer

This will return a type Double and not a type Button. Is that correct? It means we can return any type depending on what type "answer" has been declared as?
 

klaus

Expert
Licensed User
Longtime User
Your example is wrong:
Sub ButtonsCreator(Text As String) As Button
Dim answer as Double
...code
...code
...code
Return answer

because the return object doesn't match the return object definition and you will get an error with it.

But this code will work correctly:
Sub ButtonsCreator(Text As String) As Double
Dim answer as Double
...code
...code
...code
Return answer

Yes you can return any object with a routine even arrays, but the return object must match the declaration.
One dimension array:
Sub MyRoutine(Text As String) As Double()
Dim answer(5) As Double
Two dimensions array:
Sub MyRoutine(Text As String) As Double(,)
Dim answer(5, 5) As Double
 

Johan Schoeman

Expert
Licensed User
Longtime User
The below works perfectly. GreatCircleDistance declared as type String but returning Distance declared as type Float? No error encountered....

B4X:
Public Sub GreatCircleDistance (lat_d1 As Float, lat_m1 As Float, lat_s1 As Float, lon_d1 As Float, lon_m1 As Float, lon_s1 As Float, _
                                lat_d2 As Float, lat_m2 As Float, lat_s2 As Float, lon_d2 As Float, lon_m2 As Float, lon_s2 As Float) As String
 
Dim Distance As Float
 
....code
....code
....code
Return Distance
   
End Sub
 

klaus

Expert
Licensed User
Longtime User
I think that the String object is a bit special in B4A, variables are probably tranformed internally to match the declaration.
I 'm almost sure that with a view it will not work.
You can also define the return type as Object.
For me, matching the object types is normal programming practice.
 
Top