Talk:Determining What to Store

From RAD Studio
Jump to: navigation, search

The stored attribute only applies to certain types of properties. For example, floating point numbers are only stored if they have a value different from zero regardless of the value of stored. In addition, certain types of properties, such as records, can not be stored at all by the default mechanism. It is possible to use DefineProperties to overcome these limitations as illustrated below.

 TRealStorage = class(TPersistent)
 private
   FValue: real;
 protected
   procedure ReadValue(Reader: TReader);
   procedure WriteValue(Writer: TWriter);
   procedure DefineProperties(Filer: TFiler); override;
 public
   procedure Assign(Source: TPersistent); override;
 published
   property Value: real read FValue write FValue;
 end;

{ TRealStorage }

procedure TRealStorage.Assign(Source: TPersistent); begin

 if Source is TRealStorage then
 begin
   Value := TRealStorage(Source).Value;
 end
 else
 begin
   inherited;
 end;

end;

procedure TRealStorage.DefineProperties(Filer: TFiler); begin

 inherited DefineProperties(Filer);
 Filer.DefineProperty('Value', ReadValue, WriteValue, Value = 0)

end;

procedure TRealStorage.ReadValue(Reader: TReader); begin

 Value := Reader.ReadFloat;

end;

procedure TRealStorage.WriteValue(Writer: TWriter); begin

 Writer.WriteFloat(Value);

end;