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
29
votes
2 answers

How do I use class helpers to access strict private members of a class?

This is a follow-up question to: How to hide a protected procedure of an object? (I'm a bit fuzzy on the whole class helper concept) Suppose I have an class like: type TShy = class(TObject) strict private procedure TopSecret; private …
Johan
  • 74,508
  • 24
  • 191
  • 319
17
votes
2 answers

Access a strict protected property of a Delphi class?

I need to access a strict protected property, because I need to create a validation (based in the value of this property) to avoid a bug. (I don't have the source code of the third party class which has this property) only I have the definition of…
Salvador
  • 16,132
  • 33
  • 143
  • 245
11
votes
3 answers

How to access a private field from a class helper in Delphi 10.1 Berlin?

I would like to use Gabriel Corneanu's jpegex, a class helper for jpeg.TJPEGImage. Reading this and this I've learned that beyond Delphi Seattle you cannot access private fields anymore like jpegex does (FData in the example below). Poking around…
stackmik
  • 151
  • 1
  • 10
11
votes
4 answers

Is it possible to use two record helpers for the string type?

I created this helper in order to add some more functions to the string type: type AStringHelper = record helper for string function Invert: string; overload; function InvertMe: string; overload; end; But when I use it in my code, the…
NaN
  • 8,596
  • 20
  • 79
  • 153
11
votes
3 answers

Class Helper for generic class?

I'm using Delphi 2009. Is it possible to write a class helper for a generic class, i.e. for TQueue . The obvious TQueueHelper = class helper of TQueue ... end; does not work, nor does TQueueHelper = class helper of TQueue ... end;
jpfollenius
  • 16,456
  • 10
  • 90
  • 156
10
votes
10 answers

Should Class Helpers be used in developing new code?

Delphi 8 introduced Class Helpers for the purposes of mapping the VCL/RTL to the .NET object hierarchy. They allow injecting methods into an existing class without overriding the the class or modifying the original. Later versions of Delphi found…
Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194
10
votes
1 answer

What the purpose of `ancestor list` in the class helper syntax? Where it is documented? Are there any usage example(s)?

All of the documentation versions, including the one most up to date gives the following class/record helper syntax: type identifierName = class|record helper [(ancestor list)] for TypeIdentifierName memberList end; And it only explains…
Free Consulting
  • 4,300
  • 1
  • 29
  • 50
8
votes
1 answer

Delphi Class Helper RTTI GetMethod

Lets say I have a sample class helper TSampleClassHelper = class helper for TSampleClass public procedure SomeHelper; end; I do the following: var obj :TSampleClass; begin obj:=TSampleClass.Create; obj.SomeHelper; end; and this works as…
AJ.
  • 10,732
  • 13
  • 41
  • 50
5
votes
1 answer

How can I use TExtendedHelper on literals?

With System.SysUtils.TShortIntHelper (and others) I can write: output := 5.ToString(); to format the number 5 as string. As well, there is System.SysUtls.TExtendedHelper, but I'm unable to compile: output := (5.0).ToString(); E2018: Record,…
ventiseis
  • 3,029
  • 11
  • 32
  • 49
5
votes
1 answer

Find all Class Helpers in Delphi at runtime using RTTI?

Does the extended RTTI in Delphi 2010 offer a way to list defined Class and Record Helpers at run time? As far as I know Delphi does not show a hint or warning when more than one class helper is defined for a class, class helper detection might be a…
mjn
  • 36,362
  • 28
  • 176
  • 378
5
votes
3 answers

Is there any way to know if a object is being created or destroyed inside a class helper?

I have few class helpers for components to create sub-components, like popup menus, to access this sub-components in run time, I create a Singleton TDictionary. My question is how do I know that the owner-component is being destroyed to remove the…
Cesar Romero
  • 4,027
  • 1
  • 25
  • 44
4
votes
2 answers

How can access the value of a class var using the address of the class and a offset to the variable?

I Need to access a strict private class var value of a class using his instance and a offset to the variable. so far tried this , check this sample class type TFoo=class strict private class var Foo: Integer; public constructor Create; …
Salvador
  • 16,132
  • 33
  • 143
  • 245
4
votes
3 answers

How to encapsulate different classes within one class mantaining their unique methods? (multiple inheritance in delphi?)

I'm currently rewriting a free educational digital circuit simulator to add inertiality to its features. My problem is how to dispatch events to original classes adding a pre-elaboration to them. I have something like this: TC1 = class ID:…
user284362
3
votes
1 answer

Class helper in C++

In Delphi there are exists class helpers which can add methods for some given class. Is there any design pattern in C++ which can do the same?
vladon
  • 8,158
  • 2
  • 47
  • 91
3
votes
1 answer

How do I call protected methods using class helper?

Suppose we have a classes with method which is potentially very useful, but not available due protected scope: unit Sealed; interface type TGeneral = class(TObject) { this method is useful, but not available } protected procedure Useful;…
Free Consulting
  • 4,300
  • 1
  • 29
  • 50
1
2