Assigning a List A to List B

Assigning a List A to List B

  

If i assign "list B = list A", should all further changes on list A also be applied to list B, since these values are passed by reference?

Cristiano Chamorra wrote:

José Dantas wrote:

If i assign "list B = list A", should all further changes on list A also be applied to list B, since these values are passed by reference?

Hello José,

No, if you assign "list B = list a", and then add something to list A, it will only affect list A.


hope it helps,

regards,

Cristiano Chamorra



thank you cristiano for replying,

I've noticed that with some tests I've done, what does this mean then?

"On the server side, variables of List data type have a reference to the values and don't contain a copy of the elements. For example, if you have two lists List A and List B and you assign List A to the List B, you will have two variables pointing to the same list instead of two independent copies. When changing values in one variable, the other is also changed."


Solution

Hi José,

It depends if the list is being passed by parameter in the same module (or assigned in the same module) or in different modules.

I will include some screenshots with an example.

In a module called Test_A, I've created an action called "Test_ListByValue", with the following code:

As you can see, we assign a value "ByValue" in a record with the same structure as the list and we append it to the List.

After that, we reference this action in the Test_B module and include it in a local action of the module called "Test_List":


In this action, you can see the difference behaviors when the action is inside the module and outside the module:


List copied by value



As you can see, after calling the "Test_ListByValue" my list is still empty. This means that another list was created and has it's own data.


List copied by Reference

In the next action, we have the same code as the "Test_ListByValue" present in the Test_A module (only the assigned text is different) but now this action is in the same module as the action that calls it (Test_B module)

In the Debug, continuing the execution of this action, we can see that the List is not empty anymore:



Hope I could help you.


Kind regards,

Miguel Sousa


Solution

Hi,

We have many list function except List Duplicate all passed by ref.


Thanks!

Hi José,

I just deleted Chritiano's answer, as it is plain wrong. When assigning a list to another list, they reference the same list items, so adding something to one list is reflected via the other. When passing as parameter, see Miguel's excellent answer.

Also, just came to mind, be careful with actually assuming an assign refers to the same list: this is _only_ the case when the lists have identical types. If one is a List of Records instead of a List of Structures, a silent conversion happens, and they're two different lists. Also, if both are List of different Structures, but with the same attributes (or compatible attributes), a silent conversion happens.