Android Question Global variables in all pages

stp

Member
Licensed User
Longtime User
Hi, is it possible to declare a variable in eg B4XMainpage that is accessible in all pages ?
I want to have i string variable and an numeric variable that i can access everywere.
variables:
Public myString As String
Public myNum As Int
Where must i define them ? Want to access them in my subs
 

aeric

Expert
Licensed User
Longtime User
ok then lets declare a string in main named mystring and assign to it the word testword1.
Modify the string in B4XPage1 to testword2 and read it and log it in B4XPage2.
That is what i want to do.
I don't see why you can't do that.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Here you have an example.
I made the variables available in 2 ways:
- Declared in Main Activity
- Declared in a separate Code Module
When the app start both the variables in Main and in Vars module are set to something and logged.
Then you can set both variables to something else clicking the button, that will log them again.
Go to Page 2 and you can log the variables again.
Hope it helps.
 

Attachments

  • VarsTest.zip
    15.9 KB · Views: 73
Upvote 0

klaus

Expert
Licensed User
Longtime User
I made another example declaring the variables in the B4XMainPage module, that way the principle is cross-platform.
The Starter module does exist only in B4A, and the Main modules are platform specific.
You can modify the variables in any module in Edittext views.

Only tested with B4A.

I have not looked at Sagenut's example.
 

Attachments

  • GlobalVariables.zip
    14.1 KB · Views: 82
Upvote 0

stp

Member
Licensed User
Longtime User
Here you have an example.
I made the variables available in 2 ways:
- Declared in Main Activity
- Declared in a separate Code Module
When the app start both the variables in Main and in Vars module are set to something and logged.
Then you can set both variables to something else clicking the button, that will log them again.
Go to Page 2 and you can log the variables again.
Hope it helps.
The separate code module is better. Yes it works. Thank you
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
use a code module (
I usually end up with at least 2 Code modules that contain global variables. Usually an AppData module which stores App Name and anything else relevant (could go in the Main Module but i like to keep them separate) and a DataConst module which stores Constants used to access Map Data etc.
 
Upvote 0
Top