views:

108

answers:

1

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 create 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.

+11  A: 

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.

Mason Wheeler