Delphi assign const?

this site: http://www.drbob42.com/delphi/wizards.htm

showed a very cryptic code below

 unit ShareMem;
 { (c) 1997 by Bob Swart (aka Dr.Bob - http://www.drbob42.com }
 interface

 const
...
 uses
   Windows;

 const
   Handle: THandle = 0;
...
 function GetCommandLine: PChar; stdcall;
   external 'kernel32.dll' name 'GetCommandLineA';
...
   begin
     Handle := LoadLibrary('BCBMM.DLL');
 end.

how could this be?

+3
source share
3 answers

Printed constby default (Edit: as Rob noted in the comments, this has been changed to no longer be the default years ago), more like a static variable. You can disable this behavior using the compiler directive .

This was commonly used as a replacement for class / static properties in older versions of Delphi. Now that Delphi has this feature, there is no reason for this IMHO.

+4
source

Delphi - , const. / . . .

, . ​​ , .

procedure Test;
{$WRITEABLECONST ON}
const
  AssignableConst: Integer = 0;
{$WRITEABLECONST OFF}
begin
  AssignableConst := AssignableConst + 1; 
  WriteLn('Test is called ' + IntToStr(AssignableConst) + ' times'); 
end;
+6

, . Turbo Pascal. , . , DATA ( Lars Truijens). , , , . Delphi 4 ( 3 ?) Borland , $WRITEABLECONSTswitch (disabled by default). Initialized variables cannot be displayed in the local area, so there is still room for writing typed constants.

+1
source

Source: https://habr.com/ru/post/1778112/


All Articles