TSomeRecord = record
S1, S2, S3 : string;
end;
{ TIntClass }
TIntClass = class
private
fSomeRec: TSomeRecord;
function GetStr(index: integer): string;
procedure SetStr(index: integer; AValue: string);
public
property S1: string Index 1 read GetStr write SetStr; //<-- property indexing technique, just one get and set for all Sj
property S2: string Index 2 read GetStr write SetStr;
property S3: string Index 3 read GetStr write SetStr;
end;
{ TAClass3 }
TAClass3 = class
private
fSomeArr: array [0..1] of TIntClass;
private
procedure SetSomeArr(index: Integer; AValue: TIntClass);
function GetSomeArr(index: Integer): TIntClass;
public
property SomeArr [index: Integer]: TIntClass read GetSomeArr write SetSomeArr; default;
procedure InitData;
constructor Create;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
var
TmpRec : TAClass3;
{ TIntClass }
function TIntClass.GetStr(index: integer): string;
begin
case index of
1:result:=fSomeRec.S1;
2:result:=fSomeRec.S2;
3:result:=fSomeRec.S3
end;
end;
procedure TIntClass.SetStr(index: integer; AValue: string);
begin
case index of
1:fSomeRec.S1:=AValue;
2:fSomeRec.S2:=AValue;
3:fSomeRec.S3:=AValue
end;
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
case CheckBox1.checked of
true:begin
TmpRec[0].S1:=Edit1.Text;
TmpRec[0].S2:=Edit2.Text;
TmpRec[0].S3:=Edit3.Text;
end;
false:begin
TmpRec[1].S1:=Edit1.Text+'2';
TmpRec[1].S2:=Edit2.Text+'2';
TmpRec[1].S3:=Edit3.Text+'2';
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
case CheckBox1.checked of
true:begin
Edit4.Text:=TmpRec[0].S1;
Edit5.Text:=TmpRec[0].S2;
Edit6.Text:=TmpRec[0].S3;
end;
false:begin
Edit4.Text:=TmpRec[1].S1;
Edit5.Text:=TmpRec[1].S2;
Edit6.Text:=TmpRec[1].S3;
end;
end;
end;
{ TAClass3 }
procedure TAClass3.SetSomeArr(index: Integer; AValue: TIntClass);
begin
fSomeArr[index].fSomeRec:=AValue.fSomeRec; //caution here... copy only data
end;
function TAClass3.GetSomeArr(index: Integer): TIntClass;
begin
Result:=fSomeArr[index];
end;
procedure TAClass3.InitData;
begin
fSomeArr[0].S1:='test1';
fSomeArr[0].S2:='test2';
fSomeArr[0].S3:='test3';
end;
constructor TAClass3.Create;
begin
inherited Create;
fSomeArr[0]:=TIntClass.Create;
fSomeArr[1]:=TIntClass.Create;
end;
destructor TAClass3.Destroy;
begin
fSomeArr[1].Free;
fSomeArr[0].Free;
inherited Destroy;
end;
initialization
TmpRec:=TAClass3.Create;
end.