Value changed in a dictionary when not expected

Hi fellows,

I’m facing a problem with an unexpected value update in a dictionary, and I don’t understand why it happens. I hope you could enlight me on that.

Initial state:

I created a dictionary where keys are String, and values are List(of String).
I put two key/values pairs in this dictionary:

  1. “key 1” associated with the list of String [“A”, “B”]
  2. “key 2” associated with the list of String [“A”, “B”]
    So far so good.

What I did then

I stored the value associated with my “key 1” key (value which is a list of String) in a variable. Then I changed the first value of this variable to “C”. I did NOT put this modified list as a new value inside my dictionary.

What I expected

So from my point of view, my dictionary still had two pairs, which were:

  1. “key 1” associated with the list of String [“A”, “B”]
  2. “key 2” associated with the list of String [“A”, “B”]
    Indeed, I didn’t update or modify any value or key in my dictionary in my previous step.
    Plus, in my mind, I also had a stored variable aside, which contains [“C”, “B”].

What I really had

When I asked the program to display my dictionary, I had two pairs:

  1. “key 1” associated with the list of String [“C”, “B”]
  2. “key 2” associated with the list of String [“C”, “B”]

What I don’t understand:

Why is my dictionary updated with my new values, even if I didn’t modify it?
And especially, why is my second pair (associated to my “key 2” key) also modified?

Here is my workflow : Main.xaml (19.1 KB)

You can press “Run”, the logs will show you the result.
Please let me know if I missed something or did something wrong, because I really don’t understand the behaviour behind that.

Many thanks,
Rémi

Ok, solved it.

The issue had two origins:

  1. I initialized both my values in my dictionary from the same variable list [“A”, “B”].
  2. When I stored the value associated with my “key 1” key, it was not just a value, it was kind of a pointer I think to the list itself. So when I thought I was modifying only the variable which stored my value, I was in reality modyfing the original list itself! And as this list was also used to build my second entry in my dictionary, it explains the result I obtained…

So in order to prevent this:

  1. I still store my value associated with my “key 1” key in a variable (but it is a pointer, so I must not modify it directly)
  2. I create a new list of string.
  3. As the “=” in an Assign activity does not “break” the pointer, I use an “AddRange” method in an Invoke method activity in order to copy the content of my stored variable in my newly created list.
  4. I’m able to modify my newly created list as I want without modifying my whole dictionary! Hurray.

Or I could also initialize the value of my dictionary with two differents variables, which would do the trick as well.

2 Likes

can u share this thru modified code.

  1. As the “=” in an Assign activity does not “break” the pointer, I use an “AddRange” method in an Invoke method activity in order to copy the content of my stored variable in my newly created list. will help. Thanks in advance.

Hi @shweta5006,

Sure.
So let’s say I got a variable called originalList, which is a List of String, and the list I want to modify in the end.

First, I need to create a new List, like copiedList = new List(of String).

Then I use an Invoke Method in order to copy the OriginalList into the CopiedList without the wierd pointer link:

  • Target Object: copiedList
  • MethodName: AddRange
  • Parameters: In - List - OriginalList

Capture1

Sorry, hit the Reply button too fast.

Hope it helps!