How to get rid of published property in Delphi

note This article available in Russian.

Preface

Developing PostgresDAC I faced such problem twice. And both times changes affected TPSQLDump component as most developing class.

Mission

Change TPSQLDump.TableName: string property to TPSQLDump.TableNames: TStrings.

Solution

Code speaks for itself:

    TPSQLDump = class(TComponent)
      private
      ...
        procedure ReadTableName(Reader: TReader);
      protected
        procedure DefineProperties(Filer: TFiler); override;
      ...
      published
      ...
        //property TableName: string ...
        property TableNames: TStrings read FTableNames
                                   write SetTableNames;
      ...
    end; //TPSQLDump

implementation

...

procedure TPSQLDump.DefineProperties(Filer: TFiler);
begin
  inherited;
  Filer.DefineProperty('TableName', ReadTableName, nil, False);
end;

procedure TPSQLDump.ReadTableName(Reader: TReader);
var S: string;
begin
  S := Reader.ReadString;
  if S > '' then FTableNames.Append(S);
end;

Explanation

We defined TableName property as if it still exists and assigned ReadTableName function to read its value. Since we didn’t assigned a function for writing (we pass nil as second parameter) this property will never be stored in the future. Problem solved.

PS

Thanks to Alexey Vukolov aka vuk for the idea.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s