Extends existing classes/records in Delphi without using inheritance. Class helpers also introduces a wider scope for the compiler when resolving identifiers. The syntax was introduced in Delphi 2005.
Extends existing classes/records in Delphi without using inheritance. Class helpers also introduces a wider scope for the compiler when resolving identifiers. The syntax was introduced in Delphi 2005.
For classes, use class helper for TMyClass
.
For records, use record helper for TMyRecord
.
Example :
unit1 :
type
TMyClass = class
strict private
fStrictPrivate : integer;
strict protected
fStrictProtected : integer;
public
procedure MyProc;
function MyFunc : string;
end;
{ TMyClass }
function TMyClass.MyFunc: string;
begin
end;
procedure TMyClass.MyProc;
begin
end;
unit 2:
uses unit1;
type
TMyClassHelper = class helper for TMyClass
procedure HelloWorld;
function MyFunc : string;
end;
{ TMyClassHelper }
procedure TMyClassHelper.HelloWorld;
begin
WriteLn( Self.ClassName); // Self refers to TMyClass
WriteLn( Self.fStrictPrivate); // Access to strict private members
WriteLn( Self.fStrictProtected); // Access to strict protected members
end;
function TMyClassHelper.MyFunc: string;
begin
end;
...
var
x : TMyClass;
begin
x := TMyClass.Create; // Visibility of TMyClassHelper is enough for the compiler
x.MyProc; // Calls TMyClass.MyProc
x.HelloWorld; // Calls TMyClassHelper.HelloWorld
x.MyFunc; // Calls TMyClassHelper.MyFunc;
Resources: