| View previous topic :: View next topic |
| Author |
Message |
Res Guest
|
Posted: Sat Jul 24, 2004 2:35 pm Post subject: Declaring Variable With Default Value? |
|
|
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
|
Posted: Sat Jul 24, 2004 4:19 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
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
|
Posted: Sat Jul 24, 2004 4:40 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
"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
|
Posted: Sat Jul 24, 2004 7:08 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
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
|
Posted: Sat Jul 24, 2004 7:19 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
"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
|
Posted: Mon Jul 26, 2004 7:57 am Post subject: Re: Declaring Variable With Default Value? |
|
|
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
|
Posted: Mon Jul 26, 2004 4:23 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
"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
|
Posted: Mon Jul 26, 2004 10:05 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
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
|
Posted: Thu Jul 29, 2004 1:41 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
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
|
Posted: Thu Jul 29, 2004 1:44 pm Post subject: Re: Declaring Variable With Default Value? |
|
|
Darko Miletic wrote:
Ahh, I saw a silly typo mistake here is the correct constructor:
constructor TSomeClass.Create(Ione, Itwo, Ithree : Integer);
begin
one := Ione;
two := Itwo;
three := Ithree;
end;
Darko
|
|
| Back to top |
|
 |
|