B4J Question Problem with List of Label references

warwound

Expert
Licensed User
Longtime User
In my project (cannot upload the project to the forum due to company policy) i have:

  • A class named T99Button.
  • A class named T99ButtonGroup.

An instance of T99ButtonGroup contains a List of T99Button instances.

Each T99Button has a property named Label that contains a reference to a unique Label.
Each Label Tag property contains a reference to it's T99Button class instance.

So with a T99Button instance, i can get a reference to the Label it relates to.
And with a Label instance, i can get a reference to the T99Button it relates to.

My T99ButtonGroup is simple:

B4X:
Sub Class_Globals
   Private fx As JFX
   Private mT99Buttons As List
End Sub

public Sub GetButtons As List
   Return mT99Buttons
End Sub

Public Sub Initialize(Labels As List)
   mT99Buttons.Initialize
   For Each Label1 As Label In Labels
     Dim T99Button1 As T99Button
     T99Button1.Initialize(Label1)
     T99Button1.ApplyStyles
     mT99Buttons.Add(T99Button1)
     
     Label1.Tag=T99Button1
     
     
     Log("ONE T99Button1.ButtonNumber: "&T99Button1.ButtonNumber&", T99Button1.Label.Text: "&T99Button1.Label.Text)   '   OK Labels are unique (1 to 96)
   Next
   
   For Each T99Button1 As T99Button In mT99Buttons
     Log("TWO T99Button1.ButtonNumber: "&T99Button1.ButtonNumber&", T99Button1.Label.Text: "&T99Button1.Label.Text)   '   all labels are label 96
   Next
End Sub

A Label Text property is a number from 1 to 96.
A T99Button ButtonNumber property is the Label's Text padded to 2 digits.
Both these values should be the same...

