Optional in calling parameters

drj

Member
Licensed User
Longtime User
I am converting a large piece of parsing code from vb that uses calls to a msg routine with a variable number of parameters. I would not have done it that way myself but I am stuck with it. Is there any way to do this? i.e. a variable number of calling parameters?

Thanks


Private Sub getMsg(Id, Optional p1 = 0, Optional p2 = 0, Optional p3 = 0, Optional p4 = 0)
 

stevel05

Expert
Licensed User
Longtime User
If you can't rewrite it you could use an array, something like:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")


   TestParam(Array As Object(1,"P2",Activity,4,5))

End Sub

Sub TestParam(Params() As Object)

Dim Param1 As Int = 0
Dim Param2 As String = ""
Dim Param3 As Activity
Dim Param4 As Int = 0
Dim Param5 As Int = 99
Dim Param6 As String ="Default String"
Dim Param7 As Int = 0
Dim Param8 As Int = 0
Dim Param9 As Int = 0

If Params.Length > 0 Then Param1 = Params(0)
If Params.Length > 1 Then Param2 = Params(1)
If Params.Length > 2 Then Param3 = Params(2)
If Params.Length > 3 Then Param4 = Params(3)
If Params.Length > 4 Then Param5 = Params(4)
If Params.Length > 5 Then Param6 = Params(5)
If Params.Length > 6 Then Param7 = Params(6)
If Params.Length > 7 Then Param8 = Params(7)
If Params.Length > 8 Then Param9 = Params(8)

Log(Param1&" "&Param5&" "&Param6&" "&Param9)

End Sub
 
Last edited:
Upvote 0
Top