Board index » delphi » Returning a TStringList

Returning a TStringList

Hi

How do I write a function that returns a TStringList.

Carsten

 

Re:Returning a TStringList


Example:

function GetList: TStrings;
begin
  Result := TStringList.Create;
  Result.Add('A');
  Result.Add('B');
  Result.Add('C');
end;

var
  S: TStrings;
begin
  S := GetList;
  try
    Memo1.Lines.AddStrings(S);
  finally
    S.Free;
  end;
end;

ps. Returning a created object is considered bad coding, because many
programmers tend to forget to free the returned object.

A better example:

procedure GetList(List: TStrings);
begin
  List.Clear;
  List.Add('A');
  List.Add('B');
  List.Add('C');
end;

var
  S: TStrings;
begin
  S := TStringList.Create;
  try
    GetList(S);
    Memo1.Lines.AddStrings(S);
  finally
    S.Free;
  end;
end;

Quote
"Carsten Ingemansson" <cars...@knm-e.se> wrote in message

news:LUez7.639$R43.153954@newsb.telia.net...
Quote
> Hi

> How do I write a function that returns a TStringList.

> Carsten

Other Threads