[Lazarus] Different "default" value for a property in derived class

Michael Van Canneyt michael at freepascal.org
Sat Apr 6 16:21:14 CEST 2013



On Sat, 6 Apr 2013, Bart wrote:

> Hi,
>
> Consider this.
>
> type
>
>  TSomeType = (stA, stB, stC);
>
>  { TA }
>
>  TA = class
>  private
>    FFoo: TSomeType;
>  public
>    constructor Create;  virtual;
>  published
>    property Foo: TSomeType read FFoo write FFoo default stA;
>  end;
>
>  { TB }
>
>  TB = class(TA)
>  public
>    constructor Create; override;
>  end;
>
> implementation
>
> { TB }
>
> constructor TB.Create;
> begin
>  inherited Create;
>  Foo := stB;
> end;
>
> { TA }
>
> constructor TA.Create;
> begin
>  FFoo := stA;
> end;
>
> Now imagine that you drop an instance ot TB on a form.
> In OI you set Foo to stA.
>
> You build and run, and to your surprise Foo is stB.
> You think for a while and realize what happened: since property TA.Foo
> is declared with "default stA", this means that if you set Foo in OI
> to stA it is not streamed to the LFM.
>
> So far, so good.
>
> Now the question: is there any other way to fix this besides removing
> the "default stA' from the declaration of the TA.Foo property?
> (Assuming that it is necessary for TB to set Foo to stB by default).

Just change the default:

   TB = class(TA)
   public
     constructor Create; override;
   published
     property foo default stB;
   end;

Michael.




More information about the Lazarus mailing list