B4J Question Sub with String List\Array as a Parameter

Discussion in 'B4J Questions' started by melliott, Apr 21, 2015.

  1. melliott

    melliott Member Licensed 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
     
  2. melliott

    melliott Member Licensed 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")

    Code:
    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
     
  3. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    two solutions --- might be other as well
    Option 1
    Code:
    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
    Code:
    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
     
  4. melliott

    melliott Member Licensed User

    Rob thanks for posting. The List option looks very useful. Maybe even simpler.
     
  5. Daestrum

    Daestrum Well-Known Member Licensed User

    Minor improvement to list sub can be written as
    Code:
    Sub MySub(l as List)
       
    For Each s as String In l
           
    log(s)
       
    Next
    End Sub
     
    rwblinn likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice