Where is my mistake?

sarkis

Member
Licensed User
Longtime User
Hi all
I create an array of panels and keep the indexes on tag as new Type.
Then I try to restore the indexes in touch event , but all properties are ok but tag.
Tag returns indexes of last panel.( (4,4 ) the last indexes of cycle)
What is wrong in my code?

...
Sub Globals
Type indexn (ind1 As Int, ind2 As Int)
Dim Grid(5,5) As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim ind As indexn
...
For i=0 To 4
For j=0 To 4
Grid(i,j).Initialize("Panels")
ind.ind1=i
ind.ind2=j
Grid(i,j).Tag=ind 'tag is ok
....
Next
Next
End Sub

Sub Panels_Touch (Action As Int, X As Float, Y As Float) As Boolean
Dim ind As indexn
Dim p As Panel
p = Sender 'sender properties are ok but tag
ind=p.Tag 'tag is wrong
If p.RequestFocus=True Then
Msgbox("xxx", "focus")
Else
Msgbox("yyy", "no_focus")
End If
End Sub

Thank for replying
 

thedesolatesoul

Expert
Licensed User
Longtime User
Move the declaration of ind inside the loops.
In this way you are not using the same ind reference in all panel tags.


B4X:
Sub Activity_Create(FirstTime As Boolean)

...
For i=0 To 4
For j=0 To 4
Dim ind As indexn
ind.Initialize
Grid(i,j).Initialize("Panels")
ind.ind1=i
ind.ind2=j
Grid(i,j).Tag=ind 'tag is ok
....
Next
Next
End Sub
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Okay. Custom types are always assigned by reference.

So in your code, whenever you assign ind to a tag, you need to assign a new object. If you re-use the old object, whenever you change a value all the tags will change because they all refer to the same object. Dimming ind again will create a new object.



Sent from my GT-I9000 using Tapatalk
 
Upvote 0

sarkis

Member
Licensed User
Longtime User
some more question

but in loop tag valus were ok.
I checked them on debugger.
They were incorrect in sub touch only?

and one more question
why RequestFocus is return false?
 
Upvote 0

bluejay

Active Member
Licensed User
Longtime User
To clarify the reply from thedesolatesoul...

The value stored in TAG is a pointer to the memory location that contains the values for IND.

If you don't use DIM inside the loop than every TAG points to the same memory location. The contents of this memory location is whatever was last stored there, in this case 4,4. This is the value returned by TAG later.

Bluejay
 
Upvote 0
Top