What is the link with Sender?

enonod

Well-Known Member
Licensed User
Longtime User
Values have been saved in the Label.Tag in the Click sub.
When I Click the Label I do not get the original values back from snd.Tag.
It seems clear that it is occuring because of the change to 'lPos' in the Tick sub, because the new Tag values match that.
I cannot see why changing the values of 'lPos' in the Tick Sub is changing snd.Tag
What have I done wrong or what don't I know please?

B4X:
Sub Process Globals
    Type Tagg(C As Byte, R As Byte, colour As Int)
End Sub

Sub Globals
    Dim lPos As Tagg
    Dim snd as Label
End Sub

Sub live(c,r As Byte, sEvent As String)
   Dim bx As Label
   Dim lTag As Tagg
   bx.Initialize(sEvent)
   lTag.c=c : lTag.r=r
   lTag.colour=aColours(Rnd(1,6))
   bx.Tag=lTag
   bx.Color=lTag.colour
   pnl.AddView(bx, ...,...,...)
...
End Sub

Sub Bx_Click
   snd=Sender : lPos=snd.Tag
   col=lPos.C : row=lPos.R
   dirC=0 : dirR=1
   ...
End Sub

Sub Ticker_Tick
...
     lPos.C = lPos.C + dirC*-1  :  lPos.R = lPos.R + dirR*-1
...
End Sub
 
Last edited:

enonod

Well-Known Member
Licensed User
Longtime User
I really need to bump this please.
I have a feeling that I read something in tutorials or on the forum that might answer this but I cannot find it.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Hi Enonod,

I tried to find the failure with this code:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Type Tagg(C As Byte, R As Byte, colour As Int)
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim lPos As Tagg
    Dim snd As Label
   Dim dirC, dirR As Int
   Dim Ticker As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   live("10","20","Bx")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub live(c,r As Byte, sEvent As String)
    Dim bx As Label
    Dim lTag As Tagg
    bx.Initialize(sEvent)
    lTag.c=c : lTag.r=r
    'lTag.colour=aColours(Rnd(1,6))
   lTag.colour=Colors.Red
    bx.Tag=lTag
    bx.Color=lTag.colour
    Activity.AddView( bx,1,1,10%x,10%y)
   Log(lTag.C)
   Log(lTag.R)
   Log(lTag.colour)
End Sub

Sub Bx_Click
    snd=Sender : lPos=snd.Tag
    col=lPos.C : row=lPos.R
     Log(col)
   Log(row)
   Log(snd.Tag) 
   dirC=0 : dirR=1

End Sub

Sub Ticker_Tick

     lPos.C = lPos.C + dirC*-1  :  lPos.R = lPos.R + dirR*-1

End Sub

I get no change in snd.tag. Maybe my code is not right?
Sorry.
Regards
Mark
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Hello Mark. Thank you for your trouble and time.
Your code did not change send, but had a simple flaw, the timer was not running to alter lPos.c. My code was only illustrative.
I have modified the code and put an extra Log in.
You will see that the sender is incremented.
I am grateful for the provision of a small testable piece of code which I had not managed to do.
If you can help further I will be pleased.
i.e. is lPos not a copy but a clone or has the same reference or something?
Regards
Don
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type Tagg(C As Byte, R As Byte, colour As Int)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim lPos As Tagg
      Dim col,row As Byte
    Dim snd As Label
    Dim dirC, dirR As Int
    Dim Ticker As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
Ticker.Initialize("Ticker",50)
    live(10,20,"Bx")
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub live(c,r As Byte, sEvent As String)
    Dim bx As Label
    Dim lTag As Tagg
    bx.Initialize(sEvent)
    lTag.c=c : lTag.r=r
    'lTag.colour=aColours(Rnd(1,6))
    lTag.colour=Colors.Red
    bx.Tag=lTag
    bx.Color=lTag.colour
    Activity.AddView( bx,1,1,10%x,10%y)
    Log(lTag.c)
    Log(lTag.r)
    Log(lTag.colour)
End Sub

Sub Bx_Click
    snd=Sender : lPos=snd.Tag
    col=lPos.C : row=lPos.R
      Log(col)
    Log(row)
    Log(snd.Tag) 
    dirC=0 : dirR=1
Ticker.Enabled=True
End Sub

Sub Ticker_Tick

     lPos.C = lPos.C + 1 'lPos.C + dirC*-1  :  lPos.R = lPos.R + dirR*-1
       Ticker.Enabled=False
Log(lPos.C)
End Sub
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Hello Don,

