size : 2254 uploaded_on : Tue Apr 6 00:00:00 1999 modified_on : Wed Dec 8 14:03:38 1999 title : TList example org_filename : TListEx.txt author : Theo Bebekis authoremail : bebekis@otenet.gr description : An example of using TList keywords : tested : not tested yet submitted_by : The CKB Crew submitted_by_email : ckb@netalive.org uploaded_by : nobody modified_by : nobody owner : nobody lang : plain file-type : text/plain category : delphi-misc __END_OF_HEADER__ procedure TForm1.Button1Click(Sender: TObject); var MyComponent : TComponent; i : PInteger; List : TList; begin List := TList.Create; { get memory for an integer } GetMem( i, SizeOf(integer)); { assign it a value and add it to the list. because it is the first addition it goes to position zero } i^:= 5; List.Add(i); { typecast list's zero item to integer convert it to string and show it} ShowMessage( IntToStr(Integer(List[0]^)) ); { free the mem and nilify the zero item } FreeMem( i, SizeOf(integer)); List[0] := nil; { now create a component } MyComponent := TComponent.Create(nil); { and INSERT it to zero position zero position still exists while we do not delete it} List.Insert(0, MyComponent); { typecast the zero item... etc... } TComponent(List[0]).Name := 'MyNiceComponent'; ShowMessage(MyComponent.Name); // it could be : ShowMessage(TComponent(List[0]).Name ); TComponent(List[0]).Free; List.Free; end; We need to be very carefull when working with those lists. It is very easy to have memory leaks or access violations if forget what you are doing. If you alreade have added an item to that index then it is possible to update an object at the refered index. But do not forget to deallocate the memory. Here is the variation procedure TForm1.Button1Click(Sender: TObject); var MyComponent : TComponent; i : PInteger; List : TList; begin List := TList.Create; { get memory for an integer } GetMem( i, SizeOf(integer)); { assign it a value and add it to the list. because it is the first addition it goes to position zero } i^:= 5; List.Add(i); { typecast list's zero item to integer convert it to string and show it} ShowMessage( IntToStr(Integer(List[0]^)) ); { nilify the zero item } List[0] := nil; { now create a component } MyComponent := TComponent.Create(nil); { and store it to zero position } List[0] := MyComponent; { typecast the zero item... etc... } TComponent(List[0]).Name := 'MyNiceComponent'; ShowMessage(TComponent(List[0]).Name); MyComponent.Free; FreeMem( i, SizeOf(integer)); { do not forget this } List.Free; end;