Android Question use callsubdelayed for 3 argument

ArminKH

Well-Known Member
Hi
I have a sub like this
Sub test(v as view,arg1 as boolean,arg2 as float)
Now how can i use callsubdelayed for this sub?
 

DonManfred

Expert
Licensed User
Longtime User
change the parameters. Use a map instead. Then you just need one parameter.
the maximum parameters are two afaik
 
Upvote 0

ArminKH

Well-Known Member
You are long enough here to understand me. If not; learn the basics

Instead of using three parameters (or 100) you use a map and put all parameters inside this map.
The called sub takes this map and reads all needed informations from this map.

now understood Thats right mr donmanfred but my english is not good
Tnx
 
Upvote 0

ArminKH

Well-Known Member
@DonManfred

please see my code

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private Label1 As Label
    Dim Map1 As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Label1.Initialize("")
        Label1.Color=Colors.White
        Label1.TextColor=Colors.Black
        Label1.Text="THIS IS TEST TEXT"
        Activity.AddView(Label1,0,0,100%X,25%Y)
      
        Setup(Label1,True,0.6,False)
        CallSubDelayed(Me,"Test")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Public Sub Setup(A As View,B As Boolean,C As Float,D As Boolean)
    Map1.Initialize
        Map1.Put("A",A)
        Map1.Put("B",B)
        Map1.Put("C",C)
        Map1.Put("D",D)
End Sub
Public Sub Test
    Dim source As Reflector                                                             
           source.Target=Map1.GetValueAt(0)
        Dim LineCount As Int=source.RunMethod("getLineCount")
  
        Log(LineCount)
        Log(Map1.GetValueAt(1))
        Log(Map1.GetValueAt(2))
        Log(Map1.GetValueAt(3))

End Sub

this work now but i want use this in class module and in class module map is a process global variable then it cannot hold activity objects (like views)
now if i change a bit my codes and use following code thats not worked

B4X:
        CallSubDelayed2(Me,"Test",Label1)

B4X:
Public Sub Test(ViewName As View)
    Dim source As Reflector                                                             
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
  
        Log(LineCount)
        Log(Map1.GetValueAt(1))
        Log(Map1.GetValueAt(2))
        Log(Map1.GetValueAt(3))

End Sub

what is the solution?why callsubdelayed2 not worked in my codes?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The point of passing variables to a subroutine is that the variables are Local, you should only Dim the Map and fill it when you want to use it, otherwise you could access the global variables directly. You can't use it to circumvent the built in checking for coding errors.
 
Upvote 0

ArminKH

Well-Known Member
@stevel05
forget map
why CallSubDelayed2(Me,"TestSub",Label1) not worked but CallSubDelayed2(Me,"TestSub","String") works?
we cant use a view in callsubdelayed?
please explain with simpler english if is possible tnx
 
Upvote 0

ArminKH

Well-Known Member
@stevel05
java.lang.Exception: Sub test signature does not match expected signature.

if is possible please try this(just copy and compile)
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Label1.Initialize("Label1")
        Label1.Color=Colors.White
        Label1.TextColor=Colors.Black
        Label1.Text="THIS IS TEST TEXT"
        Activity.AddView(Label1,0,0,100%X,25%Y)
     
        CallSubDelayed2(Me,"Test",Label1)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Public Sub Test(ViewName As View)
    Dim source As Reflector                                                            
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub

 
Upvote 0

ArminKH

Well-Known Member
if i change my code to this that worked
B4X:
Public Sub Test(ViewName As Label)
    Dim source As Reflector                                                            
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub
but i dont know user cast which type of view
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You are passing a Label to the Subroutine, the Subs Signature should be:

Public Sub Test(ThisLabel As Label)
 
Upvote 0

ArminKH

Well-Known Member
You are passing a Label to the Subroutine, the Subs Signature should be:

Public Sub Test(ThisLabel As Label)
understood....this is my issue

i have a sub like this
B4X:
Public Sub Test(ViewName As View)
    Dim source As Reflector                                                          
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub
when i use this sub for each type of view thats work perfectly
but if i use this sub in activity_create the sub always return 0 value
now if i use Callsubdelayed2 like following code thats worked perfectly in activity_create
B4X:
Sub Activity_Create(FirstTime As Boolean)
        CallSubDelayed2(Me,"Test",Label1)
End Sub
Public Sub Test(ViewName As Label)
    Dim source As Reflector                                                           
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub

but now if the type of view be button or... what is the solution?
we should create a sub for each view type? like this
B4X:
Public Sub Test(ViewName As Label)
    Dim source As Reflector                                                           
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub
B4X:
Public Sub Test(ViewName As Button)
    Dim source As Reflector                                                           
           source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub

?????
 
Upvote 0

ArminKH

Well-Known Member
@stevel05
tnx dear steve thats solved by using this simple solution :)
B4X:
Sub Activity_Create(FirstTime As Boolean)      
    CallSubDelayed(Me,"RunTest")
End Sub
Sub RunTest
    Test(Label1)
End Sub
Public Sub Test(ViewName As View)
    Dim source As Reflector                                                           
        source.Target=ViewName
        Dim LineCount As Int=source.RunMethod("getLineCount")
        Log(LineCount)
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try casting the object before you send it, and reverting it once it's received:

B4X:
        Activity.AddView(Label1,0,0,100%X,25%Y)
         Dim Target As Object = Label1
        CallSubDelayed2(Me,"Test",Target)

Then
B4X:
Public Sub Test(Target As Object)
    Dim T As View = Target
    Dim Source As Reflector                                                          
    Source.Target = T
    Dim LineCount As Int=Source.RunMethod("getLineCount")
    Log(LineCount)
End Sub
 
Upvote 0
Top