Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally make a property save to the DFM or not?

I have a pair of components, one of the components gets "attached" to the other by setting a property. For example...

type
  TMain = class(TComponent)
  ...
  published
    property Default: Integer read FDefault write SetDefault;
  end;

  TSub = class(TComponent)
  ...
  published
    property Value: Integer read GetValue write SetValue;
    property Main: TMain read FMain write SetMain;
  end;

So in the object inspector for TSub, user would would choose the TMain to be associated with it.

In the sub component, I have a property Value with both a getter and setter. In the event that the sub component's value is set to 0, the getter then obtains the Default property from the TMain that it's attached to...

function TSub.GetValue: Integer;
begin
  if FValue = 0 then begin
    if Assigned(FMain) then begin
      Result:= FMain.Default;
    end else begin
      Result:= 0;
    end;
  end else begin
    Result:= FValue;
  end;
end;

That makes the object inspector (and thus the property its self) return the default value from the main rather than the set 0 value.

What I would like to do is make sure that when the TSub component's properties get saved to the DFM, that it doesn't save this property if it's 0 (thus using the default from the main instead). Currently, after saving the DFM, whatever value came from the default of the main will get saved in the value for the sub, which is not what I want.

Naturally, a property would be marked as default 0; for example indicating that if the property's value is set to 0, then that property wouldn't get saved into the DFM. But since the default might vary, there's no way I can flag a default value for this property (because it expects the default value to be defined).

How can I structure the TSub component to not save this property to the DFM if it's been set to 0 and instead using the default from the main in the property getter?

like image 640
Jerry Dodge Avatar asked Dec 13 '13 01:12

Jerry Dodge


1 Answers

property Value: Integer read GetValue write SetValue stored IsValueStored;

where

function TSub.IsValueStored: Boolean;
begin
  Result := (FValue <> 0) or (FMain = nil);
end;

İf I get it right.

like image 147
Sertac Akyuz Avatar answered Sep 30 '22 12:09

Sertac Akyuz