Einzelnen Beitrag anzeigen

Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#3

Re: Listen zurückgeben - best practice

  Alt 1. Apr 2010, 13:54
Also ich habe es bisher über einen var/out Parameter gelöst.

Da kann der Benutzer der Funktion nicht aus Versehen den Rückgabewert ignorieren.
Also sowas wie:

Delphi-Quellcode:
function foo(): TList<MyClass>
begin
  Result := ...
end;


begin
  foo(); // BÖSE
end;
geht dann nicht.

Bei mir ginge es dann so:

Delphi-Quellcode:
function foo(var Result: TList<MyClass>): Irgendwas; // oder Procedure
begin
  Result.DoSomething;
  Result.Add(Irgendwas);
end;

var AList: TList<MyClass>;
begin
  AList := TList<MyClass>.Create(...);
  try
     foo(AList);
     // Do something with AList
  finally
    AList.Free;
  end;
end;
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat