Android Question DIM, Private, Public

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi all,

Just for clarification is there an advantage to using Private or Public as apposed to Dim for declaring variables?

By default:
a variable declared by Dim in Sub Process Globals is available to all modules.
a variable declared by Dim in Sub Globals is available only to the module in which it is declared.

A variable declared by Private in Sub Process Globals is only available to the module in which it is declared.
QUESTION 1: Is there an advantage to declare a variable using Private in Sub Process Globals over declaring the same variable using Dim in Sub Globals?

QUESTION 2: Is there a protocol to determine when and where to use these three declaration terms?


Regards Roger
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks for the reply Erel your answer makes things a bit clearer.

My concern is that I am missing something by only using Dim.

1. I can see no reason to use either Private or Public in Sub Globals
2. I can see no reason to use Public in Sub Process Globals
3. Can you give me a practical example that would drive the use of Private in Sub Process Globals

Regards Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
3) AFAIK a process global variable cannot be private.
2) you dont need to. DIM is the same here as all process globals must be public/are public
1) variables marke as private in globals of main activity cannot be accessed from other activities. Public variables can.

PS: I hope i did not tell bullshit :D
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks DonManfred,

3) Code from Klaus's Tape Calculator Note the last line.
B4X:
Sub Process_Globals
' Designer Scripts AutoScale formula
'    Delta = ((100%x + 100%y) / (320dip + 430dip) - 1)
'    Rate = 0.5 'value between 0 to 1.
'    Scale = 1 + Rate * Delta

    Public Rate As Double
    Rate = 0.3 'value between 0 to 1.
    Private cScaleX, cScaleY, cScaleDS As Double
End Sub

Regards Roger
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't use Dim in any of the global subs. Use either public or private.

1. In Sub Globals (which is only available in Activity modules) you should only use private.
2. In other "global subs" (Process_Globals or Class_Globals) you should use private or public based on the requirements.
3. Most variables in process_globals should be private. That is unless you want to access them from a different module.

Remember that only views (and other activity objects) should be declared in Sub Globals.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Thanks DonManfred,

3) Code from Klaus's Tape Calculator Note the last line.
B4X:
Sub Process_Globals
' Designer Scripts AutoScale formula
'    Delta = ((100%x + 100%y) / (320dip + 430dip) - 1)
'    Rate = 0.5 'value between 0 to 1.
'    Scale = 1 + Rate * Delta

    Public Rate As Double
    Rate = 0.3 'value between 0 to 1.
    Private cScaleX, cScaleY, cScaleDS As Double
End Sub

Regards Roger

The code you are refering to is in the Scale Code module.
These variables, Private cScaleX, cScaleY, cScaleDS As Double, are private to the Scale Code module accessible from any Sub in this module but no need to access them from any other module.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Don't use Dim in any of the global subs. Use either public or private.

1. In Sub Globals (which is only available in Activity modules) you should only use private.
2. In other "global subs" (Process_Globals or Class_Globals) you should use private or public based on the requirements.
3. Most variables in process_globals should be private. That is unless you want to access them from a different module.

Remember that only views (and other activity objects) should be declared in Sub Globals.
Thanks Erel, That really nails it down.
Regards Roger
 
Last edited:
Upvote 0
Top