B4J Question Changing list

BeneBarros

Active Member
Licensed User
Hello,

This happening a strange problem with the handling of lists.
I created two different lists.
Initially add the same items in both lists.
When I make changes in either list the result is that the two lists are changed.
The following example Annex.

What am I doing wrong ???
 

Attachments

stevel05

Expert
Licensed User
It's exactly because you are adding the SAME item to both lists, not a copy or duplicate of the item.

Try this:
B4X:
'add duplicate items in both lists
'changes one item from a given list
Private Sub Add_It
    For i= 0 To 4
        Dim tmp As tpItem
        tmp.initialize
        tmp.sId = i
        tmp.pId = i*2
        tmp.pDesc= "Item :" & (i+1)
        prgOrig.Add(tmp)
        Dim tmp As tpItem
        tmp.initialize
        tmp.sId = i
        tmp.pId = i*2
        tmp.pDesc= "Item :" & (i+1)
        prgWork.Add(tmp)
    Next   
End Sub
Create a new item (With a dim) and duplicate the item, you will then have a separate (duplicate) item in both lists, which you can change independently.
 

Magma

Well-Known Member
Licensed User
Seems like tmp being item of list... but i don;t know if this is right...

so only solution i can think is:

B4X:
Private Sub Add_It
    For i= 0 To 4
        Dim tmp As tpItem
        Dim tmp2 As tpItem
        tmp.initialize
        tmp.sId = 10+i
        tmp.pId = i*2
        tmp.pDesc= "Item :" & (i+1)
        tmp2.initialize
        tmp2.sId = 10+i
        tmp2.pId = i*2
        tmp2.pDesc= "Item :" & (i+1)
        prgOrig.Add(tmp)
        prgWork.Add(tmp2)
    Next   
End Sub
 

BeneBarros

Active Member
Licensed User
It's exactly because you are adding the SAME item to both lists, not a copy or duplicate of the item.

Try this:
B4X:
'add duplicate items in both lists
'changes one item from a given list
Private Sub Add_It
    For i= 0 To 4
        Dim tmp As tpItem
        tmp.initialize
        tmp.sId = i
        tmp.pId = i*2
        tmp.pDesc= "Item :" & (i+1)
        prgOrig.Add(tmp)
        Dim tmp As tpItem
        tmp.initialize
        tmp.sId = i
        tmp.pId = i*2
        tmp.pDesc= "Item :" & (i+1)
        prgWork.Add(tmp)
    Next  
End Sub
Create a new item (With a dim) and duplicate the item, you will then have a separate (duplicate) item in both lists, which you can change independently.
thank you ...
Now works perfectly.
 

BeneBarros

Active Member
Licensed User
Seems like tmp being item of list... but i don;t know if this is right...

so only solution i can think is:

B4X:
Private Sub Add_It
    For i= 0 To 4
        Dim tmp As tpItem
        Dim tmp2 As tpItem
        tmp.initialize
        tmp.sId = 10+i
        tmp.pId = i*2
        tmp.pDesc= "Item :" & (i+1)
        tmp2.initialize
        tmp2.sId = 10+i
        tmp2.pId = i*2
        tmp2.pDesc= "Item :" & (i+1)
        prgOrig.Add(tmp)
        prgWork.Add(tmp2)
    Next  
End Sub
thank you
the two ways suggested work correctly.
the variables were placed only for testing,,
 
Top