Would love to have the ability to define local variables that have persistent value, like private globals, but have scope only in the sub where they are declared. The declaration could still be "Private", with the scope implicit from the location of the declaration, or it could be something distinctive like "Static" or "Persistent".
For example:
This is, of course, a nonsense example, but it should serve to illustrate the concept. In the proposed approach, myvar is, in all respects, the same as three global private variables except it is declared within the sub where it is used (and ideally would have visibility/scope only within that sub).
A compilation implementation could be as follows: when the Static keyword is encountered, generate a Java name that combines the sub with the static variable, e.g. examplesub1_myvar, so for Java purposes it could be treated as any private global, while having the benefits of scope and modularity within B4X.
Using private globals within B4X as in the "Current Method" example certainly works, but for large modules with lots of subs it can become unwieldy, messy, and harder to maintain. One may do a lot of scrolling between the Globals section and subs that are scattered around, because the variable declarations may be very distant from the sub where they are used. The proposed approach is more modular and maintainable. It's also easier to make cohesive subs that can be more easily copied and pasted from module to module, or from project to project. Also, this approach allows the Globals section to be focused on only those variables that actually are used in multiple subs within the module, or are publicly accessible.
For example:
Current Method:
Sub Process_Globals
Private globalvar as string = ""
Private var1 As Int
Private var2 As Int
Private var3 As Int
End Sub
Sub examplesub1
var1 = var1 + 1
globalvar = "example1 count: " & var1
End Sub
Sub examplesub2
var2 = var2 + 1
globalvar = "example2 count: " & var2
End Sub
Sub examplesub3
var3 = var3 + 1
globalvar = "example3 count: " & var3
End Sub
Proposed Method:
Sub Process_Globals
Private globalvar as string = ""
End Sub
Sub examplesub1
Static myvar As Int
myvar = myvar + 1
globalvar = "example1 count: " & myvar
End Sub
Sub examplesub2
Static myvar As Int
myvar = myvar + 1
globalvar = "example2 count: " & myvar
End Sub
Sub examplesub3
Static myvar As Int
myvar = myvar + 1
globalvar = "example3 count: " & myvar
End Sub
This is, of course, a nonsense example, but it should serve to illustrate the concept. In the proposed approach, myvar is, in all respects, the same as three global private variables except it is declared within the sub where it is used (and ideally would have visibility/scope only within that sub).
A compilation implementation could be as follows: when the Static keyword is encountered, generate a Java name that combines the sub with the static variable, e.g. examplesub1_myvar, so for Java purposes it could be treated as any private global, while having the benefits of scope and modularity within B4X.
Using private globals within B4X as in the "Current Method" example certainly works, but for large modules with lots of subs it can become unwieldy, messy, and harder to maintain. One may do a lot of scrolling between the Globals section and subs that are scattered around, because the variable declarations may be very distant from the sub where they are used. The proposed approach is more modular and maintainable. It's also easier to make cohesive subs that can be more easily copied and pasted from module to module, or from project to project. Also, this approach allows the Globals section to be focused on only those variables that actually are used in multiple subs within the module, or are publicly accessible.
Last edited: