How to use dictionary?

Hi, I tried to use the dictionary recently.

I met some problem that I tried to retrieve the particular value with its key.
For example: I have a Dictionary(Of String, Double) {“AA”, 0.1}, {“BB”, 0.2},
but I failed when I used Value = Dictionary(“BB”)
I searched some case but I felt confused.
A case said that the variable type of Dictionary is KeyValuePair<Of String, Double> if I
want to use dictionary.

Did I use the wrong Data Type, not the Dictionary<Of String, Double>, but the KeyValuePair<Of String, Double>?

Is Dictionary and KeyValuePair are the same thing?

Thanks in advance!


this looks correct
if possible can i have a view on the error that we were getting on using this, with a screenshot
Cheers @opas1216

you get value in the right way.
send error that you get


1 Like

You can quickly check by doing like:

Maybe some initialization issue from the dict are to check at your xaml


a Dictionary consists of KeyValuePairs, so they are not the same.

You want to extract the value of “BB”? Do like PPR suggested

I couldn’t find the error message appeared yesterday, but it seems have some new problems now.

After I changed the variable type of my dictionary variable from dictionary to KeyValuePair yesterday, right now even I changed it back , but the error message keeps showing me that "For Each: Unable to cast object of type 'System.Collections.Generic.KeyValuePair2[System.String,System.Double]' to type 'System.Collections.Generic.Dictionary2[System.String,System.Double] "

I tried For each and while tried to solve the problem but they didn’t work.

Please provide me some advice or point out where I was wrong
Thank you everyone!

Thanks @ppr @MarkusDS

I already test it before and It works!

When I use the for each to loop the dictionary then it can’t work.

I tested it today that when I set the type argument = Object is OK
But if the type argument = Dictionary it showed error.

Could you explain the reason why it can only use Object not the dictionary type?
I thought I should set it to dictionary type.

Hey @opas1216

Dictionary is collection of KeyValuePairs.

you have to select a for each Activity Argument type to System.Collections.Generic.KeyValuePair<System.String, System.Double)

Reference Example - Dictionary_Sample with KeyValuePair.xaml (6.3 KB)



Like if you a DT you will do a for each on rows…

When you have a dictionary you cant do a for each on dictionaries. You have to do it on keyvaluepairs

@MarkusDS, we can do a for each on dictionaries as well.


Sure, but then you need an array of dictionaries


Best way to iterate dictionary is to use KeyValuePair.
The dictionary stores key-value pairs. So you can use a KeyValuePair<TKey, TValue> type or an implicitly typed variable var in foreach loop

Dictionary elements can be accessed by many ways e.g. foreach, for loop or indexer.

foreach (KeyValuePair<int, string> item in dict)
    Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);


for (int i = 0; i < dict.Count; i++)
    Console.WriteLine("Key: {0}, Value: {1}", 
                                            dict[ dict.Keys.ElementAt(i)]);




1 Like

I know, thanks

@opas sorry for my delayed answer due i am travelling.
The community gave you the hints and I hope you get work. feel free to come back in case other issues occured

Here is a detailed article on that :slight_smile:



Thanks @aksh1yadav.
That worked for me. i was setting the argument type as Dictionary.
Thanks again :slight_smile:


Can anyone help me with array of Dictionaries. How can I add a new dictionary to the array of dictionary ?

Eg: I have initialized a array of dictionary as ArrayOfDict

Now what I want is to add a dictionary to this array dynamically as I get the data from another process.
I was trying to use the Add to Dictionary activity, however could not find what should I pass as dictionary name. I tried it as ArrayOfDict(1) , ArrayOfDict[1].
Nothing is helping.
Request to provide any assistance on this.
Thanks in advance!