Jump to content
karl Jonson

Default value

Recommended Posts

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
TExample = class  
private
  FIndex : Integer;
public
  constructor Create;
  property Index: Integer read FIndex write FIndex;
end;
...

constructor TExample.Create;
begin
  FIndex := 999;
end;

:classic_cool:

 

...the same idea. :classic_tongue:

Edited by haentschman

Share this post


Link to post

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
uses
  Spring;

type
  TExample = class(TManagedObject)
    [Default(-1)]
    Index: integer;
  end;

Just because I can 😎

Edited by Stefan Glienke

Share this post


Link to post
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
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 :classic_cool:

Share this post


Link to post

Too bad there is no multiple inheritance.

 

Is it possible to hook a constructor on startup?

Edited by Attila Kovacs

Share this post


Link to post
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

No need for multiple inheritance - you can also apply the mechanism from Spring.TManagedObject to any other class - simply override NewInstance and FreeInstance :classic_cool:

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×