Android Question [Resolved] What is the default scope of a sub?

Joacim

Member
Licensed User
Longtime User
I usually prefer to always add scope modifiers (Private and Public) in front of a sub or a class global variable. So I was just wondering what the default scope is for a sub if the modifier keyword is left out and likewise what the default scope is for a variable declared with the Dim keyword in the Class_Globals sub.
 

Joacim

Member
Licensed User
Longtime User
Thanks to both of you. I understand that it's recommended to always set the scope but I just wondered since the default code template you get when you create a new app doesn't have any scope modifiers on the subs it creates. Maybe that can be changed in later versions?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My previous answer was not completely accurate. Subs and variables in activities and services are always private except of variables in Process_Globals. I therefore think that it is better to leave the scope identifiers from activities and services as it may confuse. Sub Initialize in the classes template is explicitly set to be public.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
OK thanks. Are all variables that is declared in Process_Globals always global, even if you would use the Private modifier?
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
Oh, just wanted to add that I'm sorry that I didn't do a good enough search through the forums before I posted the question. I should of course have tested some other keywords and I probably would have found the article you linked to that explained all of this.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
But I still like to get an answer about my question regarding variables declared in the Process_Globals sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Intuitive i would say they are PUBLIC. Everything else dont make sense on PROCESS_GLOBAL variables.

Edit: In erels link above is the answer....

Variables
- Activities, services and code modules: process global variables are public by default.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
Thanks. I'm new to B4A and to me it'd not really intuitive that anything that you declare as Private would be Public but then again it's not really intuitive to me how anything you declare inside any sub would be anything but local. :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I like to help (also receive help :)) then I have a blank app ready to do some tests.

Perform a test to verify that takes a few seconds (ask the question a few times and waiting for an answer commits a longer time ;)).

I have tried: Private variables declared in Process_Globals are NOT visible from outside.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
Thanks, I did test it and came to the same conclusion. However now the information is also available here online for others to read. :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that it is also written in the tutorial: http://www.b4x.com/android/forum/threads/variables-and-subs-visibility.18628/


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:
Code:

Sub Process_Globals
Dim ThisIsAPublicVariable As String 'public
Public ThisIsAPublicVariableToo As String 'exactly the same as Dim.
Private ThisIsAPrivateVariable As String 'private
End Sub
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
No I did miss that part of the tutorial. Sorry to bother you all with my question. I'll leave now.
 
Upvote 0
Top