How To Prnt Key values pairs from an dictionary

Hi all, I was trying to play with Dictionaries and wanted t be able to print the key as well as the value in the same for each loop, but no matter what i do, it givs an error stating:

For Each: Unable to cast object of type ‘System.Collections.Generic.KeyValuePair2[System.String,System.String]' to type 'System.Collections.Generic.Dictionary2[System.String,System.String]’.

I have tried to change the distionary to String, String and the for each to the same or String, object and the for each to the same, but I cant get it to work.

So how can I print the key and value in the same for each loop? I have attached the sample file

Thank you in advance.
Dictionary (5.5 KB)

Test to change the For Loop TypeArgument to KeyValuePair. Then in Write Line:

item.Key + " = " + item.Value

Hi @Sidney_Vogel,

Adding to what @ptrobot said. Take a look at the solution in this thread. It contains some explanation and a sample code.

1 Like

Hi @Sidney_Vogel ,

You could also try looping through the Keys and reference the Values using the dictionary itself

For Each->

 Key In in_exceldata.Keys

So could you give this a try instead?

key+ " " + in_exceldata(key).ToString

Also, I’ve changed the ArgumentType to String (5.6 KB)

Kind Regards,
Ashwin A.K

1 Like

Hi @Sidney_Vogel,

Have a look on the below video


1 Like

Thank you this was helpful

Hi @ashwin.ashok Thank you for your example, this worked for me

Hi @jeevith Thank you for your explanation

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