Recent

Author Topic: Classes in a loop  (Read 258 times)

seany

  • Full Member
  • ***
  • Posts: 120
Classes in a loop
« on: February 02, 2023, 06:46:11 am »
Hi

I have this class defined:

Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. type
  4.     SectionNodePTR = ^NodeStruct;
  5.     NodeStruct  = packed record
  6.       rendObj   : Variant;
  7.       ID        : Integer;
  8.       Parent    : SectionNodePTR;
  9.       Children  : Array of SectionNodePTR;
  10.       end;
  11.  
  12.  
  13. markedDocumentBranch = class
  14.              
  15.   public
  16.       documentBranch             : NodeStruct;
  17.       i                          : Integer;
  18.       constructor Create();
  19.       procedure AppendNode (var ChildNodeObj : NodeStruct; var location : integer);
  20.       procedure AppendMultipleNodes (var ChildNodeObjects : Array of NodeStruct; var location : integer );
  21.  
  22.   end;  

The constructor is defined as:

Code: Pascal  [Select][+][-]
  1. constructor markedDocumentBranch.Create;
  2. begin
  3.   i := 10;
  4. end;        

All this is saved in a file TDocument.pas

Now, in unit.pas, the main program, I call:
Code: Pascal  [Select][+][-]
  1. currBranch                : TDocument.markedDocumentBranch;

This works.


Now, I want to create new TDocument.markedDocumentBranch objects over a loop.

If I write in the loop body:
Code: Pascal  [Select][+][-]
  1. currBranch = TDocument.markedDocumentBranch.Create;  

Then i get an error: Illegal Expression. No other message is given.

My goal is to create instances of the class in a loop and insert them in a dynamic array.

How can I solve this please

System : Linux glassplanet 6.1.8-1-cachyos #1 SMP PREEMPT_DYNAMIC Tue, 24 Jan 2023 10:53:24 +0000 x86_64 GNU/Linux

Lazarus 2.2.4 (rev Unknown) FPC 3.2.2 x86_64-linux-qt5
« Last Edit: February 02, 2023, 06:48:30 am by seany »
My projects, among others:

https://linktr.ee/siderealNight

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Classes in a loop
« Reply #1 on: February 02, 2023, 06:57:39 am »
Comparison doesn't create objects in Pascal like in C.

seany

  • Full Member
  • ***
  • Posts: 120
Re: Classes in a loop
« Reply #2 on: February 02, 2023, 06:57:53 am »
Replying to myself.

The Create procedure returns an address that can be assigned to a variable. So We need to do varname := class.create
This is different from declaration of a class, where we write classname = class
My projects, among others:

https://linktr.ee/siderealNight

 

TinyPortal © 2005-2018