Jim Brown Active Member Licensed User Longtime User Feb 21, 2011 #1 Does b4a have a mechanism for having variables which remember their values yet are unique to the sub (that is, not 'global' to the whole program)? Here for example, I want the variable n to be private in the test() sub yet retain its value. Currently, the value is always re-initialised each call: B4X: Sub Globals End Sub Sub Activity_Create(FirstTime As Boolean) For i=1 To 5 test Next End Sub Sub test() Dim n As Int n=n+1 Log(n) End Sub Last edited: Feb 21, 2011
Does b4a have a mechanism for having variables which remember their values yet are unique to the sub (that is, not 'global' to the whole program)? Here for example, I want the variable n to be private in the test() sub yet retain its value. Currently, the value is always re-initialised each call: B4X: Sub Globals End Sub Sub Activity_Create(FirstTime As Boolean) For i=1 To 5 test Next End Sub Sub test() Dim n As Int n=n+1 Log(n) End Sub
Erel B4X founder Staff member Licensed User Longtime User Feb 22, 2011 #3 A not very elegant workaround is to declare a global variable named: sub_test_n and then only use it in the current Sub. Upvote 0
A not very elegant workaround is to declare a global variable named: sub_test_n and then only use it in the current Sub.