Re: static variables in Delphi

Posted by Jerry on August 14, 2007

In Reply to Can't find out how to declare variables as static in Delphi 2007 Prof posted by Warwick Weston Wright on August 12, 2007

: I've been searching my useless help files for
: instructions on how to declare a variable as Static

Static variables don't exist in Delphi. In the old 16 bit Delphi 1, you could assign values to what Borland called "typed constants", so they acted as static variables (local variables in a routine, that keep their value between calls to the routine). This feature disappeared from the 32 bit versions of Delphi.

For backward compatibility, there is an option "assignable typed constants" in the "Project Options" dialog box that you can enable to have the old Delphi 1 behaviour. By default, this option is disabled, so that attempting to compile source code like below results in the error "Left side cannot be assigned to":

procedure SomeProcedure;
  Counter: integer = 0; 
  Counter := Counter + 1;