Iterate through indexed property in Delphi 2010

786 views Asked by At

In Delphi 2010 is there a way to iterate through any kind of indexed property (like Pages of TPageControl, for example)?

2

There are 2 answers

2
Jeroen Wiert Pluimers On BEST ANSWER

No, this is not possible, as there is not standard pattern that:

  • specifies the index type
  • specifies the start and end values of the index to use

That's why certain classes have enumerators: it is the pattern that tells the for ... in statement how to enumerate it.

What you can do however is a best estimate:

  • most times the index is integer
  • most times the index starts at zero
  • most times there is a property Count or Length that tells you how to obtain the end value

Finally, it is possible to "retrofit" TPageControl with a GetEnumertor using class helpers (or record helpers if the underlying type is a record).

0
Michael On

If I understand you correctly, you want to iterate through these properties via RTTI without knowing anything about the property and its index-values. Because valid index-values must not be a sequence of integer-values this can't be possible. There might be properties with string-indexes or with object-references as index-value. And there is no mechanism in RTTI to query valid index-values wich could be used for an iteration.