Android Question How to use multiple modules and global variable

RushilD

Member
Licensed User
Hello, I am fairly new to BRA and would appreciate some help. I tried to search the forums but was not able to figure out the problem.

In my Project I have the following module
1. Main ->Activity Module
2. Starter -> Service module
3. AstreamsText -> Class Module
4. B4XMainPage -> Class Module
5. Overview -> New module


I have a button " "in B4XMainPage .

B4X:
Private Sub overview_Click
    CallSub(overview,"Temperature_By_Room")
End Sub

I have some variables in Main

B4X:
Sub Globals
    Private BTConnect As Button
    
    Private overview As Button   

    Dim climate_temperature As Int
    Dim climate_humidity As Int

    Public Labelbedroomtemp As Label
    Public Labelbathroomtemp As Label
    
End Sub


Problem

When button "overview" is pressed I want to call / run a function in the OVERVIEW module
In that function I was to set some text for the labels Labelbedroomtemp

When I try to access the labels in module overview , IDE shows that the variables climate_temparature and labeldedroomtemp are not declared.

How can I fix this ? How can I share the variable from MAIN to Overview ?

Thank you
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
To access a variable declared a public in Main from outside of Main you need to do this.

Main.labelbedroomtemp

If the module is a class then you need to initialise an instance of the class first.

What type is your "overview" module. is it a class or a code module?

Assuming this a B4xPages app, you problably shouldn't be declaring Views in Main anyway.
 
Upvote 0

RushilD

Member
Licensed User


Andrew thank you for the reply. "Overview" is a class module.

I am not sure if I should use a code module or a class module.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I use code modules for helper functions which are available statically (meaning you don't need to declare an instance before accessing the functions)
I have one code module called Globals which contains all global variables and consts for general use in the app.
I use class modules for everything else.

I only use Service modules when I really must e.g. Push Notifications and since moving to B4xPages, I only use the one Activity module.

It may not be the best method, but it works for me.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would suggest you to post your project as a zip file so we could have a look at it and also test it.
In B4XPages projects do not declare anything in the Main module.
As it is a B4XPages project zip it with this line on top of the code in the IDE.
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=\%PROJECT_NAME%.zip
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…