Re: [fpc-pascal] Help with TList example

2020-09-08 Thread James Richters via fpc-pascal
Thank you for the help, I'll give it a try!

James

-Original Message-
From: fpc-pascal  On Behalf Of Jean 
SUZINEAU via fpc-pascal
Sent: Tuesday, September 8, 2020 8:47 AM
To: fpc-pascal@lists.freepascal.org
Cc: Jean SUZINEAU 
Subject: Re: [fpc-pascal] Help with TList example

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

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


Re: [fpc-pascal] Help with TList example

2020-09-08 Thread Ryan Joseph via fpc-pascal


> On Sep 8, 2020, at 6:10 PM, James Richters via fpc-pascal 
>  wrote:
> 
> I'm trying to figure out how TList works.  I found the code example below by 
> doing a search, but I can't compile it,  I get Error: Illegal qualifier on 
> the line  

Do you want an array of pointers (objects allocated on the stack) or an array 
of records (i.e. one continuous block of memory? I you want an array of 
pointers try using classes as mentioned or if you want an array of records try 
TFPGList and specialize for your record type.

Regards,
Ryan Joseph

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


Re: [fpc-pascal] Help with TList example

2020-09-08 Thread Jean SUZINEAU via fpc-pascal

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


Re: [fpc-pascal] Help with TList example

2020-09-08 Thread Tony Whyman via fpc-pascal

See https://www.freepascal.org/docs-html/rtl/system/dispose.html

On 08/09/2020 12:51, James Richters wrote:

Can you please give me an example of the correct way to use new and dispose?

I'll try the pointer

Thanks for the advice

James

-Original Message-
From: fpc-pascal  On Behalf Of Tony 
Whyman via fpc-pascal
Sent: Tuesday, September 8, 2020 7:21 AM
To: fpc-pascal@lists.freepascal.org
Cc: Tony Whyman 
Subject: Re: [fpc-pascal] Help with TList example

Two observations:

1. In Pascal you should use "new" and "dispose" to allocate and deallocate 
record types - not GetMem and FreeMem.

2. MyRec is a pointer type and you should code the line as

MyRec^.Value := tmp


On 08/09/2020 12:10, James Richters via fpc-pascal wrote:

I'm trying to figure out how TList works.  I found the code example below by 
doing a search, but I can't compile it,  I get Error: Illegal qualifier on the 
line
  MyRec.Value := tmp;
It's indicating the error is on the V of Value

I tried commenting that line out, then I get the same error on
  MyRec.AByte := Byte(tmp);
At the A of AByte

So I commented that out too and then I get the error on

 Writeln('Value: ', MyRecList[tmp].Value, ' AByte: ',
MyRecList[tmp].AByte); At the V on Value after MyRecList[tmp].

I don't know enough about the syntax to figure out what's wrong here.  Does 
anyone have any ideas?  It seems like there is something fundamentally wrong.

I'm trying to make a temporary list of records.  Kind of like a TStringList, 
but instead of a list of strings, a list of my own custom records.  Perhaps 
there is a better way?


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

uses
SysUtils, Classes;

type
PMyRec=^TMyRec;
TMyRec=record
  Value: Integer;
  AByte: Byte;
end;

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

{ TMyRecList }

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

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

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

var
MyRecList: TMyRecList;
MyRec: pMyRec;
tmp: Integer;
begin
MyRecList := TMyRecList.Create;
for tmp := 0 to 9 do
begin
  GetMem(MyRec, SizeOf(TMyRec));
  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.

James

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


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



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


Re: [fpc-pascal] Help with TList example

2020-09-08 Thread James Richters via fpc-pascal
Can you please give me an example of the correct way to use new and dispose?

I'll try the pointer

Thanks for the advice

James

-Original Message-
From: fpc-pascal  On Behalf Of Tony 
Whyman via fpc-pascal
Sent: Tuesday, September 8, 2020 7:21 AM
To: fpc-pascal@lists.freepascal.org
Cc: Tony Whyman 
Subject: Re: [fpc-pascal] Help with TList example

Two observations:

1. In Pascal you should use "new" and "dispose" to allocate and deallocate 
record types - not GetMem and FreeMem.

2. MyRec is a pointer type and you should code the line as

MyRec^.Value := tmp


On 08/09/2020 12:10, James Richters via fpc-pascal wrote:
> I'm trying to figure out how TList works.  I found the code example below by 
> doing a search, but I can't compile it,  I get Error: Illegal qualifier on 
> the line
>  MyRec.Value := tmp;
>It's indicating the error is on the V of Value
>
> I tried commenting that line out, then I get the same error on
>  MyRec.AByte := Byte(tmp);
> At the A of AByte
>
> So I commented that out too and then I get the error on
>
> Writeln('Value: ', MyRecList[tmp].Value, ' AByte: ', 
> MyRecList[tmp].AByte); At the V on Value after MyRecList[tmp].
>
> I don't know enough about the syntax to figure out what's wrong here.  Does 
> anyone have any ideas?  It seems like there is something fundamentally wrong.
>
> I'm trying to make a temporary list of records.  Kind of like a TStringList, 
> but instead of a list of strings, a list of my own custom records.  Perhaps 
> there is a better way?
>
>
> program Project1;
> {$mode objfpc}{$H+}
>
> uses
>SysUtils, Classes;
>
> type
>PMyRec=^TMyRec;
>TMyRec=record
>  Value: Integer;
>  AByte: Byte;
>end;
>
>TMyRecList=class(TList)
>private
>  function Get(Index: Integer): PMyRec;
>public
>  destructor Destroy; override;
>  function Add(Value: PMyRec): Integer;
>  property Items[Index: Integer]: PMyRec read Get; default;
>end;
>
> { TMyRecList }
>
> function TMyRecList.Add(Value: PMyRec): Integer; begin
>Result := inherited Add(Value);
> end;
>
> destructor TMyRecList.Destroy;
> var
>i: Integer;
> begin
>for i := 0 to Count - 1 do
>  FreeMem(Items[i]);
>inherited;
> end;
>
> function TMyRecList.Get(Index: Integer): PMyRec; begin
>Result := PMyRec(inherited Get(Index)); end;
>
> var
>MyRecList: TMyRecList;
>MyRec: pMyRec;
>tmp: Integer;
> begin
>MyRecList := TMyRecList.Create;
>for tmp := 0 to 9 do
>begin
>  GetMem(MyRec, SizeOf(TMyRec));
>  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.
>
> James
>
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org 
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org 
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

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


Re: [fpc-pascal] Help with TList example

2020-09-08 Thread Tony Whyman via fpc-pascal

Two observations:

1. In Pascal you should use "new" and "dispose" to allocate and 
deallocate record types - not GetMem and FreeMem.


2. MyRec is a pointer type and you should code the line as

MyRec^.Value := tmp


On 08/09/2020 12:10, James Richters via fpc-pascal wrote:

I'm trying to figure out how TList works.  I found the code example below by 
doing a search, but I can't compile it,  I get Error: Illegal qualifier on the 
line
 MyRec.Value := tmp;
   It's indicating the error is on the V of Value

I tried commenting that line out, then I get the same error on
 MyRec.AByte := Byte(tmp);
At the A of AByte

So I commented that out too and then I get the error on

Writeln('Value: ', MyRecList[tmp].Value, ' AByte: ', MyRecList[tmp].AByte);
At the V on Value after MyRecList[tmp].

I don't know enough about the syntax to figure out what's wrong here.  Does 
anyone have any ideas?  It seems like there is something fundamentally wrong.

I'm trying to make a temporary list of records.  Kind of like a TStringList, 
but instead of a list of strings, a list of my own custom records.  Perhaps 
there is a better way?


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

uses
   SysUtils, Classes;

type
   PMyRec=^TMyRec;
   TMyRec=record
 Value: Integer;
 AByte: Byte;
   end;

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

{ TMyRecList }

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

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

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

var
   MyRecList: TMyRecList;
   MyRec: pMyRec;
   tmp: Integer;
begin
   MyRecList := TMyRecList.Create;
   for tmp := 0 to 9 do
   begin
 GetMem(MyRec, SizeOf(TMyRec));
 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.

James

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


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


[fpc-pascal] Help with TList example

2020-09-08 Thread James Richters via fpc-pascal
I'm trying to figure out how TList works.  I found the code example below by 
doing a search, but I can't compile it,  I get Error: Illegal qualifier on the 
line  
MyRec.Value := tmp;
  It's indicating the error is on the V of Value

I tried commenting that line out, then I get the same error on
MyRec.AByte := Byte(tmp);
At the A of AByte

So I commented that out too and then I get the error on 

   Writeln('Value: ', MyRecList[tmp].Value, ' AByte: ', MyRecList[tmp].AByte); 
At the V on Value after MyRecList[tmp].

I don't know enough about the syntax to figure out what's wrong here.  Does 
anyone have any ideas?  It seems like there is something fundamentally wrong.

I'm trying to make a temporary list of records.  Kind of like a TStringList, 
but instead of a list of strings, a list of my own custom records.  Perhaps 
there is a better way?


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

uses
  SysUtils, Classes;

type
  PMyRec=^TMyRec;
  TMyRec=record
Value: Integer;
AByte: Byte;
  end;

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

{ TMyRecList }

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

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

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

var
  MyRecList: TMyRecList;
  MyRec: pMyRec;
  tmp: Integer;
begin
  MyRecList := TMyRecList.Create;
  for tmp := 0 to 9 do
  begin
GetMem(MyRec, SizeOf(TMyRec));
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.

James

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