Hi, I have a Dictionary (of Integer, Dictionary (of String, Datatable)).
a) Instead of using a for each loop through keyvaluepair (of Integer, Dictionary (of String, Datatable)), I need to use a do while loop where I increment the index by counter.
b) And then within the do while loop, I have a for each loop to write Dictionary (of String, Datatable).
My question is how do I write a)? This is my code right now but it is giving me an exception.
I do not understand why you don’t want to use a For Each loop as you do in your code?
I see that the error that you have is “Key is not present”, are you sure that the int32 key of each element of “i_DictionaryofDictionaries” are perfectly sequential (0,1,2,3,4,…)? Because the loop that you put in place in your flow chart imposes it. If not, it may cause your “Key not found exception”.
To handle that you must add a condition before your “for each” loop to verify that the key “DictionaryCounter” exists in “i_DictionaryofDictionaries”. (Mind also to adjust your condition in the flowchart.
Using foreach is the simplest solution. But if your requirement requires using while loop with counter, then create a variable with type as System.Collections.Generic.KeyValuePair<TKey, TValue> by specifying the datatype you are using.
in your while loop, assign this variable as MyDictionary.ElementAt(intCounter)
You can also directly access the Key and value as
Key → MyDictionary.ElementAt(intCounter).Key
Value → MyDictionary.ElementAt(intCounter).Value