Bug? Bug with Types

Scantech

Well-Known Member
Licensed User
Longtime User
See attach file. Click the button twice and observe the list size(lData).

first time = 1
second time = 0

Reinitializing the list(Starter.DTCView.lDataDTC.Initialize) in type causing this behavior. Its interfering with the other list(lData).
 

Attachments

  • Bug with Types.zip
    9 KB · Views: 228
Last edited:

Scantech

Well-Known Member
Licensed User
Longtime User
I also notice passing lData to other list will cause lData to be empty on second attempt? That occurs if i initialize the list that gets the lData.
 

Scantech

Well-Known Member
Licensed User
Longtime User
Its actually an issue with List not Types.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Dim lData As List
    Dim lstGet As List
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
 
    lData.Initialize
    lData.Add("HI")
 
End Sub

B4X:
Sub Button1_Click

    lstGet.Initialize
    lstGet = lData
    Log(lData.Size)

End Sub

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
1
0
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
 

Pendrush

Well-Known Member
Licensed User
Longtime User
When you click button fot the first time, you create reference between two lists
B4X:
lstGet = lData
Then you click button for the second time and in this line
B4X:
lstGet.Initialize
you Initialize already referenced list. This make both list just initialized without any data, size is 0.
This is normal behavior across B4X.

You can CopyObject with B4XSerializator, see this link
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not a bug. It happens because of two reasons:
1. Both variables point to the same object.
2. The assumption that calling Initialize creates a new object is incorrect though it looks like this in many cases.

The correct way to create a new object is by calling Dim. In this case, as the list is part of a type, you should do it like this:
B4X:
Dim l As List
l.Initialize
Starter.DTCView.lDataDTC = l
 

Scantech

Well-Known Member
Licensed User
Longtime User
One more explanation, please.

This one

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Dim lstGet As List
    Dim lstSet As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    lstSet.Initialize

End Sub
B4X:
Sub Button1_Click
    lstSet.Clear
     lstSet.Add("HI")
    Data(lstSet)
End Sub

Sub Data(l As List)  
    lstGet.Initialize
    lstGet = l
    Log(l.Size)
End Sub

Same result 1 and then 0. l as list is local variable and gets created with new object?
 
Top