Recent

Author Topic: Custom class adding itself to array of that class  (Read 441 times)

TomTom

  • Full Member
  • ***
  • Posts: 170
Custom class adding itself to array of that class
« on: November 03, 2022, 08:24:48 am »
Hi. I have this :
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   EAreaType = (atVillage, atTown, atCity, atDungeon, atSite, atEvent);
  4.  
  5.   { TMapArea }
  6.  
  7.   TMapArea = class
  8.     FArea : TRect;
  9.     FType : EAreaType;
  10.     public
  11.       property Area : TRect read FArea write FArea;
  12.       property AreaType : EAreaType read FType write FType;
  13.  
  14.       constructor Create(aList: array of TMapArea; aType: EAreaType; aRect: TRect);
  15.   overload;  

Code: Pascal  [Select][+][-]
  1. constructor TMapArea.Create(aList: array of TMapArea; aType: EAreaType;
  2.   aRect: TRect);
  3. begin
  4.   self.FType := aType;
  5.   self.FArea := aRect;
  6.   SetLength(aList, (Length(aList) + 1));
  7.   aList[Length(aList)]:=self;
  8. end;

I would like to add new objects of that class to be added to pointed array. This gives me type mismatch and I can't figure out why. Ofcourse I can add it later in the code but not in Create :(. I'm missing something but don't know what.


« Last Edit: November 03, 2022, 08:49:00 am by TomTom »

alpine

  • Hero Member
  • *****
  • Posts: 633
Re: Custom class adding itself to array of that class
« Reply #1 on: November 03, 2022, 08:59:55 am »
Shouldn't it be that way (-1)?
Code: Pascal  [Select][+][-]
  1. aList[Length(aList)-1]:=Self;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

bytebites

  • Hero Member
  • *****
  • Posts: 538
Re: Custom class adding itself to array of that class
« Reply #2 on: November 03, 2022, 09:04:19 am »
Code: Pascal  [Select][+][-]
  1. constructor TMapArea.Create(aList: array of TMapArea; aType: EAreaType;
  2.   aRect: TRect);

The reason to type mismatch is that aList is open array not dynamic array.
« Last Edit: November 03, 2022, 09:12:37 am by bytebites »

Zvoni

  • Hero Member
  • *****
  • Posts: 1604
Re: Custom class adding itself to array of that class
« Reply #3 on: November 03, 2022, 09:51:43 am »
Independent of the Answers above: Wouldn't "aList" be lost, once the constructor finishes?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 12619
Re: Custom class adding itself to array of that class
« Reply #4 on: November 03, 2022, 10:26:52 am »
Yes. It needs to be var (or maybe out).
Another option is to have a class constructor but that needs some internal management.
« Last Edit: November 03, 2022, 10:28:40 am by Thaddy »
The only thing I can say about Putin - born st Petersburg- is that he is indeed Russian, as opposed to Stalin, who was Georgian. Depending of historical time frame they could both be Lithuanian or Polish...even German. Shut him up!

howardpc

  • Hero Member
  • *****
  • Posts: 4104
Re: Custom class adding itself to array of that class
« Reply #5 on: November 03, 2022, 11:48:30 am »
It needs to be written thus  to compile:
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   EAreaType = (atVillage, atTown, atCity, atDungeon, atSite, atEvent);
  4.  
  5.   TMapArea = class;
  6.  
  7.   TMapAreaArray = array of TMapArea;
  8.  
  9.   TMapArea = class(TObject)
  10.   private
  11.     FArea : TRect;
  12.     FType : EAreaType;
  13.   public
  14.     constructor Create(var aList: TMapAreaArray; aType: EAreaType; aRect: TRect); //overload;
  15.     property Area : TRect read FArea write FArea;
  16.     property AreaType : EAreaType read FType write FType;
  17.   end;
  18.  
  19.   constructor TMapArea.Create(var aList: TMapAreaArray; aType: EAreaType; aRect: TRect);
  20.   begin
  21.     FType := aType;
  22.     FArea := aRect;
  23.     SetLength(aList, (Length(aList) + 1));
  24.     aList[High(aList)] := Self;
  25.   end;

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Custom class adding itself to array of that class
« Reply #6 on: November 03, 2022, 08:08:52 pm »
Thank You howrdpc :).

 

TinyPortal © 2005-2018