Forum > General

Array of Record as Property

<< < (3/3)

Weitentaaal:

--- Quote from: MarkMLl on June 29, 2022, 10:23:58 am ---
--- Quote from: Weitentaaal on June 29, 2022, 07:39:25 am ---Attached a small example

--- End quote ---

OK, so the missing link is that it's mode objfpc and you've got H+.

Zvoni's obviously got a neat solution, but TBH I think I'd go for the verbose solution even if it required a great deal of boilerplate code, since that unambiguously avoids shuffling fields around when they're not going to be changed.

In the general case and noting that in your example the elements are of the same type, do you really need to have a record with (named) fields or could this be done with an array with indexed elements?

MarkMLl


--- End quote ---

yes i need  a record with (named) fields because in my Code its a rcord of Double, String and integer. Didn't thought about this when writing the example code, just wanted to try this.

Thank you i will take one of the various Solution you guys presented me :)

Paolo:
this can help ?

see attached file


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  StrNum = (S1, S2, S3);    //-- her you can add/remove other elements without change next code   TStrArr = array [StrNum] of string;   TSomeRecord2 = record    String_ : TStrArr;  end;    { TAClass2 }   TAClass2 = class  private    fSomeArr: Array[0..1] of TSomeRecord2;  private    function GetSomeArrStr(index: integer; SNum: StrNum): string;    procedure SetSomeArrStr(index: integer; Snum: StrNum; AValue: string);    procedure SetSomeArr(index: Integer; AValue: TSomeRecord2);    function GetSomeArr(index: Integer): TSomeRecord2;  public    property SomeArr [index: Integer]: TSomeRecord2 read GetSomeArr write SetSomeArr;    property SomeArrStr [index1: integer; SNum: StrNum]: string read GetSomeArrStr write SetSomeArrStr; default;    procedure InitData;  end; var  Form1: TForm1; implementation {$R *.lfm} var  TmpRec : TAClass2; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  TmpRec[0,S1]:=Edit1.Text;  //example usage in assignement  TmpRec[0,S2]:=Edit2.Text;  TmpRec[0,S3]:=Edit3.Text;end; procedure TForm1.Button2Click(Sender: TObject);begin  Edit4.Text:=TmpRec[0,S1];  //example usage in reading  Edit5.Text:=TmpRec[0,S2];  Edit6.Text:=TmpRec[0,S3];end; { TAClass2 } function TAClass2.GetSomeArrStr(index: integer; SNum: StrNum): string;begin  result:=fSomeArr[index].String_[SNum];end; procedure TAClass2.SetSomeArrStr(index: integer; Snum: StrNum; AValue: string);begin  fSomeArr[index].String_[SNum]:=AValue;end; procedure TAClass2.SetSomeArr(index: Integer; AValue: TSomeRecord2);begin  fSomeArr[index]:=AValue;end; function TAClass2.GetSomeArr(index: Integer): TSomeRecord2;begin  Result:=fSomeArr[index];end; procedure TAClass2.InitData;begin  fSomeArr[0].String_[S1]:='test1';  fSomeArr[0].String_[S2]:='test2';  fSomeArr[0].String_[S3]:='test3';end; 

Weitentaaal:

--- Quote from: Paolo on June 29, 2022, 11:10:22 am ---this can help ?

see attached file


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  StrNum = (S1, S2, S3);    //-- her you can add/remove other elements without change next code   TStrArr = array [StrNum] of string;   TSomeRecord2 = record    String_ : TStrArr;  end;    { TAClass2 }   TAClass2 = class  private    fSomeArr: Array[0..1] of TSomeRecord2;  private    function GetSomeArrStr(index: integer; SNum: StrNum): string;    procedure SetSomeArrStr(index: integer; Snum: StrNum; AValue: string);    procedure SetSomeArr(index: Integer; AValue: TSomeRecord2);    function GetSomeArr(index: Integer): TSomeRecord2;  public    property SomeArr [index: Integer]: TSomeRecord2 read GetSomeArr write SetSomeArr;    property SomeArrStr [index1: integer; SNum: StrNum]: string read GetSomeArrStr write SetSomeArrStr; default;    procedure InitData;  end; var  Form1: TForm1; implementation {$R *.lfm} var  TmpRec : TAClass2; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  TmpRec[0,S1]:=Edit1.Text;  //example usage in assignement  TmpRec[0,S2]:=Edit2.Text;  TmpRec[0,S3]:=Edit3.Text;end; procedure TForm1.Button2Click(Sender: TObject);begin  Edit4.Text:=TmpRec[0,S1];  //example usage in reading  Edit5.Text:=TmpRec[0,S2];  Edit6.Text:=TmpRec[0,S3];end; { TAClass2 } function TAClass2.GetSomeArrStr(index: integer; SNum: StrNum): string;begin  result:=fSomeArr[index].String_[SNum];end; procedure TAClass2.SetSomeArrStr(index: integer; Snum: StrNum; AValue: string);begin  fSomeArr[index].String_[SNum]:=AValue;end; procedure TAClass2.SetSomeArr(index: Integer; AValue: TSomeRecord2);begin  fSomeArr[index]:=AValue;end; function TAClass2.GetSomeArr(index: Integer): TSomeRecord2;begin  Result:=fSomeArr[index];end; procedure TAClass2.InitData;begin  fSomeArr[0].String_[S1]:='test1';  fSomeArr[0].String_[S2]:='test2';  fSomeArr[0].String_[S3]:='test3';end; 
--- End quote ---

very creative solution !

Thank you ! i will try this one too.

Paolo:
this also should work (not in deep tested), see attached code.



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---   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 dataend; 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.  
Here it is allowed to write what (as I understood) you want

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TmpRec[0].S1:=Edit1.Text;Edit4.Text:=TmpRec[0].S1; 
let me know if you will find bugs.

Navigation

[0] Message Index

[*] Previous page

Go to full version