5

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

Rafael Piccolo
  • 2,328
  • 1
  • 18
  • 29

2 Answers2

2

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.

Michael
  • 858
  • 1
  • 5
  • 11
1

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

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • I did see your post (good stuff) while I was searching on the internet, trying to find an answer for this :) In my case, I have half dozen indexed properties of different components. I iterate through them just to get a "Caption" property. So, right now, I have half a dozen almost identical loops, and I don't like that. Although the GetEnumerator thing is cool, that's too much code for such a small problem. – Rafael Piccolo Nov 01 '11 at 02:38
  • 1
    What about abstracing those loops into some generic code, then pass an anonymous method to it that has contains the non-identical code? – Jeroen Wiert Pluimers Nov 02 '11 at 09:05