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:

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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…