for in ループを使おう

Delphi7以降。やっぱり便利。

program recdarray;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TMyRecord = record
    s: String;
    i: Integer;
  end;

  TMyRecords = array of TMyRecord;

procedure MyProc(Records: TMyRecords);
var
  rec: TMyRecord;
begin
  for rec in Records do
  begin
    WriteLn(Format('%s %d', [rec.s, rec.i]));
  end;
end;

var
  MyRecords: TMyRecords;
  i: Integer;
begin
  SetLength(MyRecords, 3);
  for i := Low(MyRecords) to High(MyRecords) do
  begin
    MyRecords[i].s := Chr(Ord('A') + i);
    MyRecords[i].i := i;
  end;
  MyProc(MyRecords);
end.

出力

>recdarray.exe
A 0
B 1
C 2

そういえば、動的配列を引数に使う場合は、array of..を使って動的配列の型を定義しておかないと使えない。