Android Question life cycle of var

Gianni Sassanelli

Active Member
Licensed User
Longtime User
hello,
i need to declare a var in a Sub and i need that this var is visible from all sub called after the sub in witch the var is declared.
view the example code

B4X:
Sub s_main
'this is the main sub that call in sequence s_1 and S_3
'the S_1 call the s_2
' I need v12 var is visible from S_1 and s_2 but not from s_3
'I don't pass my var as as parameter from s_1 to s_2
'Is this possible ?
'thank's


    s_1
    s_3
End Sub
Sub s_1
    Private v12 As Int
    v12 = 100
    s_2
End Sub
Sub s_2
    'need to use v12 var in this sub but not in other subs
    'v12 = is not visible here
End Sub
Sub s_3
   'i don't want that v_12 is visible from this sub
  
End Sub
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
Maybe I do not understand what you are trying to do, but why don't you pass v12 to sub2 as an argument?

B4X:
Sub s_1
    Private v12 As Int
    v12 = 100
    s_2(v12)
End Sub
Sub s_2(n as int)
    'need to use v12 var in this sub but not in other subs
    'v12 = is not visible here
    ... no, but [n] would be
End Sub
 
Upvote 0

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Maybe I do not understand what you are trying to do, but why don't you pass v12 to sub2 as an argument?

i don't pass v12 as argument because i have many arguments to pass

I have a sub in witch i use many var but this var can be modified in many mode for different scenario
the sub have a common section + specific section + common section
now i have all code in s_main proc but i want to separate for better readable of code


for example
B4X:
Sub s_main
    Dim nO, nI, nB As Int
    Dim mode     As Int
    Dim cr         As Cursor
    Select mode
    Case 1      ' select only order
        'all select ise very long procedure and bls code
        s_order
    Case 2     ' select only invoice
        'all select ise very long procedure and bls code
        s_invoice     
    Case 3     ' select only budget
        'all select ise very long procedure and bls code
        s_budget     
    End Select
   
   
    ' i need to exec other code that require cr, nO, nI ecc 
    ' if i pass more argument i need to return them
    ' now i return many parameter by return a list
   
   
End Sub
Sub s_order
    '....msany code
End Sub
Sub s_invoice
    'many code
End Sub
Sub s_budget
    Dim cr As Cursor
    Dim SQL As SQL
    Dim nO
    cr = SQL.ExecQuery("/*long query*/")
    nO = SQL.ExecQuerySingleResult("select sum(orders) from .....")
    '.....
End Sub
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Okay - then maybe this idea will not work either, but I will just mention it.

An easy way to parcel up a lot of parameters (useful, for instance, in "callSubDelayed()" when you have a limited number of arguments), is to use a custom type ......
B4X:
Type order (cust as str, accnt as str, orderNum as int, items as List, ...)

Sub processOrder(details as order)
....

This at least makes the code readable, and logical, but I realise that it might not meet your objective in this case.
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Okay - then maybe this idea will not work either, but I will just mention it.

An easy way to parcel up a lot of parameters (useful, for instance, in "callSubDelayed()" when you have a limited number of arguments), is to use a custom type ......
B4X:
Type order (cust as str, accnt as str, orderNum as int, items as List, ...)

Sub processOrder(details as order)
....

This at least makes the code readable, and logical, but I realise that it might not meet your objective in this case.

Yep, very C++ like with data structs.
 
Upvote 0
Top