Invoke Code - Change of content for in_arguments

If im adding something into an empty list using invoke code, but the list is a ‘IN’ argument, why it changes the size and content of the list even outside of the invoke code activity? That does not happen lets say if i change a string using invoke code (in argument) and then logging the result outside of the invoke code activity… Its kinda confusing, plz someone help thanks!

Hi,

Because List type variable is reference type and it passes not value but reference (Address)
So, if we modify content of reference type variable in InvokeCode, the varaible will be changed outside of InvokeCode because its content exists in same address.

image

Note String is special reference type and it behaves like value type.

Regards,

1 Like

Hi @yuri.santos

Can I know what you want to do exactly. I will help you with that
This example will help you out too
=>Give the below syntax in Assign activity

originalList= New List(of String) From {"A","B","C"}

originalList is of DataType System.Collections.Generic.List(System.String).
=> Use the below code in Invoke Code activity:

' Input Argument: originalList (Type: List(Of String))

' Create a new list and copy elements from the original list
modifiedList = New List(Of String)(originalList)

' Make modifications to the modifiedList
modifiedList.Add("D")
modifiedList.Add("E")
modifiedList.Add("F")

' Output Argument: modifiedList (Type: List(Of String))

Below are the Invoked arguments:


modifiedList is of DataType System.Collections.Generic.List(System.String). and it’s an Out argument. originalList is of In argument.
=> You can print it in Message box => below syntax prints the original list

"Original List: " +String.Join(", ", originalList) 

=> You can print it in Message box => below syntax prints the Modified list

"Modified List: " + String.Join(", ", modifiedList)


Output:
Original List

Modified List

Check the below workflow
Sequence23.xaml (7.8 KB)

Regardss

1 Like

So why does a String (Reference type) does not change its state? Lets say for example i changed a strVar using replace inside an invoke code with in type argument…

Hi,

Lets say for example i changed a strVar using replace inside an invoke code with in type argument…

Can you share specific sentence in InvokeCode?

Regards,

1 Like

Hi @yuri.santos

Check the below workflow:


Below are the Invoked Arguments:

Code inside Invoke Code activity:

strText= strText.Replace("Some","Helloo")

Output:

Regards,

He answered your question at the end of his reply:

Note String is special reference type and it behaves like value type.

1 Like

Hi,

It’s normal behavior of ReferenceType variable.

First, reference of strTest in InvokeCode is same as reference of strTest of Main.xaml.
So both refers same address (original “something”)

image

Next, in InvokeCode, strTest.Replace("Something",Hello") creates new instance at another address. So strTest in InvokeCode refers different address from strTest of Main.xaml

image

After exiting InvokeCode, strTest still refers (original) “Something”

Please note that strTest in Main.xaml and strTest in InvokeCode are different variable.

On the other hand, List.Add or Array.Sort etc. operate something to original instance. As a result, it affects original instance.

image

Array.Sort sorts original instance.
image
After exiting InvokeCode, original array is affected by operation in the InvokeCode, as a result.

One more example, Array.Sort and LINQ OrderBy both are method of sorting. However, according to the above, it behaves differently, as the following.

Array.Sort
image

LINQ GroupBy
image

Hope this helps you.

Regards,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.