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
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
> 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
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
>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
> 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)
> 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.
> > 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)