How to get the key from TDictionary?

I have a TDictionary<TKeyClass, TValueClass> .

I want to do something like

 for i := 0 to MyDictionary.Count -1 do ShowMessage(MyDictionary.Keys[i].AStringProperty) 

I can’t access the keys anymore, I can just use them if I know them for sure.

The only alternative is creating a TDictionary<TValueClass, TKeyValue> ? So can I loop the keys?

The desktop I found is to create a TList<TKeyClass> , but this is what I don't like.

+7
delphi
source share
1 answer

Use an enumerator.

 for Key in MyDictionary.Keys do ShowMessage(Key.AStringProperty); 

Since TDictionary is a hash table, you cannot access it with integer indexing, like an array or TList. But the enumerator works fine.

+14
source share

All Articles