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.
But, first: you can try : don't declare the scope in a code module and try to call the routine from an activity.
second: it is better to explicity declare the scope, as you do.
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?
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.
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.
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.
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