The log shows:
ONE T99Button1.ButtonNumber: 01, T99Button1.Label.Text: 1
ONE T99Button1.ButtonNumber: 02, T99Button1.Label.Text: 2
ONE T99Button1.ButtonNumber: 03, T99Button1.Label.Text: 3
ONE T99Button1.ButtonNumber: 04, T99Button1.Label.Text: 4
ONE T99Button1.ButtonNumber: 05, T99Button1.Label.Text: 5
ONE T99Button1.ButtonNumber: 06, T99Button1.Label.Text: 6
ONE T99Button1.ButtonNumber: 07, T99Button1.Label.Text: 7
ONE T99Button1.ButtonNumber: 08, T99Button1.Label.Text: 8
ONE T99Button1.ButtonNumber: 09, T99Button1.Label.Text: 9
ONE T99Button1.ButtonNumber: 10, T99Button1.Label.Text: 10
ONE T99Button1.ButtonNumber: 11, T99Button1.Label.Text: 11
ONE T99Button1.ButtonNumber: 12, T99Button1.Label.Text: 12
ONE T99Button1.ButtonNumber: 13, T99Button1.Label.Text: 13
ONE T99Button1.ButtonNumber: 14, T99Button1.Label.Text: 14
ONE T99Button1.ButtonNumber: 15, T99Button1.Label.Text: 15
ONE T99Button1.ButtonNumber: 16, T99Button1.Label.Text: 16
ONE T99Button1.ButtonNumber: 17, T99Button1.Label.Text: 17
ONE T99Button1.ButtonNumber: 18, T99Button1.Label.Text: 18
ONE T99Button1.ButtonNumber: 19, T99Button1.Label.Text: 19
ONE T99Button1.ButtonNumber: 20, T99Button1.Label.Text: 20
ONE T99Button1.ButtonNumber: 21, T99Button1.Label.Text: 21
ONE T99Button1.ButtonNumber: 22, T99Button1.Label.Text: 22
ONE T99Button1.ButtonNumber: 23, T99Button1.Label.Text: 23
ONE T99Button1.ButtonNumber: 24, T99Button1.Label.Text: 24
ONE T99Button1.ButtonNumber: 25, T99Button1.Label.Text: 25
ONE T99Button1.ButtonNumber: 26, T99Button1.Label.Text: 26
ONE T99Button1.ButtonNumber: 27, T99Button1.Label.Text: 27
ONE T99Button1.ButtonNumber: 28, T99Button1.Label.Text: 28
ONE T99Button1.ButtonNumber: 29, T99Button1.Label.Text: 29
ONE T99Button1.ButtonNumber: 30, T99Button1.Label.Text: 30
ONE T99Button1.ButtonNumber: 31, T99Button1.Label.Text: 31
ONE T99Button1.ButtonNumber: 32, T99Button1.Label.Text: 32
ONE T99Button1.ButtonNumber: 33, T99Button1.Label.Text: 33
ONE T99Button1.ButtonNumber: 34, T99Button1.Label.Text: 34
ONE T99Button1.ButtonNumber: 35, T99Button1.Label.Text: 35
ONE T99Button1.ButtonNumber: 36, T99Button1.Label.Text: 36
ONE T99Button1.ButtonNumber: 37, T99Button1.Label.Text: 37
ONE T99Button1.ButtonNumber: 38, T99Button1.Label.Text: 38
ONE T99Button1.ButtonNumber: 39, T99Button1.Label.Text: 39
ONE T99Button1.ButtonNumber: 40, T99Button1.Label.Text: 40
ONE T99Button1.ButtonNumber: 41, T99Button1.Label.Text: 41
ONE T99Button1.ButtonNumber: 42, T99Button1.Label.Text: 42
ONE T99Button1.ButtonNumber: 43, T99Button1.Label.Text: 43
ONE T99Button1.ButtonNumber: 44, T99Button1.Label.Text: 44
ONE T99Button1.ButtonNumber: 45, T99Button1.Label.Text: 45
ONE T99Button1.ButtonNumber: 46, T99Button1.Label.Text: 46
ONE T99Button1.ButtonNumber: 47, T99Button1.Label.Text: 47
ONE T99Button1.ButtonNumber: 48, T99Button1.Label.Text: 48
ONE T99Button1.ButtonNumber: 49, T99Button1.Label.Text: 49
ONE T99Button1.ButtonNumber: 50, T99Button1.Label.Text: 50
ONE T99Button1.ButtonNumber: 51, T99Button1.Label.Text: 51
ONE T99Button1.ButtonNumber: 52, T99Button1.Label.Text: 52
ONE T99Button1.ButtonNumber: 53, T99Button1.Label.Text: 53
ONE T99Button1.ButtonNumber: 54, T99Button1.Label.Text: 54
ONE T99Button1.ButtonNumber: 55, T99Button1.Label.Text: 55
ONE T99Button1.ButtonNumber: 56, T99Button1.Label.Text: 56
ONE T99Button1.ButtonNumber: 57, T99Button1.Label.Text: 57
ONE T99Button1.ButtonNumber: 58, T99Button1.Label.Text: 58
ONE T99Button1.ButtonNumber: 59, T99Button1.Label.Text: 59
ONE T99Button1.ButtonNumber: 60, T99Button1.Label.Text: 60
ONE T99Button1.ButtonNumber: 61, T99Button1.Label.Text: 61
ONE T99Button1.ButtonNumber: 62, T99Button1.Label.Text: 62
ONE T99Button1.ButtonNumber: 63, T99Button1.Label.Text: 63
ONE T99Button1.ButtonNumber: 64, T99Button1.Label.Text: 64
ONE T99Button1.ButtonNumber: 65, T99Button1.Label.Text: 65
ONE T99Button1.ButtonNumber: 66, T99Button1.Label.Text: 66
ONE T99Button1.ButtonNumber: 67, T99Button1.Label.Text: 67
ONE T99Button1.ButtonNumber: 68, T99Button1.Label.Text: 68
ONE T99Button1.ButtonNumber: 69, T99Button1.Label.Text: 69
ONE T99Button1.ButtonNumber: 70, T99Button1.Label.Text: 70
ONE T99Button1.ButtonNumber: 71, T99Button1.Label.Text: 71
ONE T99Button1.ButtonNumber: 72, T99Button1.Label.Text: 72
ONE T99Button1.ButtonNumber: 73, T99Button1.Label.Text: 73
ONE T99Button1.ButtonNumber: 74, T99Button1.Label.Text: 74
ONE T99Button1.ButtonNumber: 75, T99Button1.Label.Text: 75
ONE T99Button1.ButtonNumber: 76, T99Button1.Label.Text: 76
ONE T99Button1.ButtonNumber: 77, T99Button1.Label.Text: 77
ONE T99Button1.ButtonNumber: 78, T99Button1.Label.Text: 78
ONE T99Button1.ButtonNumber: 79, T99Button1.Label.Text: 79
ONE T99Button1.ButtonNumber: 80, T99Button1.Label.Text: 80
ONE T99Button1.ButtonNumber: 81, T99Button1.Label.Text: 81
ONE T99Button1.ButtonNumber: 82, T99Button1.Label.Text: 82
ONE T99Button1.ButtonNumber: 83, T99Button1.Label.Text: 83
ONE T99Button1.ButtonNumber: 84, T99Button1.Label.Text: 84
ONE T99Button1.ButtonNumber: 85, T99Button1.Label.Text: 85
ONE T99Button1.ButtonNumber: 86, T99Button1.Label.Text: 86
ONE T99Button1.ButtonNumber: 87, T99Button1.Label.Text: 87
ONE T99Button1.ButtonNumber: 88, T99Button1.Label.Text: 88
ONE T99Button1.ButtonNumber: 89, T99Button1.Label.Text: 89
ONE T99Button1.ButtonNumber: 90, T99Button1.Label.Text: 90
ONE T99Button1.ButtonNumber: 91, T99Button1.Label.Text: 91
ONE T99Button1.ButtonNumber: 92, T99Button1.Label.Text: 92
ONE T99Button1.ButtonNumber: 93, T99Button1.Label.Text: 93
ONE T99Button1.ButtonNumber: 94, T99Button1.Label.Text: 94
ONE T99Button1.ButtonNumber: 95, T99Button1.Label.Text: 95
ONE T99Button1.ButtonNumber: 96, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 01, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 02, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 03, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 04, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 05, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 06, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 07, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 08, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 09, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 10, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 11, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 12, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 13, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 14, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 15, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 16, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 17, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 18, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 19, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 20, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 21, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 22, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 23, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 24, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 25, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 26, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 27, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 28, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 29, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 30, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 31, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 32, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 33, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 34, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 35, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 36, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 37, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 38, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 39, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 40, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 41, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 42, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 43, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 44, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 45, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 46, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 47, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 48, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 49, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 50, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 51, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 52, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 53, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 54, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 55, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 56, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 57, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 58, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 59, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 60, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 61, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 62, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 63, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 64, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 65, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 66, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 67, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 68, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 69, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 70, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 71, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 72, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 73, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 74, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 75, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 76, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 77, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 78, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 79, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 80, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 81, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 82, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 83, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 84, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 85, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 86, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 87, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 88, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 89, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 90, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 91, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 92, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 93, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 94, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 95, T99Button1.Label.Text: 96
TWO T99Button1.ButtonNumber: 96, T99Button1.Label.Text: 96

