B4J Question Returning multiple values from a sub

kostefar

Active Member
Licensed User
Longtime User
Dear All,

Is this possible like in b4a where byref is used?


B4X:
Sub AddTwo (input as Int) as Int
input = input + 2
Dim multiply as int = input * 3
Return input
End Sub

So input + 2 is returned, but how do I return multiply as well? Just a silly example.
 

XbNnX_507

Active Member
Licensed User
Longtime User
B4X:
Sub AddTwo (input As Int) As Int()
    input = input + 2
    Dim multiply As Int = input * 3
    Return Array as Int ( multiply, input )
End Sub

log ( AddTwo(2)(0) ) '<- multiply
log ( AddTwo(2)(1) '<- input
 
Last edited:
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thank
B4X:
Sub AddTwo (input As Int) As Int()
    input = input + 2
    Dim multiply As Int = input * 3
    Return Array as Int ( multiply, input )
End Sub

log ( AddTwo(2)(0) ) '<- multiply
log ( AddTwo(2)(1) '<- input


Thanks, I ended up using a map, so basically the same.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
a class or type should always a reference and a type or class object can also be returned
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form

   Type MyByRef(a As Int,b As Int)
   Type MyReturn(a As Int,b As Int)
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
  
   MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
  
   MainForm.Show

   Dim aaaa As MyByRef
   aaaa.a=1
   aaaa.b=2
  
   Dim ret As MyReturn
   ret = Test(aaaa)
  
   Log(aaaa.a)
   Log(aaaa.b)

   Log(ret.a)
   Log(ret.b)
  
End Sub

Sub Test(x As MyByRef) As MyReturn
  
   x.a=2
   x.b=3
  
   Dim ret As MyReturn
   ret.a=123
   ret.b=456
  
   Return ret

End Sub
 
Upvote 0
Top