Android Question use callsubdelayed for 3 argument

Discussion in 'Android Questions' started by ArminKH, Feb 28, 2015.

  1. ArminKH

    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?
     
  2. DonManfred

    DonManfred Expert Licensed User

    change the parameters. Use a map instead. Then you just need one parameter.
    the maximum parameters are two afaik
     
  3. ArminKH

    ArminKH Well-Known Member

    @DonManfred
    Can u please more explain?
    Any sample code
    Tnx
     
  4. DonManfred

    DonManfred Expert Licensed User

    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.
     
    ArminKH likes this.
  5. ArminKH

    ArminKH Well-Known Member

    now understood Thats right mr donmanfred but my english is not good
    Tnx
     
  6. ArminKH

    ArminKH Well-Known Member

    @DonManfred
    Can we use an array instead map?the array is acceptable in callsubdelayed?
     
  7. DonManfred

    DonManfred Expert Licensed User

    You can use any object.
    A list, a array.
    BUT if you use a array you are limited to one objecttype except you are using Object as type
    I suggest using a map. Here you can store int, long, string, lists, objects...
     
    ArminKH and stevel05 like this.
  8. stevel05

    stevel05 Expert Licensed User

    And retrieve them by name.
     
    ArminKH likes this.
  9. ArminKH

    ArminKH Well-Known Member

    @DonManfred

    please see my code

    Code:
    #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

    Code:
    CallSubDelayed2(Me,"Test",Label1)
    Code:
    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?
     
  10. stevel05

    stevel05 Expert Licensed 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.
     
    ArminKH likes this.
  11. ArminKH

    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
     
  12. stevel05

    stevel05 Expert Licensed User

    You don't say what error you are getting.
     
    ArminKH likes this.
  13. ArminKH

    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)
    Code:
    #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


     
  14. ArminKH

    ArminKH Well-Known Member

    if i change my code to this that worked
    Code:
    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
     
  15. stevel05

    stevel05 Expert Licensed User

    You are passing a Label to the Subroutine, the Subs Signature should be:

    Public Sub Test(ThisLabel As Label)
     
    ArminKH likes this.
  16. ArminKH

    ArminKH Well-Known Member

    understood....this is my issue

    i have a sub like this
    Code:
    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
    Code:
    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
    Code:
    Public Sub Test(ViewName As Label)
        
    Dim source As Reflector                                                           
               source.Target=ViewName
            
    Dim LineCount As Int=source.RunMethod("getLineCount")
            
    Log(LineCount)
    End Sub
    Code:
    Public Sub Test(ViewName As Button)
        
    Dim source As Reflector                                                           
               source.Target=ViewName
            
    Dim LineCount As Int=source.RunMethod("getLineCount")
            
    Log(LineCount)
    End Sub
    ?????
     
  17. ArminKH

    ArminKH Well-Known Member

    @stevel05
    tnx dear steve thats solved by using this simple solution :)
    Code:
    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
     
  18. stevel05

    stevel05 Expert Licensed User

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

    Code:
    Activity.AddView(Label1,0,0,100%X,25%Y)
             
    Dim Target As Object = Label1
            CallSubDelayed2(Me,
    "Test",Target)
    Then
    Code:
    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
     
    ArminKH likes this.
  19. ArminKH

    ArminKH Well-Known Member

    @stevel05
    woooooooow this is better now
    tnx again:)
     
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