Apologies...
this was wrong topic (loop through values). My issue was looping through the key value pairs, not just the values.
Will leave this here if there are no objections as a possible option to get values from SortedList collection.
I just tried to figure out this error as well and my solution was to use the DictionaryEntry
type to replace the erroring out KeyValuePair
type.
Found from MS reference
https://msdn.microsoft.com/en-us/library/system.collections.dictionaryentry(v=vs.100).aspx
In my case, I had code creating a SortedList
type collection and neither the var
or KeyValuePair
types worked (var
errored out when trying to read the key/value from the collection item and the KeyValuePair
errored out in the initial loop definition. Both errored out with "Specified cast is not valid"
)
So here is sample of code that worked for me:
SortedList _dims = GetList("mysortedlist");
foreach (DictionaryEntry kvp in _dims)
{
Console.WriteLine(kvp.Key.ToString());
Console.WriteLine(kvp.Value.ToString());
}
HTH
Dave