Board index » delphi » Re: record properties

Re: record properties


2003-08-13 12:00:06 PM
delphi63
Something like this (code compiles but was not tested):
unit Unit2;
interface
type
TMyRecord = record
MyInteger: Integer;
MyString: string;
end;
TMyClass = class
private
FMyRecord : TMyRecord;
function GetMyRecord: TMyRecord;
procedure SetMyRecord(const Value: TMyRecord);
public
property RecordA: TMyRecord read GetMyRecord write SetMyRecord;
property RecordB: TMyRecord read GetMyRecord write SetMyRecord;
property RecordC: TMyRecord read GetMyRecord write SetMyRecord;
end;
implementation
{ TMyClass }
function TMyClass.GetMyRecord: TMyRecord;
begin
Result.MyInteger := FMyRecord.MyInteger;
Result.MyString := FMyRecord.MyString;
end;
procedure TMyClass.SetMyRecord(const Value: TMyRecord);
begin
FMyRecord.MyInteger := Value.MyInteger;
FMyRecord.MyString := Value.MyString;
end;
end.
--
Alain Quesnel
XXXX@XXXXX.COM
www.logiquel.com
"Clayton L. Wilson" <XXXX@XXXXX.COM>writes
Quote
I have multiple properties in my component.

property RecordA: TMyRecord read GetMyRecordA write SetMyRecordA;
property RecordB: TMyRecord read GetMyRecordB write SetMyRecordB;
property RecordC: TMyRecord read GetMyRecordC write SetMyRecordC;

All of them are the same type of record structure.

type
TMyRecord = record
MyInteger: Integer;
MyString: string;
end;

How can I use generic Get/Set routines so I don't have to write them for
each property? Example:

procedure TMyComponent.SetMyRecord(TheRecord: PropertyName, RecordData:
TMyRecord);
begin
TheRecord.MyInteger := RecordData.MyInteger;
TheRecord.MyString := RecordData.MyString;
end;

function TMyComponent.GetMyRecord(TheRecord: PropertyName): TMyRecord;
begin
Result.MyInteger := TheRecord.MyInteger;
Result.MyString := TheRecord.MyString;
end;

Thanks for the help.

--
Clayton L. Wilson


 
 

Re: record properties

Hi Clayton!
"Clayton L. Wilson" <XXXX@XXXXX.COM>píše v diskusním příspěvku
Quote
How can I use generic Get/Set routines so I don't have to write them for
each property? Example:
Here comes my version (AFAIK, all record members get copied automatically
upon record assignment):
type
TMyRecord = record
MyInteger: Integer;
MyString: string;
end;
TMyClass = class
private
FMyRecord1: TMyRecord;
FMyRecord2: TMyRecord;
FMyRecord3: TMyRecord;
function GetMyRecord(Index: Integer): TMyRecord;
procedure SetMyRecord(Index: Integer; const Value: TMyRecord);
public
property RecordA: TMyRecord index 0 read GetMyRecord write SetMyRecord;
property RecordB: TMyRecord index 1 read GetMyRecord write SetMyRecord;
property RecordC: TMyRecord index 2 read GetMyRecord write SetMyRecord;
end;
implementation
function TMyClass.GetMyRecord(Index: Integer): TMyRecord;
begin
case Index of
0: Result := FMyRecord1;
1: Result := FMyRecord2;
2: Result := FMyRecord3;
else
raise Exception.Create('Invalid index');
end;
end;
procedure TMyClass.SetMyRecord(Index: Integer; const Value: TMyRecord);
begin
case Index of
0: FMyRecord1 := Value;
1: FMyRecord2 := Value;
2: FMyRecord3 := Value;
else
raise Exception.Create('Invalid index');
end;
end;
Best regards,
Ivo