[Lazarus] property setter

Paul Ishenin ip at kmiac.ru
Thu Jan 24 03:18:15 CET 2013


24.01.2013 10:08, xrfang пишет:

> procedure TPainter.SetFont(AValue: TFont);
> begin
>    if FFont=AValue then Exit;
>    FFont:=AValue;
> end;
>
> My question is, why the "if" clause needed? Is there any performance
> benefits?

Usually setters have more complex code than just FFont:=AValue;

In this case If block protects both code performance and code logic. 
Setters usually should not perform any actions if they are called with 
the same value as property have.

If setter only assigns a value to a field then probably it can be 
replaced with the field in the property declaration.

Best regards,
Paul Ishenin





More information about the Lazarus mailing list