Making List2 = List1

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
This might have been answered before, but I couldn't think how to search for it and it's not covered in the List tutorial:

If I populate List1 and then say "List2 = List1", then any changes made to List1 are also automatically made to List2.

Is this correct (and/or intended)?
 
Last edited:

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Thanks. This was causing me a problem and I spent several hours trying to figure out how the 2nd List was getting off. (Made harder to find because B4A debug doesn't work with my too-large app.)

Couldn't find it documented anywhere on here nor in the Beginners Guide, so I've added a note to the Wiki.

When I first figured out what must be going on, I also tested to see if changing the second List would change the original one (I had been changing the original previously) and it appeared not to be changing the original so I changed all my code then found out that it *does* change.

Your post explains why it changes, but I wonder why it was designed that way. I can't think of any use for setting up two variables which always have the same value.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I am not 100% sure why it is designed that way. I think it is because of the way Java manages variables and memory.
While assigning a primitive you will be able to 'clone' it, with objects like lists or arrays or types, you have to 'clone' them manually.
To be honest, once you get the hang of it, it turns out quite handy, and you can reference an object in many ways while it is in only one place in memory.
 
Upvote 0

persianpowerman1

Active Member
Licensed User
Longtime User
hey guys... wondering... can i use this code to copy listVeiw stuff to a list... my code gave me an error

like...
B4X:
Dim l2 As List
l2.Initialize
l2.AddAll(listview1)

or is there an easier way to do this... copying from Listview to list and viz??
 
Upvote 0
Top