Questions tagged [class-helpers]

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:

Delphi documentation: Class_and_Record_Helpers.

22 questions
2
votes
1 answer

How to call the original class's code when a class helper is in scope?

I'm doing some unit testing on an improved version of quicksort. The (hopefully) faster version is implemented using: TArrayHelper = class helper for System.Generics.Collections.TArray .... class procedure Sort(var Values: array of T);…
Johan
  • 74,508
  • 24
  • 191
  • 319
1
vote
1 answer

Class and class-helpers

I am new to c++ and want to ask about class helpers. Well, i have a class and i what class helpers to be in other file. For that i should use namespaces but then i cant #include this file multiple times(because of redefinition). What is the right…
Stals
  • 1,543
  • 4
  • 27
  • 52
1
vote
1 answer

How to access the private method TStreamReader.FillBuffer in Delphi 10.1 Berlin?

How to access the private method TStreamReader.FillBuffer in Delphi 10.1 Berlin, we did it with a class helper before 10.1 - but the proposed solution does not work: uses System.Rtti; procedure TForm1.FormCreate(Sender: TObject); begin …
cydo
  • 187
  • 2
  • 10
1
vote
1 answer

How to add a property to TTabSheet such that it can be used at design time with TPageControl

I would like to add "MyProperty" to TTabSheet and work with it at design time. However if I subclass it I fear I will need to also subclass TPageControl, since it internally creates/manages the TTabSheets. I believe this would require duplicating…
user1627960
  • 421
  • 1
  • 3
  • 10
1
vote
1 answer

Class helper in C++ Builder

I am wondering whether there is a function named Class Helper even for C++ Builder. My Environment: RadStudio XE4 Update 1. I found explanation for Delphi in RadStudio XE4 here I searched similar page for C++ Builder here, but do not find…
sevenOfNine
  • 1,509
  • 16
  • 37
0
votes
0 answers

ClassHelper expand existing function to be virtual

I am using Delphi 10.4 (untested for D11). I am trying to extend a function in an existing class with a ClassHelper so that the function becomes a virtual function. The compilation works partially. TMetaTable = class public function Clone:…
-2
votes
1 answer

Extending class from another unit

We can extend any class so: TColumn = class(FMX.Grid.TColumn) private FId: Integer; public property Id: Integer read FId write FId; end; And this is works fine in our own unit, but if we try this (Grid: TGrid on the form): procedure…
Alex Egorov
  • 907
  • 7
  • 26
1
2