How to declare a TDictionary enumerator?

I have a TDictionary that stores a bunch of objects indexed by name, and I would like to be able to examine all the objects. So I tried this:

var enumerator: TMyObject; begin for enumerator in myDictionary do 

But this did not compile. "Incompatible types:" TMyObject "and" TPair "

So, I tried it a little differently:

 var enumerator: TPair<string, TMyObject>; 

This also did not compile. This error message is even stranger: Incompatible types: "TPair" and "TPair"

So, apparently, I need some kind of funky grammar to list my vocabulary with a loop for ... c . Does anyone know how to declare it correctly?

EDIT: Fabio Gomez gave an example that works correctly, but my code still does not compile using its method. Maybe because I work in another unit? The dictionary and class that it uses for the Value side are defined in one block, and this code in another place. Does this make it a compiler error? Can anyone confirm this?

EDIT 2: Found a problem. http://qc.embarcadero.com/wc/qcmain.aspx?d=69461 if anyone is interested.

+4
source share
1 answer

This works as expected:

 var Enum: TPair<string, TForm>; MyDict: TDictionary<string, TForm>; begin MyDict := TDictionary<string, TForm>.Create; try MyDict.Add('Form1', Self); for Enum in MyDict do ShowMessage(Enum.Key); finally MyDict.Free; end; 

Paste this code into the FormCreate event of any form and see for yourself.

+7
source

All Articles