␡
- Defining Properties
- An Example of DefineProperty()
- TddgWaveFile: An Example of DefineBinaryProperty()
Like this article? We recommend
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 1DefProp.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.