1. Home
  2. Computing & Technology
  3. Delphi Programming

Demystifying Class Helpers in Delphi for .NET
Fast Forward to Delphi for .NET - Part VII.

By Zarko Gajic, About.com

This is the seventh article in the series of articles designed with one goal in mind: to provide a quick and dirty introduction to the world of .Net programming with Delphi.

Delphi for .NET compiler magic: splitting a large class into two files. And much more: extending without inheriting, extending sealed classes, ...

One of the new language features in Delphi for .NET are Class helpers. In simple words, a class helper is a construct that extends a class with all fields from the helper class. It allows you to extend existing class without actually modifying it or inheriting from it. The main reason Borland needed to extend the language with class helpers was to make VCL portable to .NET.

Here's an example (no implementations, only class definition / method interface):

TTest = class
   procedure SomeProc;
end;

TTestHelper = class helper for TTest
   function SomeFunc : string;
end;

With the above class definitions, we can use the next code:

var
   t:TTest;
begin
  t:=TTest.Create;
  t.SomeFunc ;
...
even though SomeFunc was *not* defined in class TTest!

Class Helpers: Implications (and Limitations)

Let’s consider some implications of Class Helpers. Class Helpers allow Delphi developers to:
  1. Extend existing CLR ("native" .NET) classes without modifying their implementation.
  2. Extend existing CLR classes without inheriting from them.
  3. Extend sealed classes. Note: A sealed class is one that cannot be extended through inheritance.
  4. Split a (large) class definition across 2 (or more) source files.
There are a few limitation, though
  1. A class helper cannot access (strict) private fields of the class it extends. Note: this is not a true limitation.
  2. A class helper cannot implement an interface for the class it extends.
  3. You cannot define a class to be a helper for a class that already is a class helper.
In other words, the next code will not compile:

TTest = class; //ok
THelper = class helper for TTest; //ok
TDoubleHelper = class helper for THelperClass. // NOT ok

While extending existing classes can come really helpful, the ability to span a class definition into 2 source files ads several nifty features to Delphi developers:

  1. Class helpers are great when several developers work on the same class. Without Class Helpers team would have to use source code management features such as check-in/check-out and merge changes for multiple programmers to work on a single class; with class helpers, each team member can work on a separate part of the class, and the system will handle merging the separate code files back into a single class at compile time.
  2. If your class is auto created using some kind of code generator, using class helpers would save you from losing custom parts of the class, when the class needs to be regenerated. Simply add any additional methods to a class helper.
  3. Suppose you need to maintain two versions of your product, a component maybe, where one version has more features than the other one (i.e. "Professional" and "Standard" version of a component). Class helpers allow you to add all the "Professional" features in a separate file. When you build the "Standard" version, you simply exclude (from the project) the unit where the class helper was used!

Class Helper Example

To understand how class helpers operate, I'll present you with one example. The "ClassHelpers " is a console mode application. The project contains two units. Unit "Test.pas" defines the TTest class with one class procedure "TestProcedure". Unit "TestHelper.pas" defines a TTestHelper class as a class helper for TTest. TTestHelper ads a procedure "HelperProcedure" to the TTest class.

~~~~~~~~~~~~~~~~~~~~~~~~~
program ClassHelpers;
{$APPTYPE CONSOLE}
uses
   Test in 'Test.pas',
   TestHelper in 'TestHelper.pas';
begin
   TTest.TestProcedure; //defined in TTest
   TTest.HelperProcedure; // defined in TTestHelper
end.

unit Test;
interface
type
   TTest = class
     class procedure TestProcedure;
   end;

unit TestHelper;
interface
   uses Test;
type
   TTestHelper = class helper for TTest
     class procedure HelperProcedure;
   end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Download full source.

Only in Delphi? How about VB.NET, C# or other languages? Partial Types?

Class helpers are a specific Delphi for .NET language feature. Neither VB.NET nor C# developers cannot use the ideas shown above.

Note, however, that the next version of the .NET framework, code named "Whidbey", ads one similar feature: Partial Types.
The simple idea of Partial Types is to have a class or a structure definition/implementation split across multiple source files.

However, Microsoft's Partial Types do not allow already compiled types to be extended - while Borland's Class Helpers do!

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Learn Delphi for .NET
  5. Demystifying Class Helpers in Delphi for .Net

©2009 About.com, a part of The New York Times Company.

All rights reserved.