开发者

How to get the key from a TDictionary?

开发者 https://www.devze.com 2023-01-17 12:48 出处:网络
I have a TDictionary<TKeyClass, TValueClass>. I want to accomplish something like for i := 0 to MyDictionary.Count -1 do

I have a TDictionary<TKeyClass, TValueClass>.

I want to accomplish something like

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

I cannot Access the Keys anymore I can just use them if I exactly know them.

Is the only alternative c开发者_开发问答reate a TDictionary<TValueClass, TKeyValue> ? So I can loop thorugh the Keys?

The workaroud I found is to create a TList<TKeyClass> but this is something i don't like.


Use an enumerator.

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

Since TDictionary is a hash table, you can't access it with integer indexing like an array or TList. But an enumerator works just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消