Recent

Author Topic: Initialization of array of records  (Read 429 times)

freemind001

  • New Member
  • *
  • Posts: 35
Initialization of array of records
« on: March 28, 2023, 08:47:48 pm »
I do init a record this way:
Code: Pascal  [Select][+][-]
  1. type
  2.    TMyRec = record  
  3.       Val1: integer;
  4.       Val2: string;
  5.    end;  
  6. var
  7.    MyRec: TMyRec;
  8. begin
  9.    MyRec:= Default(TMyRec);
  10.  
Init of array:
Code: Pascal  [Select][+][-]
  1. var
  2.    MyArr: array [0..2] of byte = (0, 0, 0);

How do i initialize a static array of records?
Code: Pascal  [Select][+][-]
  1. var
  2.    MyArrOfRec: array [1..2] of TMyRec = ?


« Last Edit: March 28, 2023, 08:54:06 pm by freemind001 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Initialization of array of records
« Reply #1 on: March 28, 2023, 09:02:38 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$AppType console}
  5.  
  6. const
  7.   RangeLow = 0;
  8.   RangeHigh = 19;
  9.  
  10.  
  11. type
  12.   TMyRec = record
  13.     Val1: integer;
  14.     Val2: string;
  15.   end;
  16.  
  17.   TRange = RangeLow..RangeHigh;
  18.  
  19.   TMyRecArr = Array[TRange] of TMyRec;
  20.  
  21.  
  22. var
  23.   MyRec: TMyRec;
  24.   MyRecArr: TMyRecArr;
  25.  
  26. begin
  27.   MyRec:= Default(TMyRec);
  28.   MyRecArr := Default(TMyRecArr);
  29.   WriteLn('MyRecArr[5].Val2="', MyRecArr[5].Val2,'"'#10'MyRecArr[5].Val1=', MyRecArr[5].Val1);
  30.   ReadLn;
  31. end.

freemind001

  • New Member
  • *
  • Posts: 35
Re: Initialization of array of records
« Reply #2 on: March 28, 2023, 09:13:37 pm »
exactly)
Thanx

simone

  • Hero Member
  • *****
  • Posts: 573
Re: Initialization of array of records
« Reply #3 on: March 28, 2023, 09:38:55 pm »
How do i initialize a static array of records?
Code: Pascal  [Select][+][-]
  1. var
  2.    MyArrOfRec: array [1..2] of TMyRec = ?

In a less elegant, but more compact way:

Code: Pascal  [Select][+][-]
  1. type
  2.    TMyRec = record
  3.       Val1: integer;
  4.       Val2: string;
  5.    end;
  6. var
  7.    MyArrOfRec : array [1..2] of TMyRec =((Val1:0;Val2:''),(Val1:0;Val2:''));  

Obviously this solution is practical only for small static arrays.
« Last Edit: March 28, 2023, 11:51:44 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018