Android Question Indirect call of a Sub Routine

FERNANDO SILVEIRA

Active Member
Licensed User
Hello guys,

I have this test program where I'm trying to reach a SUB by passing its name as arg of another an initial SUB (see example below).
I'm using callsub2. Documentation says I have to pass component name (which in my case is Main), but syntax checker says that Main is an undefined variable and, when I code "Main" instead, it ignores the routine to be called and does nothing.

B4X:
Sub Activity_Create(FirstTime As Boolean)
  
    rotina1("rotina2", "Teste1")
    rotina1("rotina3", "Teste2")
  
End Sub

Sub rotina1(proc As String, parm As String)
    Log ("rotina1 - parm: " & parm)
    CallSub2(main, proc, parm )        ' <== NOT CALLING THE DESIRED SUB ROUTINE
End Sub

Sub rotina2(parm1 As String)
    Log("rotina 2 - parm: " & parm1)
End Sub

Sub rotina3(parm1 As String)
    Log("rotina 3 - parm: " & parm1)
End Sub

Any clues on how to fix it?
Regards.
 

FERNANDO SILVEIRA

Active Member
Licensed User
If that code is inside the Main module then you use Me to refer to the component.

Hello Roycefer,

Using Me, don't cause syntax error but still doesn't call rotina2 and rotina3... see log below... I even have a for/next loop to repeat the call 5 times but nothing happens.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    For i = 1 To 5
        Log("Main try " & i)
        rotina1("rotina2", "Teste1")
        rotina1("rotina3", "Teste2")
    Next
End Sub

Sub rotina1(proc As String, parm As String)
    Log ("  rotina1 - parm: " & parm)
    CallSub2(Me, proc, parm )        ' <== NOT CALLING THE DESIRED SUB ROUTINE
End Sub

Sub rotina2(parm1 As String)
    Log("   rotina2 - parm: " & parm1)
End Sub

Sub rotina3(parm1 As String)
    Log("   rotina3 - parm: " & parm1)
End Sub


** Activity (main) Create, isFirst = true **
Main try 1
rotina1 - parm: Teste1
rotina1 - parm: Teste2
Main try 2
rotina1 - parm: Teste1
rotina1 - parm: Teste2
Main try 3
rotina1 - parm: Teste1
rotina1 - parm: Teste2
Main try 4
rotina1 - parm: Teste1
rotina1 - parm: Teste2
Main try 5
rotina1 - parm: Teste1
rotina1 - parm: Teste2
** Activity (main) Resume **
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It happens because you are callling routines from Activity_Create.

Try the same from Activity_Resume, it should work.

You could also write so, in Activity_Create:
B4X:
For i = 1 To 5
    Log("Main try " & i)
    CallSubDelayed3(Me, "rotina1", "rotina2", "Teste1")
    CallSubDelayed3(Me, "rotina1", "rotina3", "Teste2")
Next
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
It happens because you are callling routines from Activity_Create.

Try the same from Activity_Resume, it should work.

You could also write so, in Activity_Create:
B4X:
For i = 1 To 5
    Log("Main try " & i)
    CallSubDelayed3(Me, "rotina1", "rotina2", "Teste1")
    CallSubDelayed3(Me, "rotina1", "rotina3", "Teste2")
Next

Thank you, Mario
It worked fine calling from Activity_Resume.

Do you know what is special about Activity_Create that makes it behave like this?


** Activity (main) Resume **
Main try 1
rotina1 - parm: Teste1
rotina2 - parm: Teste1
rotina1 - parm: Teste2
rotina3 - parm: Teste2
Main try 2
rotina1 - parm: Teste1
rotina2 - parm: Teste1
rotina1 - parm: Teste2
rotina3 - parm: Teste2
Main try 3
rotina1 - parm: Teste1
rotina2 - parm: Teste1
rotina1 - parm: Teste2
rotina3 - parm: Teste2
Main try 4
rotina1 - parm: Teste1
rotina2 - parm: Teste1
rotina1 - parm: Teste2
rotina3 - parm: Teste2
Main try 5
rotina1 - parm: Teste1
rotina2 - parm: Teste1
rotina1 - parm: Teste2
rotina3 - parm: Teste2


Regards,
Fernando
 
Upvote 0
Top