B4J Question Sub with String List\Array as a Parameter

melliott

Member
Licensed User
Longtime User
Hello,

Can someone please post a snippet of a user defined Sub with a parameter as a List or an Array of string values. I want to pass something like this: MySub("A,B,C")

Then loop through the values in the Sub.


Thanks for your help,

Michael
 

melliott

Member
Licensed User
Longtime User
I think I answered my own question. Pass it as a string then convert the string to an array in your sub.

DoThis("A,B,C")

B4X:
Sub DoThis(sMyStringList as String)
   Dim aNames() As String = Regex.Split(",", sMyStringList)

   for i = 0 to aNames.Length-1
      log(aNames(i))
   next
End Sub
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

two solutions --- might be other as well
Option 1
B4X:
Sub AppStart (Form1 As Form, Args() As String)
...
   MySub("A,B,C")

Sub MySub(s As String)
    Dim sl() As String = Regex.Split(",", s)
    For i = 0 To sl.Length - 1
        Log(sl(i))
    Next
End Sub

Option 2
B4X:
Sub Process_Globals
    Private MyList As List
..
Sub AppStart (Form1 As Form, Args() As String)
...
    MyList.Initialize
    MyList.AddAll(Array As String("A", "B", "C"))
    MySub(MyList)
...

Sub MySub(l As List)
    For i = 0 To l.Size - 1
        Log(l.Get(i))
    Next
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Minor improvement to list sub can be written as
B4X:
Sub MySub(l as List)
   For Each s as String In l
       log(s)
   Next
End Sub
 
Upvote 0
Top