[Israel.pm] Fwd: Delegates in Object Pascal
Shlomi Fish
shlomif at iglu.org.il
Sat Feb 3 10:53:16 EET 2007
Hi all!
Here is ik's example of delegating methods using Object Pascal. I dare say
it's hard for me to follow it because it's too verbose. From what I
understand, he demonstrated one delegated method, but the question is whether
it can be done with several delegated methods of different type signatures,
while just listing their names (as is possible in Perl 5).
Regards,
Shlomi Fish
---------- Forwarded Message ----------
Date: Monday 29 January 2007 19:59
From: ik <idokan at gmail.com>
To: "Shlomi Fish" <shlomif at iglu.org.il>
Anyway, for delegating stuff using Object Pascal you can do the following:
program EventExample;
uses
SysUtils, Classes;
type
{ define the event type }
TOnCalc = procedure (APercent: Integer) of object;
{ class that triggers the event }
TCalculator = class
private
FOnCalc: TOnCalc;
public
procedure Calculate(A, B: Integer);
property OnCalc: TOnCalc read FOnCalc write FOnCalc;
end;
{ class that uses the event }
TPresenter = class
public
procedure PresenterOnCalc(APercent: Integer); // <--- this is the
delegated method
procedure ShowCalc;
end;
implementation
{ TCalculator }
procedure TCalculator.Calculate(A, B: Integer);
begin
Writeln(A + B);
{ Execute the event if assigned from outside this class }
if Assigned(OnCalc) then
OnCalc(10); // 10 is the percent calculated at this moment
end;
{ TPresenter }
procedure TPresenter.ShowCalc;
var
lCalculator: TCalculator;
begin
lCalculator := TCalculator.Create;
lCalculator.OnCalc := PresenterOnCalc; // <-- assign the delegate
lCalculator.Free;
end;
procedure TPresenter.PresenterOnCalc(APercent: Integer);
begin
Writeln('Calculated at this moment: ' + APercent + '%');
end;
end.
---------------------------------------------------------------------
Shlomi Fish shlomif at iglu.org.il
Homepage: http://www.shlomifish.org/
Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.
More information about the Perl
mailing list