[fpc-devel] "helper" feature finished

Sven Barth pascaldragon at googlemail.com
Tue Apr 5 11:49:53 CEST 2011


Am 05.04.2011 11:42, schrieb dhkblaszyk at zeelandnet.nl:
> On Tue, 05 Apr 2011 11:17:48 +0200, Sven Barth
> <pascaldragon at googlemail.com> wrote:
>> Am 05.04.2011 04:27, schrieb Paul Ishenin:
>>> 05.04.2011 3:51, Sven Barth wrote:
>>>
>>>> Both "class helpers" and "record helpers" are implemented and work as
>>>> Delphi compatible as reasonably possible.
>>>
>>> Congratulations.
>>>
>>
>> Thank you.
>
> Just from my curiosity. What is the special benefit of a class helper
> over inheritance?

You can add methods to a class that you can't inherit from (e.g. you 
have a 3rd party component that you can't extend, because it's declared 
as sealed). Or instead of defining (global) utility procedures for e.g. 
TStringList in some unit you can create a helper and call those methods 
as if those belong to TStringList.

The question that got me to start this feature was the question whether 
one could get the count of all checked items in a TCheckListBox.

A (simple) helper implementation for this would be the following:

==== source begin ====

TCheckListBoxHelper = class helper for TCheckListBox
private
   function GetCheckedCount: Integer;
public
   property CheckedCount: Integer read GetCheckedCount;
end;

function TCheckListBoxHelper.GetCheckedCount: Integer;
begin
   Result := 0;
   for i := 0 to Count - 1 do
     if Checked[i] then
       Inc(Result);
end;

==== source end ====

Also using the record helpers you can add methods to records that are 
not declared with methods (e.g. some Windows structs or something like 
that...).

Regards,
Sven



More information about the fpc-devel mailing list