BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Declaring Variable With Default Value?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical
View previous topic :: View next topic  
Author Message
Res
Guest





PostPosted: Sat Jul 24, 2004 2:35 pm    Post subject: Declaring Variable With Default Value? Reply with quote



Does anyone know if its possible to declare variables but specify a default
value for them at the declaration stage instead of having to assign them
values in the FormCreate event or whatever?

[GOT]
public
{ Public declarations }
test1, test2: integer;

[WANT]
public
{ Public declarations }
test1 = 0, test2 = 1: integer;

TIA


Back to top
Darko Miletic
Guest





PostPosted: Sat Jul 24, 2004 4:19 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote



On Sat, 24 Jul 2004 15:35:37 +0100, "Res" <res (AT) totalise (DOT) co.uk> wrote:

Quote:
Does anyone know if its possible to declare variables but specify a default
value for them at the declaration stage instead of having to assign them
values in the FormCreate event or whatever?

[GOT]
public
{ Public declarations }
test1, test2: integer;

[WANT]
public
{ Public declarations }
test1 = 0, test2 = 1: integer;

No. As Microsoft often says "This behaviour is by design". You can not
assign default value to any field in class definition. You can
preinitialize only global consts and variables. Like this

unit something;

interface

var
aa : Cardinal = 22;
bb: String = 'ABC';

implementation

end.

Only thing you can do is to initialize values preferably in class
constructor.

Darko

Back to top
John Herbster
Guest





PostPosted: Sat Jul 24, 2004 4:40 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote




"Darko Miletic" <kiklop (AT) fibertel (DOT) com.ar> wrote

Quote:
Only thing you can do is to initialize values
preferably in class constructor.

Darko, Do you know ...
How can the constructor fetch and initialize properties with
their corresponding default property values so that the value
does not have to be duplicated in the code?
Regards, JohnH

Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sat Jul 24, 2004 7:08 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote

John Herbster wrote:
Quote:
How can the constructor fetch and initialize properties with
their corresponding default property values so that the value
does not have to be duplicated in the code?

Is this what you mean?

type
TForm1 = class(TForm)
Label1: TLabel;
private
FFoo: integer;
public
constructor Create(AOwner: TComponent); override;
published
property Foo: integer read FFoo write FFoo default 99;
end;

[...]

uses TypInfo;

constructor TForm1.Create(AOwner: TComponent);
var pi: PPropInfo;
begin
inherited;
pi := GetPropInfo(self, 'Foo');
Label1.Caption := IntToStr(pi.Default);
end;

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"The moment the idea is admitted into society that property is not as
sacred as the laws of God and there is not a force of law and public
justice to protect it, anarchy and tyranny commence." - John Adams



Back to top
John Herbster
Guest





PostPosted: Sat Jul 24, 2004 7:19 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote


"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> wrote:

Quote:
How can the constructor fetch and initialize properties with
their corresponding default property values so that the value
does not have to be duplicated in the code?

Is this what you mean?

type
...
published
property Foo: integer read FFoo write FFoo default 99;
...

uses TypInfo;

constructor TForm1.Create(AOwner: TComponent);
var pi: PPropInfo;
begin
inherited;
pi := GetPropInfo(self, 'Foo');
Label1.Caption := IntToStr(pi.Default);
end;

Yes. Thanks Wayne. I am sorry that it requires RTTI, but
better there than nowhere. Regards, JohnH

Back to top
Alisdair Meredith (TeamB)
Guest





PostPosted: Mon Jul 26, 2004 7:57 am    Post subject: Re: Declaring Variable With Default Value? Reply with quote

Wayne Niddery [TeamB] wrote:

Quote:
Is this what you mean?

type
TForm1 = class(TForm)
Label1: TLabel;
private
FFoo: integer;
public
constructor Create(AOwner: TComponent); override;
published
property Foo: integer read FFoo write FFoo default 99;
end;

[...]

uses TypInfo;

constructor TForm1.Create(AOwner: TComponent);
var pi: PPropInfo;
begin
inherited;
pi := GetPropInfo(self, 'Foo');
Label1.Caption := IntToStr(pi.Default);
end;

I haev no experience with Delphi8/.NET, but would something similar be
possible for regular fields, by using custom attributes (such as
[Default Value = xxx])?

AlisdairM

Back to top
Kirk Halgren
Guest





PostPosted: Mon Jul 26, 2004 4:23 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote

"Res" <res (AT) totalise (DOT) co.uk> wrote

Quote:
Does anyone know if its possible to declare variables but specify a
default
value for them at the declaration stage instead of having to assign them
values in the FormCreate event or whatever?

[GOT]
public
{ Public declarations }
test1, test2: integer;

[WANT]
public
{ Public declarations }
test1 = 0, test2 = 1: integer;

Can't {$WRITEABLECONST ON} do this? It's a misnomer of course, and there
are other reasons to avoid it, but won't this do what you ask?

Kirk Halgren

"Any fool can write code that a computer can understand. Good programmers
write code that humans can understand."
-- Martin Fowler



Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Mon Jul 26, 2004 10:05 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote

Alisdair Meredith (TeamB) wrote:
Quote:

I haev no experience with Delphi8/.NET, but would something similar be
possible for regular fields, by using custom attributes (such as
[Default Value = xxx])?

Using attributes would work.

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"The two most abundant elements in the universe are hydrogen and
stupidity." - Harlan Ellison



Back to top
Darko Miletic
Guest





PostPosted: Thu Jul 29, 2004 1:41 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote

John Herbster wrote:

Quote:
Darko, Do you know ...
How can the constructor fetch and initialize properties with
their corresponding default property values so that the value
does not have to be duplicated in the code?
Regards, JohnH

Sorry for the delay but I was occupied with some other things.

Well you can use this simple trick with default values in constructor
like this:

unit test;

unit Unit2;

interface

type
TSomeClass = class(TObject)
private
one, two, three : Integer;
public
constructor Create( Ione : Integer = 1;
Itwo : Integer = 2;
Ithree : Integer = 3 );
end;

implementation

constructor TSomeClass.Create(Ione, Itwo, Ithree : Integer);
begin
one := Ione;
Itwo := Itwo;
Ithree := Ithree;
end;

end.

And you just call constructor without parameters like this:

var
somecl : TSomeClass;

somecl := TsomeClass.Create;


Darko

Back to top
Darko Miletic
Guest





PostPosted: Thu Jul 29, 2004 1:44 pm    Post subject: Re: Declaring Variable With Default Value? Reply with quote

Darko Miletic wrote:

Ahh, I saw a silly typo mistake Wink here is the correct constructor:

constructor TSomeClass.Create(Ione, Itwo, Ithree : Integer);
begin
one := Ione;
two := Itwo;
three := Ithree;
end;

Darko
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.