John,

The first form is a typed constant.  Delphi allocates a string variable and
assigns it the value 'This is a test'.  Depending on your compiler options
(Assignable typed constants), Delphi may or may not allow this variable to
be assigned.  When you assign this to another variable, it is done at run
time.

The second form is a simple constant, and is resolved at compile time.  It
cannot be assigned to.

example:

program Test;
{$APPTYPE CONSOLE}
{$J+}
const
  A: string = 'This is a test';
  B = 'This is a test';
var
  s: string;
begin
  s := A;
  WriteLn ('A = ', s);
  s := B;
  WriteLn ('B = ', s);
  A := 'New value';
  s := A;
  WriteLn ('A = ', s);
//  B := 'New value'; will raise compiler error
  s := B;
  WriteLn ('B = ', s);
end.

Typed constants allow variables to be created with an initial value.

Typed constants also allow for "static" variables inside functions and
procedures.  Example:

procedure DoSomething;
const
  FirstTime: Boolean = True;
begin
  if FirstTime then begin
    FirstTime := False;
    // Perform initialisation
  end;

  // Perform main task
end;

Regards,
Dennis.

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, 13 November 2001 01:30
> To: Multiple recipients of list delphi
> Subject: [DUG]: Const
>
>
> Hi all,
>
> Can anybody tell me the difference between the following two declarations
> and why you want to use the first one. The second one seems to use less
> memory!
>
>
>       const
>          sName : string = 'This is a test';
>
>
> and
>
>       const
>          sName  = 'This is a test';
>
>
>
> TIA
>
> John.
>
> ------------------------------------------------------------------
> ---------
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to