Restrictions of code modules

WAZUMBi

Well-Known Member
Licensed User
Longtime User
Is there something similar to INCLUDE or IMPORT in Basic4Android.
Code modules seem to be too restrictive.
Since I can’t call subs from code modules or reference views I am not finding them very practical for my purposes.
 

Ricky D

Well-Known Member
Licensed User
Longtime User
What do you mean by not being able to call subs or reference views?

I access views on all my activities

They need to be dimmed :

B4X:
Sub Globals
    'lShiftDate is defined in the layout
    Dim lShiftDate As Label
End Sub
.
.
.
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layoutShiftEdit")
    lShiftDate.Text = "a date needs to go here"
End Sub

'now let's assume I have code module named u

lShiftDate.Text = u.GetDate(DateTime.Now)

'in module u
Sub GetDate(datein As Long) As String
    Dim ret as String

    ret = "something"

    Return ret
End Sub

is this what you mean?

regards, Ricky
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Is there something similar to INCLUDE or IMPORT in Basic4Android.
Code modules seem to be too restrictive.
Since I can’t call subs from code modules or reference views I am not finding them very practical for my purposes.

You can call subs from code modules and even reference views by passing them as arguments.
Use CallSub to call a sub from a code module.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
WAZUMBi, note that Activity and its views are not always existing in memory when code module's code is running.
 
Upvote 0

WAZUMBi

Well-Known Member
Licensed User
Longtime User
From what I can tell
inside a code module it is not possible to referrence lShiftDate.Text directly.
You can only return a value from a sub inside the code module:

B4X:
'in module 
uSub GetDate(datein As Long) As String    
Main.lShiftDate.Text = "something"    
End Sub

is not possible

from Basic4Android documentation:

"Calls the given sub. CallSub can be used to call a sub which belongs to a different module.
However the sub will only be called if the other module is not paused. In that case an empty string will be returned.
You can use IsPaused to test whether a module is paused.
This means that one activity cannot call a sub of a different activity. As the other activity will be paused for sure.
CallSub allows an activity to call a service sub or a service to call an activity sub.
Note that it is not possible to call subs of code modules.
CallSub can also be used to call subs in the current module. Pass an empty string as the component in that case.
Example:
CallSub(Main, "RefreshData")"

from this thread:
http://www.b4x.com/forum/basic4android-getting-started-tutorials/7541-static-code-modules.html#post42971

Erel state code modules cannot catch events

All this does indeed seem to be the case.
I just want to simply insert (include) code from other files however there seems to be restrictions on how that code and global variables/views can be shared among other modules.
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
How to reference a view from a code module:

In Code Module "mod"
B4X:
Sub GetDate(datein As Long, lbl as label)
label.text = datein
End Sub

In Main:
B4X:
mod.GetDate(DateTime.Now, LblDate)

The above also shows how to call a code module sub from a main module.

You do not use CallSub TO CALL Code modules sub. Only FROM code modules.

So inside a code module you will do:
CallSub("Main","TestSub") [Call from code module to activity]
BUT from inside an activity module:
Mod.GetDate [Call from activity to code module]

Regarding events, you need to have the event handler in the Activity, but you can delegate it to the code module with a call.
 
Upvote 0

Mike Maurice

Member
Licensed User
Longtime User
Correction

The code:
Sub GetDate(datein As Long, lbl as label)
label.text = datein
End Sub

Should read:
Sub GetDate(datein As Long, lbl as label)
lbl.text = datein
End Sub
 
Upvote 0
Top