Delphi – File IO with TStringList

TStringList in the Classes namespace provides a really quick and  easy way to read (LoadFromFile) and write (SaveToFile) files.  For instance, if you want the entire file contents read in to a TStringList you can do the following.

function TSomething.ReadFile(filePath : string) : TStringList;
begin
    if FileExists(filePath ) then
    begin
        result := TStringList.create();
        result.LoadFromFile(filePath);
    end
    else
        result := nil;
end;

Delphi Callbacks/Function Pointers

While working on a legacy Delphi project recently at work,  the need arose to use a callback which Delphi makes surprisingly easy.

Define the type:

type
  TCallBackProc = procedure(paramOne : string) : of object;

Call:

procedure ExecCallBack(callBack : TCallBackProc)
begin
  callBack('test');
end;