Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-01

delegate objects

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hi List,

There is a small conversation I read regarding delegating methods in objects.

The thing is something like so (from the original discussion)

<<<<<<<<<
sub factors
{

   my $self = shift;
   return $self->result()->factors();
}
>>>>>>>>

And besides it similar declarations for some other functions:

<<<<<<<<<<
sub is_square
{

   my $self = shift;
   return $self->result()->is_square();

}

sub exists
{

   my ($self, $factor) = @_;
   return $self->result()->exists($factor);
}

sub first
{

   my $self = shift;
   return $self->result()->first();
}
>>>>>>>>>>

Is there a way do delegate such methods on classes using Object Pascal ? If so, how would you do it ?

Thanks,

Ido

Reply by email

Leonardo M. Ram�
to ik
Is there a way do delegate such methods on classes using Object Pascal ? If so, how would you do it ?

Yes, delegates are called "Events" in Object Pascal.

Here a simple example:

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.

Leonardo M. Ramé
http://leonardorame.blogspot.com

____________________________________________________________________________________ Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html

Reply by email

Leonardo M. Ram�
to ik: There is a small conversation I read regarding delegating methods in objects. The thing…
Is there a way do delegate such methods on classes using Object

Pascal ?

If so, how would you do it ?

Yes, delegates are called "Events" in Object Pascal.

Here a simple example:

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.

Leonardo M. Ramé
http://leonardorame.blogspot.com

____________________________________________________________________________________ Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains. http://farechase.yahoo.com/promo-generic-14795097

Reply by email

ik
to Leonardo M. Ram�

Hi,

Thank you for the example.
I will study it more on the weekend, when I'll have more time to learn it ..

Ido

Leonardo M. Ramé wrote 75 lines
> Is there a way do delegate such methods on classes using Object Pascal ? > If so, how would you do it ? Yes, delegates are called "Events" in Object Pascal. Here a simple example: 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. Leonardo M. Ramé http://leonardorame.blogspot.com ____________________________________________________________________________________ Finding fabulous fares is fun. Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains. http://farechase.yahoo.com/promo-generic-14795097 _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email