Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Component Custom property not to be saved on the DFM files

I have a property on a custom component that I don't want to be saved on the DFM files. I have overrided the DefineProperties method to provide no ReadData and WriteData procedures, expecting that it won't save its value, but it still does.

procedure TAEFDQuery.DefineProperties(Filer: TFiler);
begin
  inherited;
  // Set the ADO Compatibility custom properties to not be saved on the DFM
  Filer.DefineProperty('CommandText', nil, nil, False);
end;

The reason to not save this property is because I have ported a project from ADO to FireDAC and I have created "fake" properties that allows some ADO code to run unchanged, redirecting it to their correspondent FireDAC properties.

type
    TAEFDQuery = class(TFDQuery)
    private
        function GetCommandText: string;
        procedure SetCommandText(AValue: string);
    protected
        procedure DefineProperties(Filer: TFiler); override;
    published
        property CommandText: integer read GetCommandText write SetCommandText;
    end;

implementation

procedure TAEFDQuery.SetCommandText(AValue: string);
begin
  SQL.Text := AValue;
end;

function TAEFDQuery.GetCommandText: string;
begin
  Result := SQL.Text;
end;

procedure TAEFDQuery.DefineProperties(Filer: TFiler);
begin
  inherited;
  // Set the ADO Compatibility custom properties to not be saved on the DFM
  Filer.DefineProperty('CommandText', nil, nil, False);
end;

How is the correct way to keep these "fake" properties, for compatibility sake, without having them filling the DFM files with useless copies of the real properties ?.

Thank you.

like image 513
Marc Guillot Avatar asked Dec 05 '22 08:12

Marc Guillot


2 Answers

Add a storage specifier to the property that is or returns false.

 property CommandTimeout: integer read GetCommandTimeout write SetCommandTimeout stored False;

ref: Properties (Delphi) -> Storage Specifiers

like image 191
Brian Avatar answered Jan 26 '23 00:01

Brian


Another way to prevent the property from being saved to the DFM is to simply declare the property as public instead of published, as only published properties are streamed in/out of a DFM.

type
  TAEFDQuery = class(TFDQuery)
  private
    function GetCommandText: string;
    procedure SetCommandText(AValue: string);
  public
    property CommandText: integer read GetCommandText write SetCommandText;
  end;

It doesn't make sense to have a published property exposed to the Object Inspector at design-time if it can't be saved. If you are just trying to port some old code, then you don't need the added design-time support for old properties.


With that said, for purposes of porting old code, consider using a class helper instead of deriving a full component, eg:

unit MyFDQueryHelper;

interface

uses
  FireDAC.Comp.Client;

type
  TAEFDQuery = class helper for TFDQuery
  private
    function GetCommandText: string;
    procedure SetCommandText(AValue: string);
  public
    property CommandText: integer read GetCommandText write SetCommandText;
  end;

implementation

procedure TAEFDQuery.SetCommandText(AValue: string);
begin
  Self.SQL.Text := AValue;
end;

function TAEFDQuery.GetCommandText: string;
begin
  Result := Self.SQL.Text;
end;

end.

Now, you can simply add MyFDQueryHelper to the uses clause of a unit and any TFDQuery objects in that unit will automatically gain the new CommandText property. No need to replace TFDQuery objects with TAEFDQuery objects.

like image 40
Remy Lebeau Avatar answered Jan 25 '23 23:01

Remy Lebeau