Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EmptyStr - var vs. constant

47 views
Skip to first unread message

David Saracini

unread,
Jan 24, 2002, 6:24:58 PM1/24/02
to
Hello All,

I often use EmptyStr instead of '' in my code because I find it more
readable (and I guess I just like to type <g>). I always assumed that it
was a constant value (I know, I know... never assume). But, I discovered
today that it's actually a global var in SysUtils.pas. Why is this a var
instead of a constant? Wouldn't the code the compiler generates be more
efficient if it was a constant?

Thanks and Best Regards,

David Saracini


John Herbster

unread,
Jan 25, 2002, 9:14:00 AM1/25/02
to
"David Saracini" <David.S...@SelectronTechnologies.com> wrote
> I often use EmptyStr instead of '' in my code ... I always assumed
> that it was a constant value ... But, I discovered today that it's

> actually a global var in SysUtils.pas. Why is this a var instead of
> a constant? ...

David, In D5ud1, the following use of EmptyStr compiles and runs.

procedure TForm1.Button1Click(Sender: TObject);
begin
EmptyStr := 'Hello World';
Edit1.Text := EmptyStr;
end;

Becides in SysUtils, "EmptyStr" also is referenced in VCL\DdeMan
and VCL\Outline.

Also interesting, is that the following compiles and runs.

const HW: string = 'Hi World';

procedure TForm1.Button2Click(Sender: TObject);
begin
NullStr := @HW;
Edit1.Text := NullStr^;
end;

I wonder who programmed EmptyStr and NullStr. Rgds, JohnH

David Saracini

unread,
Jan 25, 2002, 10:41:27 AM1/25/02
to
Hello John...

> I wonder who programmed EmptyStr and NullStr. Rgds, JohnH

Exactly!! I was shocked when I realized it was a variable and not a
constant... I mean, if you wanted to be devilish to someone, you could just
change this var and if they were using it, they would have a hell of a time
figuring out why their code was not working!

So, I thought, well there much be a reason. Hence the post. But, maybe it
was just poor coding. :(

Best Regards,

David

John Herbster

unread,
Jan 25, 2002, 10:44:28 AM1/25/02
to
"David Saracini" <David.S...@SelectronTechnologies.com> wrote
> I often use EmptyStr instead of '' in my code because ...

David, Check following references out. Too bad that they could
not use {$J-} or {$WRITEABLECONST OFF}. Rgds, JohnH

http://groups.google.com/groups?q=Delphi+EmptyStr+NullStr&hl=en

Manuel Algora

unread,
Jan 25, 2002, 4:51:37 PM1/25/02
to
On Fri, 25 Jan 2002 09:41:27 -0600, "David Saracini"
<David.S...@SelectronTechnologies.com> wrote:

>I was shocked when I realized it was a variable and not a
>constant... I mean, if you wanted to be devilish to someone, you could just
>change this var and if they were using it, they would have a hell of a time
>figuring out why their code was not working!

May be those who wrote Delphi had a good reason to make it a variable,
not a constant. But then, perhaps a different approach might have been
followed. Instead of providing variables, the VCL would provide an
object exposing them as read-only properties.

Manuel Algora
cen...@wanadoo.es

Rudy Velthuis (TeamB)

unread,
Jan 26, 2002, 3:41:39 PM1/26/02
to
In article <3c5097e9$1_2@dnews>, David Saracini says...

> I often use EmptyStr instead of '' in my code because I find it more
> readable (and I guess I just like to type <g>). I always assumed that it
> was a constant value (I know, I know... never assume). But, I discovered
> today that it's actually a global var in SysUtils.pas. Why is this a var
> instead of a constant?

Good question. Probably because of backward compatibility as well. But I
can't say for sure.
--
Rudy Velthuis (TeamB)

Iain Macmillan

unread,
Jan 28, 2002, 1:40:06 PM1/28/02
to

In article <3c51682e$1_2@dnews>, "John Herbster" <jo...@petronworld.com>
wrote:

> I wonder who programmed EmptyStr and NullStr. Rgds, JohnH

I wonder *why*.
I have never used these.
I didn't realise they existed till today!

However most of my projects do contain this line
Const C_NOTHINGTEXT='';

.. I didn't really have to rack my brains to come up with this, not the sort
of thing I really need Borland to write for me... I'd rather have them write
a preprocessor. <g>

------
In article <MPG.16bd32edd...@newsgroups.borland.com>, "Rudy
Velthuis (TeamB)" <rvel...@gmx.de> wrote:

>> today that it's actually a global var in SysUtils.pas. Why is this a var
>> instead of a constant?

> Good question. Probably because of backward compatibility as well. But I


> can't say for sure.

Const NullStr:PString = @EmptyStr {sysutils}

- this would not comnpile if EmptyStr was a Const, unless it was Typed.

Rudy Velthuis (TeamB)

unread,
Jan 28, 2002, 2:33:12 PM1/28/02
to
In article <3c5597de$1_1@dnews>, Iain Macmillan says...

> > Good question. Probably because of backward compatibility as well. But I
> > can't say for sure.
> Const NullStr:PString = @EmptyStr {sysutils}
>
> - this would not comnpile if EmptyStr was a Const, unless it was Typed.

Indeed. And I think the question was why it was not a typed constant
instead of a var.
--
Rudy Velthuis (TeamB)

0 new messages