In Delphi 2010 is there a way to iterate through any kind of indexed property (like Pages of TPageControl, for example)?
-
1@TLame `ActivePageIndex` is not an indexed property. – David Heffernan Oct 31 '11 at 13:57
-
I didn't understand your comment, quite offensive by the way... I didn't say anything about ActivePageIndex, I don't know where you saw it. I said Pages. – Rafael Piccolo Nov 01 '11 at 02:07
-
I presume you are referring to the deleted comments – David Heffernan Nov 01 '11 at 07:20
2 Answers
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.

- 858
- 1
- 5
- 11
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).

- 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
-
1What 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