Android Tutorial Variables and Subs visibility

Basic4android v2.00 adds two new access modifiers: Public and Private.
This tutorial will cover the rules which determine when a variable or a sub can be accessed from outside.

Note that the default accessibility is Public.

Variables
- Activities, services and code modules: process global variables are public by default.
Activity global variables are always private.
You can hide process global variables by using the Private modifier:
B4X:
Sub Process_Globals
 Dim ThisIsAPublicVariable As String 'public
 Public ThisIsAPublicVariableToo As String 'exactly the same as Dim.
 Private ThisIsAPrivateVariable As String 'private
End Sub

- Classes: sames as above for variables declared in Class_Globals.

Subs
- Subs declared in Activity and Service modules are private.
- Subs declared in Code modules and Class modules are public by default.
You can hide such subs with the Private modifier:
B4X:
Private Sub ThisIsAPrivateSub(x As Int)

End Sub
The Public modifier can also be used (though it doesn't have any effect):
B4X:
'Public subs
Sub S1

End Sub

Public Sub S2
 
End Sub

CallSub and CallSubDelayed can be used to call subs in other services, activities and classes. These methods can be used to access both private and public subs.
 

rbsoft

Active Member
Licensed User
Longtime User
That is great - very flexible.

Rolf
 

Djembefola

Active Member
Licensed User
Longtime User
Yes! Yes! Yes!

:sign0008:

This was the top item in my wishlist!
Thank you so much.

Next item:

Public Property Set
Public Property Get
 

mjtaryan

Active Member
Licensed User
Longtime User
Hi Erel,

I have a generally related question. How many times does a Process Global variable have to be declared?

I ask because I declared a variable in Sub Process_Global in the Activity Main and use it in both Main and in a second activity, call it "Second." The variable is boolean and I check its value in the Activity_Create in Activity Second.

During compiling I get the message that it was used before it was declared. So, I'm confused.
 

moster67

Expert
Licensed User
Longtime User
Did you check it using the module name before (main.myboolean)?

example:

B4X:
if main.myboolean = true then
 ''''
end if
 

mjtaryan

Active Member
Licensed User
Longtime User
Did you check it using the module name before (main.myboolean)?

example:

B4X:
if main.myboolean = true then
 ''''
end if

AH! :sign0161:

No, I hadn't -- didn't think I needed to, I'm so used to declaring globals only at the beginning of the package's start up module and being able to use them like any other variable. THANKS!
 

Ennesima77

Member
Licensed User
Longtime User
I to everyone,
How can I do if I need to call a sub with parameters?
B4X:
Sub SetSpinnerID(ID as int, Refill as boolean)

I tried to use CallSub(Main, "SetSpinnerID(1, False)") but it doesn't work.
How can I solve it?
 
Last edited:

Ennesima77

Member
Licensed User
Longtime User
B4X:
FillSpinner(True, False)
If the routine is in the Main module.

Best regards.

I correct the code.

B4X:
Sub SetSpinnerID(ID as int, Refill as boolean)

the sub is in the main activity, but I need to call it from a secondary activity.

I try to explain what I use it for:

In the main activity there are 2 spinner, first is the Tournament list and second is the Players List. Near the spinner there are two (button one for each spinner) and button call Tournament Activity a Players Activity where user can add/modify/delete etc..
At the end, when Tournament or Players Activity were closed, I need to refill spinner and set the tournament or players ID in the spinner.

Thanks for your kindly help!
 
Last edited:

Ennesima77

Member
Licensed User
Longtime User
You should have your data in Process_Global variables.
In the Edit activities you change the data and then when coming back the Spinners are updated.

Best regards.

Isn't possible use a sub or function like a Class Module?
Like DBUtils, I will call Main.SetSpinnerID(1,false).
I need to declare a lot a variable in this way and in a big project may be use a lot of memory resource.:sign0085:
 

klaus

Expert
Licensed User
Longtime User
When you call another activity from the Main activity you are not sure that that yo will have all the data when you return.
You should read about the activity life cycle, either in this tutorial , in the Wiki or in the Beginner's Guide.

Best regards.
 

MrKim

Well-Known Member
Licensed User
Longtime User
Did you check it using the module name before (main.myboolean)?

example:

B4X:
if main.myboolean = true then
 ''''
end if

OH MAN, I have been fighting that ALL day. I wish there was somewhere that told us the rules. I am SOOOO used to VBA. A global is a global, there is no ned to reference the declaring module unless it is a form. But I am getting used to it. Love the program.
 

mjtaryan

Active Member
Licensed User
Longtime User
I have three LIstView2 objects the items for each which I want to format using the SPListView library. Therefore, I'd like to put the formatting sub in a code module and call it from the appropriate activities. In an effort to accomplish this I tired declaring each of the ListView2 objects in Process_Globals. However, I get this error message:

----------
Parsing code. 0.05
Compiling code. Error
Error compiling program.
Error description: Cannot access activity object from sub Process_Globals.
Occurred on line: 12
Dim lstTOC As ListView2 'The actual Table of Contents
Word: listview2
----------


Does this mean a view cannot be declared in Process_Globals or is there another reason for the error? Is there a way to correct this? Thanks.
 

mjtaryan

Active Member
Licensed User
Longtime User
Yes, you cannot dim a view in Process_Globals !
But you can transfer it in the calling routine as a parameter.

Best regards.

How should I declare It in the parameter list of the called sub? That is, should I declare It as an Object, a View, a ListView2 or what? Thanks.
 
Last edited:
Top