Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
Beginners
(Moderators:
FPK
,
Tomas Hajny
) »
Custom class adding itself to array of that class
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
How to install Castle Gam...
by
shyub
[
Today
at 05:09:19 pm]
Help Needed: Cross Platfo...
by
Bogen85
[
Today
at 05:02:04 pm]
Linked list inside a clas...
by
Blaazen
[
Today
at 04:54:22 pm]
informacion sobre actuali...
by
zelda16bit
[
Today
at 04:54:02 pm]
DatePointHintTool - inval...
by
Nicole
[
Today
at 04:48:53 pm]
is there an internet conn...
by
Bogen85
[
Today
at 04:16:17 pm]
Unicode resourcestring
by
PascalDragon
[
Today
at 03:55:37 pm]
Using protected procedure...
by
PascalDragon
[
Today
at 03:48:55 pm]
Support for Git?
by
Bogen85
[
Today
at 03:44:24 pm]
Prevent computer logging ...
by
KodeZwerg
[
Today
at 03:29:18 pm]
« previous
next »
Print
Pages: [
1
]
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]
[+]
[-]
type
EAreaType
=
(
atVillage
,
atTown
,
atCity
,
atDungeon
,
atSite
,
atEvent
)
;
{ TMapArea }
TMapArea
=
class
FArea
:
TRect
;
FType
:
EAreaType
;
public
property
Area
:
TRect
read
FArea
write
FArea
;
property
AreaType
:
EAreaType
read
FType
write
FType
;
constructor
Create
(
aList
:
array
of
TMapArea
;
aType
:
EAreaType
;
aRect
:
TRect
)
;
overload
;
Code: Pascal
[Select]
[+]
[-]
constructor
TMapArea
.
Create
(
aList
:
array
of
TMapArea
;
aType
:
EAreaType
;
aRect
:
TRect
)
;
begin
self
.
FType
:
=
aType
;
self
.
FArea
:
=
aRect
;
SetLength
(
aList
,
(
Length
(
aList
)
+
1
)
)
;
aList
[
Length
(
aList
)
]
:
=
self
;
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
»
Logged
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]
[+]
[-]
aList
[
Length
(
aList
)
-
1
]
:
=
Self
;
Logged
"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]
[+]
[-]
constructor
TMapArea
.
Create
(
aList
:
array
of
TMapArea
;
aType
:
EAreaType
;
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
»
Logged
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?
Logged
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
»
Logged
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]
[+]
[-]
type
EAreaType
=
(
atVillage
,
atTown
,
atCity
,
atDungeon
,
atSite
,
atEvent
)
;
TMapArea
=
class
;
TMapAreaArray
=
array
of
TMapArea
;
TMapArea
=
class
(
TObject
)
private
FArea
:
TRect
;
FType
:
EAreaType
;
public
constructor
Create
(
var
aList
:
TMapAreaArray
;
aType
:
EAreaType
;
aRect
:
TRect
)
;
//overload;
property
Area
:
TRect
read
FArea
write
FArea
;
property
AreaType
:
EAreaType
read
FType
write
FType
;
end
;
constructor
TMapArea
.
Create
(
var
aList
:
TMapAreaArray
;
aType
:
EAreaType
;
aRect
:
TRect
)
;
begin
FType
:
=
aType
;
FArea
:
=
aRect
;
SetLength
(
aList
,
(
Length
(
aList
)
+
1
)
)
;
aList
[
High
(
aList
)
]
:
=
Self
;
end
;
Logged
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
.
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
Beginners
(Moderators:
FPK
,
Tomas Hajny
) »
Custom class adding itself to array of that class
TinyPortal
© 2005-2018