Board index » delphi » NEWBIE Q: Looping through all instances of an object

NEWBIE Q: Looping through all instances of an object

Howdy,

What is the best way to implement a system to loop through all instances of a
TObject and using its fields? I have tried joining the objects in a linked
list, but I suspect there is a better way.

Any help would be much appreciated!

Cheers,
John K

   \\\\   John Kean
   OO |   ~~~~~~~~~
   <  )   keanj@ag_research.cri.nz
   \/ |   (remove underscore when replying)
   `--

 

Re:NEWBIE Q: Looping through all instances of an object


remove underscore when replying John Kean wrote:

Quote

> Howdy,

> What is the best way to implement a system to loop through all instances of a
> TObject and using its fields? I have tried joining the objects in a linked
> list, but I suspect there is a better way.

        It's impossible to iterate through all existing instances of TObject
(or of some more specific type) - there's no record anywhere of all the
objects that have been created. If you have a list of objects that you
want to iterate through you need to keep track of them yourself - you
could do this with a TList instead of your linked list.

        (If you're talking about all components on a form that's
different - the reason it's different is that the form _does_
keep a list of all its components handy.)

--
David Ullrich

sig.txt not found

Re:NEWBIE Q: Looping through all instances of an object


keanj@ag_research.cri.nz (remove underscore when replying) (John Kean)
wrote:

Quote
>What is the best way to implement a system to loop through all instances of a
>TObject and using its fields? I have tried joining the objects in a linked
>list, but I suspect there is a better way.

Not entirely sure what you're looking for, but...

If you're creating the objects dynamically at run-time, you can store
them in some form of TList instead of a linked list, which is easier
to use and probably more efficient.  (Personally, I'd use a
TStringList, since it lets you store a descriptive string with each
object - like, for instance, its name - and perform searches easily.
YMMV, of course.)

If you want to go through a bunch of similar components on a form, you
can loop through the form's Components[] array property and use the
'is' operator to identify which ones are of interest.

Either way, you'll probably want to use 'as' or an explicit typecast
to get Delphi to recognize the object as the desired type.

--
To a man who only has VB, everything looks like a control array.

Other Threads