Android Question [Solved] How to call a public variable at B4XPages from Another B4xPages

AndroidMadhu

Active Member
Licensed User
Hello,
I am not able to call a public variable from another B4xPages.
The below is the code which I am calling from another page. :
B4X:
Dim mp As B4XMainPage = B4XPages.GetPage("MainPage")
Log(mp.cloc)
Log(B4XPages.MainPage.cloc)

The variable I declared at MainPage
B4X:
Sub Process_Globals
Public ActionBarHomeClicked As Boolean
Public cloc As String
End Sub

But I am not able to call cloc from another page.

Please advice

Thanks
 

mangojack

Expert
Licensed User
Longtime User
I have not played with B4XPages much ...
but I am sure any public variables (or Views) , should be declared in the Page Class_Globals , Not in the Main Activity Process_Globals.

In your case Public cloc As String should be declared in B4XMainPage Class_Globals.
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Hello @AndroidMadhu

The best way to do this is with a code module.

1621093375972.png


B4X:
Sub Process_Globals
    Dim Name As String
End Sub

The variables of this module can be called anywhere in your application.

B4X:
Sub AddName
    MiModulo.Name="Bladimir"
End Sub

This is a good solution.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
From this thread:

  1. We can directly access public global variables of other pages.

For example:

B4XMainPage:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    Public MyVariable As string

End Sub

In other pages, you can do:

B4X:
Dim variable as string
variable = B4XPages.MainPage.MyVariable
 
Upvote 0
Top