locked
SUB , array, variables ? HELP ! RRS feed

  • Question

  • i try to find a way to do this , for example:

    For X = 1 to 3

       BAR[x]()

    EndFor

    Sub BAR [1]

      do something

    endsub

    Sub BAR [2]

      do another task

    endsub

    Sub BAR [3]

      call my boss

    endsub

    Tuesday, May 3, 2016 8:11 PM
    Answerer

Answers

  • Or...

    You can call subroutines from other subroutines.   :-)

    task = 1
    SubTasks()
    
    Sub SubTasks
      If task = 1 Then
        SubTask1()
      ElseIf task = 2 then
        SubTask2()
      ElseIf task = 3 then
        SubTask3()
      EndIf
    EndSub
    
    Sub SubTask1
      'stuff..
    EndSub
    
    Sub SubTask2
      'stuff..
    EndSub
    
    Sub SubTask3
      'stuff..
    EndSub

    • Proposed as answer by litdev Wednesday, May 4, 2016 5:28 PM
    • Marked as answer by WhTurner33Editor Wednesday, May 18, 2016 5:52 PM
    Wednesday, May 4, 2016 3:44 PM
    Answerer

All replies

  • For X = 1 to 3  
      BAR()  
    EndFor
    
    Sub BAR
      If (x=1) then
        TextWindow.WriteLine("Do x="+x)
      ElseIf (x=2) then
        TextWindow.WriteLine("Do x="+x)
      ElseIf (x=3) then
        TextWindow.WriteLine("Do x="+x)
      EndIf
    EndSub

    Or

    For x = 1 to 3
      Stack.PushValue("Args",x)
      BAR()  
    EndFor
    
    Sub BAR
      arg = Stack.PopValue("Args")
      If (arg=1) then
        TextWindow.WriteLine("Do arg="+arg)
      ElseIf (arg=2) then
        TextWindow.WriteLine("Do arg="+arg)
      ElseIf (arg=3) then
        TextWindow.WriteLine("Do arg="+arg)
      EndIf
    EndSub

    Or, using extension function with a return value

    args = ""
    For x = 1 to 3
      result = LDCall.Function("BAR",x)
      TextWindow.WriteLine(result)
    EndFor
    
    Sub BAR
      If (args[1]=1) then
        TextWindow.WriteLine("Do arg="+args[1])
        return = "Result1"
      ElseIf (args[1]=2) then
        TextWindow.WriteLine("Do arg="+args[1])
        return = "Result2"
      ElseIf (args[1]=3) then
        TextWindow.WriteLine("Do arg="+args[1])
        return = "Result3"
      EndIf
    EndSub

    • Proposed as answer by litdev Wednesday, May 4, 2016 5:28 PM
    Tuesday, May 3, 2016 8:24 PM
  • Fantastic ! solution #1

    i will applied to my Moonlight Sonata program, :) thanks !

    Tuesday, May 3, 2016 9:19 PM
    Answerer
  • Or...

    You can call subroutines from other subroutines.   :-)

    task = 1
    SubTasks()
    
    Sub SubTasks
      If task = 1 Then
        SubTask1()
      ElseIf task = 2 then
        SubTask2()
      ElseIf task = 3 then
        SubTask3()
      EndIf
    EndSub
    
    Sub SubTask1
      'stuff..
    EndSub
    
    Sub SubTask2
      'stuff..
    EndSub
    
    Sub SubTask3
      'stuff..
    EndSub

    • Proposed as answer by litdev Wednesday, May 4, 2016 5:28 PM
    • Marked as answer by WhTurner33Editor Wednesday, May 18, 2016 5:52 PM
    Wednesday, May 4, 2016 3:44 PM
    Answerer
  • great solution to Coding Cat, i like it !

    thanks :)

    Wednesday, May 4, 2016 4:59 PM
    Answerer