Board index » delphi » TList-Questions

TList-Questions

Hello,

I have some question about the TList-Object for which I couldn't find the
answers in the help/doku. Perhaps anybody can help me?

1.) If I want to add a record to the list have I allocate the memory for each
record or will this be done by the TList-Object, e.g. which of the following
is correct:
    a)  new(recordptr)                    b) for a:=1 to 100 do begin
        for a:=1 to 100 do begin               new(recordptr)
          recordptr^.value:=a;                 recordptr^.value:=a;
          <TList>.add(recordptr)               TList.add(recordptr);
        end                                  end;
2.) Dispose the TList-Object the memory it/I allocated for the entries if I
free the object with TList.free of have I delete every entry manually before
freeing the object in cause not to loose memory-resources?
3.) Can I fill up a TStringList-Objects with values, then assign it via
Assign()-method to an item-property of a ComboBox, then clear the TStringList
with the clear-method, fill it up new and then assign it to another ComboBox,
e. g. stores the ComboBox a pointer or the values in items?
4.) Can I test if a TList-object has been created be testing it =Nil and if it
not Nil then do a TList.Free and create it newly?
5.) Is it possibly to save in a List/ComboBox a record (i. e. an index number
and a string) and only show the string on the screen? (I need the number to
for reference but it shouldn't be visible to the user).

I'm glad if you can mail directly to me address:
stefan.west...@stud.uni-bamberg.de

Thanks...

Stefan

 

Re:TList-Questions


Quote
Stefan Westner wrote:

> Hello,

> I have some question about the TList-Object for which I couldn't find the
> answers in the help/doku. Perhaps anybody can help me?

> 1.) If I want to add a record to the list have I allocate the memory for each
> record or will this be done by the TList-Object, e.g. which of the following
> is correct:
>     a)  new(recordptr)                    b) for a:=1 to 100 do begin
>         for a:=1 to 100 do begin               new(recordptr)
>           recordptr^.value:=a;                 recordptr^.value:=a;
>           <TList>.add(recordptr)               TList.add(recordptr);
>         end                                  end;

b) is correct.  You have to perform the allocation yourself.

Quote
> 2.) Dispose the TList-Object the memory it/I allocated for the entries if I
> free the object with TList.free of have I delete every entry manually before
> freeing the object in cause not to loose memory-resources?

You must free the objects yourself prior to freeing the list itself.  TList.Free does NOT do this.

Quote
> 3.) Can I fill up a TStringList-Objects with values, then assign it via
> Assign()-method to an item-property of a ComboBox, then clear the TStringList
> with the clear-method, fill it up new and then assign it to another ComboBox,
> e. g. stores the ComboBox a pointer or the values in items?

Sorry I don't quite understand.  I think that the answer is yes, however be aware that TStringList.Clear does
not (despite what the help may say) free off pointeed-to objects!

Quote
> 4.) Can I test if a TList-object has been created be testing it =Nil and if it
> not Nil then do a TList.Free and create it newly?

Performing myObject.Free prior to reallocating your object is safe as Free can cope with Nil object pointers.

Quote
> 5.) Is it possibly to save in a List/ComboBox a record (i. e. an index number
> and a string) and only show the string on the screen? (I need the number to
> for reference but it shouldn't be visible to the user).

The items in a TList/TCombobox each have an index number associated with them.  You can ascertain this by
performing the methods IndexOf and ItemIndex on a TCombobox.Items or IndexOf on a TList.

Sorry it's not a complete answer, but I'm no great expert.  Hope that this is of some use.

Andy Coshan

Other Threads