karl Jonson 0 Posted November 19, 2020 Hi, How can I give Index variable in TExample a default value of -1 ? TExample = class Index : integer; end TIA Share this post Link to post
Lajos Juhász 293 Posted November 19, 2020 type Texample = class private findex: integer; public constructor Create; reintroduce; published property Index: integer read fIndex write fIndex default -1; end; constructor TExample.create; begin inherited; findex:=-1; end; If it's a published property you should use the default directive (when the value of the published property equals to it's default value it's not streamed to the dfm). Even if you set the default directive it's the job of the constructor to initialize the field value. Share this post Link to post
haentschman 92 Posted November 19, 2020 (edited) TExample = class private FIndex : Integer; public constructor Create; property Index: Integer read FIndex write FIndex; end; ... constructor TExample.Create; begin FIndex := 999; end; ...the same idea. Edited November 19, 2020 by haentschman Share this post Link to post
David Heffernan 2345 Posted November 19, 2020 Fields of a class are default initialised upon instantiation. That means, for an integer, a value of 0. If you want to assign a different value, you need to do that yourself, in the constructor. Share this post Link to post
Stefan Glienke 2002 Posted November 20, 2020 (edited) uses Spring; type TExample = class(TManagedObject) [Default(-1)] Index: integer; end; Just because I can 😎 Edited November 20, 2020 by Stefan Glienke Share this post Link to post
Mahdi Safsafi 225 Posted November 20, 2020 33 minutes ago, Stefan Glienke said: uses Spring; type TExample = class(TManagedObject) [Default(-1)] Index: integer; end; Just because I can 😎 IIRC Extended-Pascal had similar thing but it also applies to type(declared type can have default value as well). Share this post Link to post
Stefan Glienke 2002 Posted November 20, 2020 6 minutes ago, Mahdi Safsafi said: IIRC Extended-Pascal had similar thing but it also applies to type(declared type can have default value as well). Interesting - I think I am going to add that to the TManagedObject functionality Share this post Link to post
Attila Kovacs 629 Posted November 20, 2020 (edited) Too bad there is no multiple inheritance. Is it possible to hook a constructor on startup? Edited November 20, 2020 by Attila Kovacs Share this post Link to post
Mahdi Safsafi 225 Posted November 20, 2020 18 minutes ago, Attila Kovacs said: Too bad there is no multiple inheritance. multiple inheritance = multiple trouble Quote Is it possible to hook a constructor on startup? Yep if you install it before it gets created. Share this post Link to post
Stefan Glienke 2002 Posted November 24, 2020 No need for multiple inheritance - you can also apply the mechanism from Spring.TManagedObject to any other class - simply override NewInstance and FreeInstance Share this post Link to post