Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
From the author of

An Example of DefineProperty()

To bring all this rather technical information together, Listing 1 shows the DefProp.pas unit. This unit illustrates the use of DefineProperty() by providing storage for two private data fields: a string and an integer.

Listing 1—DefProp.pas Illustrated Using the DefineProperty() Function

unit DefProp;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
 TDefinePropTest = class(TComponent)
 private
  FString: String;
  FInteger: Integer;
  procedure ReadStrData(Reader: TReader);
  procedure WriteStrData(Writer: TWriter);
  procedure ReadIntData(Reader: TReader);
  procedure WriteIntData(Writer: TWriter);
 protected
  procedure DefineProperties(Filer: TFiler); override;
 public
  constructor Create(AOwner: TComponent); override;
 end;

implementation

constructor TDefinePropTest.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 { Put data in private fields }
 FString := 'The following number is the answer...';
 FInteger := 42;
end;

procedure TDefinePropTest.DefineProperties(Filer: TFiler);
begin
 inherited DefineProperties(Filer);
 { Define new properties and reader/writer methods }
 Filer.DefineProperty('StringProp', ReadStrData, WriteStrData,
  FString <> '');
 Filer.DefineProperty('IntProp', ReadIntData, WriteIntData, True);
end;

procedure TDefinePropTest.ReadStrData(Reader: TReader);
begin
 FString := Reader.ReadString;
end;

procedure TDefinePropTest.WriteStrData(Writer: TWriter);
begin
 Writer.WriteString(FString);
end;

procedure TDefinePropTest.ReadIntData(Reader: TReader);
begin
 FInteger := Reader.ReadInteger;
end;

procedure TDefinePropTest.WriteIntData(Writer: TWriter);
begin
 Writer.WriteInteger(FInteger);
end;

end.

CAUTION

Always use the ReadString() and WriteString() methods of TReader and TWriter to read and write string data. Never use the similar-looking ReadStr() and WriteStr() methods because they'll corrupt your DFM file.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.