Bug in List with User defined type

touchsquid

Active Member
Licensed User
Longtime User
If I add elements to a list which are user defined types, all the elements in the list are the same, although the added code was different.

The user defined Type is:
Type DeviceProfile (Brand As String, _
TypeName As String, _
BrandID As Int, _
Model As String, _
TypeID As Int, _
DeviceID As Int, _
TVinput As Int)

If I add a single string to the list, instead of a Typed variable, it works correctly.

Is this a known bug? If not I can probably create an example for testing.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Make sure that you are not reusing the same single object when you are adding the items.
Objects are never being copied implicitly.
The correct way to add many objects of a certain type is:
B4X:
For i = 0 To 100
 Dim mt As MyType
 mt.Initialize
 mt... = ...
 mt... = ...
 List1.Add(mt)
Next
A new object is created each iteration.
 

agraham

Expert
Licensed User
Longtime User
Seems fine to me.
B4X:
   Dim L As List
   Dim i As Int
   L.Initialize
   Dim msg
   For i = 0 To 10
      Dim D As DeviceProfile
      D.Initialize
      D.BrandID = i
      L.Add(D)
   Next   
   For i = 0 To 10
      Dim D As DeviceProfile
      D = L.Get(i)
      msg = msg & D.BrandID & " "
   Next
   Msgbox(msg, "List")
 

touchsquid

Active Member
Licensed User
Longtime User
My mistake was Dim X as Deviceprofile outside the loop. Your version corrects the problem. As always, great help and support!
 
Top