Another way is to declare TMyRec as class instead of record.

In this case, you don't need the  dereference operator ^ .

No need of new and dispose, just Create and Free, as for TMyRecList.

I don't know if there is a better performance when you use a record,
but as far as I know, record/object/new/dispose is just the old syntax for the first object oriented programming born with Turbo Pascal 5.5 around 1989.

The new syntax with class / constructor Create / destructor Free is born Delphi 1 around 1995.

Below is your code modified with TMyRec=class

==========================

program Project1;
{$mode objfpc}{$H+}

uses
  SysUtils, Classes;

type
  TMyRec=class
    Value: Integer;
    AByte: Byte;
  end;

  TMyRecList=class(TList)
  private
    function Get(Index: Integer): TMyRec;
  public
    destructor Destroy; override;
    function Add(Value: TMyRec): Integer;
    property Items[Index: Integer]: TMyRec read Get; default;
  end;

{ TMyRecList }

function TMyRecList.Add(Value: TMyRec): Integer;
begin
  Result := inherited Add(Value);
end;

destructor TMyRecList.Destroy;
var
  i: Integer;
begin
  for i := 0 to Count - 1 do
    Items[i].Free;
  inherited;
end;

function TMyRecList.Get(Index: Integer): TMyRec;
begin
  Result := TMyRec(inherited Get(Index));
end;

var
  MyRecList: TMyRecList;
  MyRec: TMyRec;
  tmp: Integer;
begin
  MyRecList := TMyRecList.Create;
  for tmp := 0 to 9 do
  begin
    MyRec:= TMyRec.Create;
    MyRec.Value := tmp;
    MyRec.AByte := Byte(tmp);
    MyRecList.Add(MyRec);
  end;

  for tmp := 0 to MyRecList.Count - 1 do
    Writeln('Value: ', MyRecList[tmp].Value, ' AByte: ', MyRecList[tmp].AByte);
  WriteLn('  Press Enter to free the list');
  ReadLn;
  MyRecList.Free;
end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to