When my T99Button is created it's Label references the correct Label.
After that - with no other code being executed - all T99Button Labels reference the same Label, Label #96.

I'm trying to apply some CSS style to each Label of each T99Button but only Label #96 gets the style applied - it gets the same style applied 96 times!

Can anyone see what the problem is?
 

warwound

Expert
Licensed User
Longtime User
Fixed by using a traditional for loop instead of the for each:

B4X:
   For i=0 To Labels.Size-1
     Dim Label1 As Label=Labels.Get(i)
     
     Dim T99Button1 As T99Button
     T99Button1.Initialize(Label1)
     T99Button1.ApplyStyles
     mT99Buttons.Add(T99Button1)
     
     Label1.Tag=T99Button1
     
     
     Log("ONE T99Button1.ButtonNumber: "&T99Button1.ButtonNumber&", T99Button1.Label.Text: "&T99Button1.Label.Text)   '   OK Labels are unique (1 to 96)
   Next
   
   For Each T99Button1 As T99Button In mT99Buttons
     Log("TWO T99Button1.ButtonNumber: "&T99Button1.ButtonNumber&", T99Button1.Label.Text: "&T99Button1.Label.Text)   '   all labels are label 96
   Next

And my log shows that the T99Button Label remains the same.

So the problem was with the for each loop, it wasn't creating a new instance of my Label on each iteration.
So the last Label assigned to the last T99Button's Label property actually becomes assigned to all previous T99Buttons.

Is this a bug with the for each syntax?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is related to Label being a wrapper object.
The For Each iterator reuses the same wrapper instance.
The result is that all T99Buttons reference the same wrapper.

You can solve it by changing the iterator type to Object:
B4X:
For Each Label1 As Object Labels
A new wrapper will be created for each call to T99Button.Initialize.
 
Upvote 0
Top