ByRef parameter for Android

blong

Active Member
Licensed User
Longtime User
I would desperately like the ByRef keyword for arguments passed to subroutines in Basic for Android. Really will make static modules better in a reuseable sense.
Please ... please
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't think that ByRef will be added as the underlying layer which is Java doesn't support it, and it will probably add too much overhead.

However you can easily mimic ByRef behavior using Type:
B4X:
Sub Process_Globals
    Type ByRefInt (val As Int)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim a, b As ByRefInt
    a.val = 2
    b.val = 3
    s1(a, b)
    Log("a = " & a.val & ", b = " & b.val) 'prints: a = 3, b = 6
End Sub

Sub s1 (n1 As ByRefInt, n2 As ByRefInt)
    n1.val = n1.val + 1
    n2.val = n2.val * 2
End Sub
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Is it possible for this to work with a variable? It doesn't appear to be able to. With the new classes I want to set the text of a view and store a ref to the string variable that set it so in a way a variable can be tied to an EditText View and when the text of it changes the variable can be changed with it. Some way of determining if they passed a string like "My String" or an actual string variable...along with if the variable is still in scope may be needed too.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
You could use a global variable as a buffer, or pass it inside a custom type
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…