I tried your code. The timer didn't work. lpos.C was not updated until I clicked, logical. I think you may need to rethink a little. Have a look at this post http://www.b4x.com/forum/basic4android-updates-questions/21382-object-tag-issue.html#post123622.

Custom tags are passed by reference, thats why you change. Thats what I understood. You reference an array, which you then change in the Ticker sub. Are you following me. Not knowing what you are trying to do, I can only help a little.



Regards
Mark
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Still trying Don,

"Dim Ticker ....." should be moved to Process_Globals.

lPos will only increment by one on every click. What do you need a timer for? You turn it off after every click????

The modified code. I think my previous post has the answer but not the solution.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type Tagg(C As Byte, R As Byte, colour As Int)
   Dim Ticker As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim lPos As Tagg
        Dim col,row As Byte
    Dim snd As Label
    Dim dirC, dirR As Int
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Ticker.Initialize("Ticker",50)
   Ticker.Enabled=False
    live(10,20,"Bx")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub live(c,r As Byte, sEvent As String)
    Dim bx As Label
    Dim lTag As Tagg
    bx.Initialize(sEvent)
    lTag.c=c : lTag.r=r
    'lTag.colour=aColours(Rnd(1,6))
    lTag.colour=Colors.Red
    bx.Tag=lTag
    bx.Color=lTag.colour
    Activity.AddView( bx,1,1,10%x,10%y)
'    Log(lTag.c)
'    Log(lTag.r)
'    Log(lTag.colour)
End Sub

Sub Bx_Click
    snd=Sender : lPos=snd.Tag
    col=lPos.C : row=lPos.R
'    Log(col)
'    Log(row)
    Log("in Bx_Click: snd.Tag " & snd.Tag) 
    dirC=0 : dirR=1
   Ticker.Enabled=True
End Sub

Sub Ticker_Tick
       lPos.C = lPos.C + 1 'lPos.C + dirC*-1  :  lPos.R = lPos.R + dirR*-1
    Ticker.Enabled=False
   Log("in Ticker_Tick: lPos.C " & lPos.C)
   Log("in Ticker_Tick: lPos.R " & lPos.R)
End Sub

Good luck
Mark
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks again Mark. The code I posted back worked for me.
There is much more code in my timer sub and in the click code which calls another routine also using the Tag data. The timer in the example was to illustrate I was using a timer. The turning off of the timer in the Tick sub is actually 10 lines down the code.

Your finding that link was useful and explains that the same pointer is used for both 'arrays'. I now understand that.

As I see it, in the code I posted back to use the lPos.C and lPos.R I simply need to copy them to two new Byte variables to break the link so to speak, and use them instead. It 'seems' to work.
I did not think of doing that without knowing the cause because, why would one copy one variable to another, why not use the original.
So... thank you very much for your time and effort and the informative link.
Using Z below works fine and seems simple... NOW.
[EDIT] I agree about where dim'd.
[EDIT] How stupid I missed out the last line Z=Z+1, but that works

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type Tagg(C As Byte, R As Byte, colour As Int)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim lPos As Tagg
      Dim col,row As Byte
    Dim snd As Label
    Dim dirC, dirR As Int
    Dim Ticker As Timer
      Dim Z As Byte
End Sub

Sub Activity_Create(FirstTime As Boolean)
Ticker.Initialize("Ticker",50)
    live(10,20,"Bx")
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub live(c,r As Byte, sEvent As String)
    Dim bx As Label
    Dim lTag As Tagg
    bx.Initialize(sEvent)
    lTag.c=c : lTag.r=r
    'lTag.colour=aColours(Rnd(1,6))
    lTag.colour=Colors.Red
    bx.Tag=lTag
    bx.Color=lTag.colour
    Activity.AddView( bx,1,1,10%x,10%y)
    Log(lTag.c)
    Log(lTag.r)
    Log(lTag.colour)
End Sub

Sub Bx_Click
Dim a As Tagg
    snd=Sender : lPos=snd.Tag
      a.C=lPos.C : a.R=lPos.R
    col=lPos.C : row=lPos.R
      Log(a.C)
      Log(col)
    Log(row)
    Log(snd.Tag) 
    dirC=0 : dirR=1
Ticker.Enabled=True
End Sub

Sub Ticker_Tick

   '  lPos.C = lPos.C + 1 'lPos.C + dirC*-1  :  lPos.R = lPos.R + dirR*-1
       Ticker.Enabled=False
   Z=lPos.C
        Z=Z+1
   Log(lPos.C & " - " & Z)
End Sub
 
Last edited:
Upvote 0
Top