Board index » delphi » Dynamic Type array

Dynamic Type array

Quote
Shannon Hanson (proto...@netrix.net) wrote:

: I need to create a variable number of objects at runtime.
: ...
:   Items : Array[1.. Table.recordcount] of object
:
: Can anyone help ?? My next thought is to use Pointers !! any thoughts ?

Create your objects dynamically and store the pointers to them in a TList
object. You will be able to handle them like an array. Read the online-help
for TList. If you want (or need) i could send you a piece of code that does
it.

hope it helps ... bernd

--
.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.
Bernd Biechele                 be...@sol.wohnheim.uni-ulm.de                
Heilmeyersteige 107/B205       bbiec...@hydra.informatik.uni-ulm.de
D-89075 Ulm
.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.

 

Re:Dynamic Type array


I need to create a variable number of objects at runtime.

For example if I had a table which contained a variable number of food
items and wanted the user to check off items according to some criteria.
 I would want to create a dynamic form that places one checkbox for each
item on a form.    In VB (which I am trying to leave behind) the
programmer can create an array and then REDIM that array to the needed
number.  Since this is not allowed in a compiled lang I thought I should
be able to do the following

  Items : Array[1.. Table.recordcount] of object

This wont compile because table.recordcount can not be evaluated to a
const at compile time. I have even tried four or five ways to 'Trick'
Delphi without success,  Dooooo !!!!

Can anyone help ?? My next thought is to use Pointers !! any thoughts ?

any help would be greatly appreciated.
thanks

Shannon Hanson

Re:Dynamic Type array


Quote
>Can anyone help ?? My next thought is to use Pointers !! any thoughts ?

Your getting close to the answer. In fact I have seen this question posted in
one form or another by many.

You have the option of using the TStringList component where you can
dynamically add objects that you may wish to use -- whether these are
components (buttons, text boxes or other) is unimportant. For example you
would call >> AddObject('SomeName', TObject.Create).

Another option is to create your own unidirectional linked list of TObjects.
In fact that is what TStringlist is doing. Go back to your old Turbo Pascal
books -- some have great examples on linked lists.

By the way never forget that Object Pascal treats all Objects as pointers.

Other Threads