1

I want to use multi TList in Delphi. For example:

var
 temp1List : TList;
 temp2List : TList;
begin
 temp1List := TList.Create;
 temp2List := TList.Create;
 temp1List.add(temp2List);
end;

I think it is not correct because TList accepts parameter as Pointer value.

Is there way to use multi TList?

1 Answer 1

4

Have a look at the Generic TList<T> instead, eg:

uses
  ..., System.Classes, System.Generics.Collections;

var
  temp1List : System.Generics.Collections.TList<System.Classes.TList>;
  temp2List : System.Classes.TList;
begin
  temp1List := System.Generics.Collections.TList<System.Classes.TList>.Create;
  temp2List := System.Classes.TList.Create;
  temp1List.Add(temp2List);
  // don't forget to free them when you are done...
  temp1List.Free;
  temp2List.Free;
end;

Alternatively, since TList is a class type, you can use TObjectList<T> instead, and take advantage of its OwnsObjects feature:

uses
  ..., System.Classes, System.Generics.Collections;

var
  temp1List : System.Generics.Collections.TObjectList<System.Classes.TList>;
  temp2List : System.Classes.TList;
begin
  temp1List := System.Generics.Collections.TObjectList<System.Classes.TList>.Create; // takes Ownership by default
  temp2List := System.Classes.TList.Create;
  temp1List.Add(temp2List);
  // don't forget to free them when you are done...
  temp1List.Free; // will free temp2List for you
end;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.