Android Question Declaring variables with Private in a regular Sub

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I noticed some users do this eg:

B4X:
Sub GetMaxValue(arrInt() As Int) As Int

    If arrInt.Length = 1 Then Return arrInt(0)

    Private i As Int 'not sure what the point/benefit is of declaring this with Private
    Dim iResult As Int

    iResult = arrInt(0)

    For i = 1 To arrInt.Length - 1
        If arrInt(i) > iResult Then iResult = arrInt(0)
    Next

    Return iResult

End Sub
[CODE]

Could somebody explain what the benefit/purpose is to declare the variable i using Private rather than Dim in this case?

RBS
 

BlueVision

Active Member
Licensed User
Longtime User
Useless in my eyes. It makes no difference to me if you declare a variable inside a regular sub with the Private Statement or with the Dim Statement. At the end, the variable "i" is a private integer variable only valid for this sub. But maybe there is a reason I don't know.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Useless in my eyes. It makes no difference to me if you declare a variable inside a regular sub with the Private Statement or with the Dim Statement. At the end, the variable "i" is a private integer variable only valid for this sub. But maybe there is a reason I don't know.
Exactly what I thought, but maybe we are missing something.

RBS
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Since 'i' in this case is an iterator index it is the one variable that is automatically declared as Int.
You don't need to declare it at all. Iterator indices are always local.

B4X:
Private Sub test
    For i = 0 To 1
        Log (GetType(i))        'java.lang.Integer
    Next
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Since 'i' in this case is an iterator index it is the one variable that is automatically declared as Int.
You don't need to declare it at all. Iterator indices are always local.

B4X:
Private Sub test
    For i = 0 To 1
        Log (GetType(i))        'java.lang.Integer
    Next
End Sub
OK, thanks, that is useful to know.
What if i was declared globally as say String (I understand that is very unlikely)?
What if i was declared in another Sub as say: Private i As String?
Is there any point in declaring a local Sub variable with Private in any case?

RBS
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Is there any point in declaring a local Sub variable with Private in any case?

No. Although Private works too. Public declarations inside a non-global sub are actually treated as Private.
To avoid that contradiction, I always declare local variables using Dim.
The Sub declaration on the other hand could be Public or Private (by default).
For clarity I always specify Private Sub or Public Sub.

By the way, it is extremely easy to tests these things oneself.

Edit: AS @LucaMs pointed out. If you don't specify Public or Private for a Sub, the default is Public.
Good to know, although I always specify it.
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
For completeness. This is something I use all the time, but may not be well-known.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Private x() As Float    'Declared as Float Array of zero length
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    Log(x.Length)            '0
   
    'Dim x(10) As Int        'throws error
    Dim x(10) As Float        'works  and keeps x as global
    Log(x.Length)            '10
    x(5) = 9999
    Log(x(5))                '9999
   
    '...
   
    Dim x(100) As Float     'works, resets all values in x to 0, keeps x as global
    Log(x.Length)        '100
    Log(x(5))                '0
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If I remember correctly, Dim is for backward compatibility or making user coming from VB6 or BASIC background feel more at home.
For programmer coming from other languages maybe they feel more familiar with Private.

Eventually both Private and Dim will be converted to private scope in Java when it is inside a Sub.

No right or wrong.

I prefer to use Dim as less typing and feel less stress.

I also trying to practice standardisation to my coding style where possible.

Edit: Check section 2.3.1 Declaring Variables in B4X Guide
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The 'gotcha' is that Public declarations inside a non-global sub are also treated as Private.
Saying "Private" inside Sub seems to imply (to me) that "Public" is an option when it isn't, even though is is syntactically accepted.

And... Dim is shorter to type :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Be careful not to think that a variable declared in a Sub with Private does not affect a possible variable with the same name (and type) declared at module level. Changing the value of this variable in the Sub changes the value of the one declared at the module level, as they are the same variable.

The Sub declaration on the other hand could be Public or Private (by default).
By default it is Public.


BTW, the function in the first post is unnecessarily long:
B4X:
Sub GetMaxValue(arrInt() As Int) As Int
    Dim iResult As Int

    For i = 0 To arrInt.Length - 1
        iResult = Max(iResult, arrInt(i))
    Next

    Return iResult
End Sub
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Another example:
B4X:
Sub GetMaxValue(arrInt() As Int) As Int
    Try
        Dim L As List = arrInt
        L.Sort(False)
        Return L.Get(0)
    Catch
        Log(LastException)
        Return -1
    End Try
End Sub

Log( GetMaxValue(Null) )
Log( GetMaxValue(Array As Int(2, 9, 5, -1, 3)) )
Log( GetMaxValue(Array As Int()) )
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Be careful not to think that a variable declared in a Sub with Private does not affect a possible variable with the same name (and type) declared at module level. Changing the value of this variable in the Sub changes the value of the one declared at the module level, as they are the same variable.


By default it is Public.


BTW, the function in the first post is unnecessarily long:
B4X:
Sub GetMaxValue(arrInt() As Int) As Int
    Dim iResult As Int

    For i = 1 To arrInt.Length - 1
        iResult = Max(iResult, arrInt(i))
    Next

    Return iResult
End Sub
>>
BTW, the function in the first post is unnecessarily long

It was just an example.
Your Sub GetMaxValue doesn't look quite OK to me.

RBS
 
Upvote 0
Top