Android Question Global variables in all pages

stp

Active 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
 

epiCode

Active Member
Licensed User
You can set a public variable in Starter module and access it like Starter.mystring
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
Thank you i found that also in another post but dont know how to do that !!!
Is there any example in the forum ?
 
Upvote 0

epiCode

Active Member
Licensed User
Thank you i found that also in another post but dont know how to do that !!!
are you asking step by step directions or sharing that solution is already known to you ?
 
Upvote 0

pliroforikos

Active Member
Licensed User
If you have some public variables for example in MainPage you can use

You can use B4XPages.MainPage.<variable name> from other modules in order to acces it.
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
If you have some public variables for example in MainPage you can use

You can use B4XPages.MainPage.<variable name> from other modules in order to acces it.
That is for accessing variables from buttons or labels. i found that also in 3pages example.
I want a string variale and an numeric variable to be accessed in whole pages.
 
Upvote 0

epiCode

Active Member
Licensed User
That is for accessing variables from buttons or labels. i found that also in 3pages example.
I want a string variale and an numeric variable to be accessed in whole pages.
It works for variables too
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
If you have some public variables for example in MainPage you can use

You can use B4XPages.MainPage.<variable name> from other modules in order to acces it.
No i mean i found onother reference
are you asking step by step directions or sharing that solution is already known to you ?
i mean i found another reference about Starter module but dont know how to do that!!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You can access any variable or object from another module/page as long as they are declared public.

Like, if you have in B4XMainPage a Public myLabel as B4XView
you can use in any module/ page the following format hostModule.ObjectName.Property = Value, which in this case would be B4XPages.MainPage.myLabel.text ) "any text you want"
B'XPages is the object that holds the reference to all pages in the stack, MainPage is the only page that must exist in the stack, but you can access any page using the same methodology
But remember, the actual change won't be visible until the page is brought back to foreground!
 
Upvote 0

epiCode

Active Member
Licensed User
Want to have string variable or num variable like in c or any other language
This is not possible that you just type the name of variable and use it in any module.
When you are outside of the module in which it is declared you need to let the compiler know where is your module declared.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
This is not possible that you just type the name of variable and use it in any module.
Nop... it doesn't work that way.... is like sending a letter to someone, you need to include the whole address, not just the destinatary's name!

[EDIT] sorry @epiCode thought you were the OP
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
example:
Label2.text=$"Hello ${B4XPages.MainPage.myvar}!"$

i know that. But i dont want that. Want to have string variable or num variable like in c or any other language
This code should work.
Are you declaring it as Public or Dim inside Sub Class_Globals?
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
This code should work.
Are you declaring it as Public or Dim inside Sub Class_Globals?
Yes that code works but i wanted something like that
code:
Public myString As String
Public myNum As Int
i work now to fix it with onother solution. Thank you
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yes that code works but i wanted something like that
code:
Public myString As String
Public myNum As Int
i work now to fix it with onother solution. Thank you
Nothing wrong to fix.
Another alternative is declaring the variables on Main.
B4i and B4J have no Starter.

If you think the code is long when referencing from B4XPages object, you can declare a new variable and assign the value to it.
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
Nothing wrong to fix.
Another alternative is declaring the variables on Main.
B4i and B4J have no Starter.

If you think the code is long when referencing from B4XPages object, you can declare a new variable and assign the value to it.
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.
 
Upvote 0

stp

Active Member
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.
So there is something to fix. (find a solution)
 
Upvote 0
Top