1. Computing

Understanding Typed Constants in Delphi

How to implement persistent values between function calls.

By

When Delphi invokes an event handler, the old values of local variables are wiped out. What if we want to keep track of how many times a button has been clicked? We could have the values persist by using a unit-level variable, but it is generally a good idea to reserve unit-level variables only for sharing information. What we need are usually called static variables or typed constants in Delphi.

Variable or constant?

Typed constants can be compared to initialized variables-variables whose values are defined on entry to their block (usually event handler). Such a variable is initialized only when the program starts running. After that, the value of a typed constant persists between successive calls to their procedures.

Using typed constants is a very clean way of implementing automatically initialized variables. To implement these variables without typed constants, we'll need to create an initialization section that sets the value of each initialized variable.

Variable typed constants

Although we declare typed constants in the const section of a procedure, it is important to remember that they are not constants. At any point in your application, if you have access to the identifier for a typed constant you'll be able to modify its value.

To see typed constants at work, put a button on a blank form, and assign the following code to the OnClick event handler:

 procedure TForm1.Button1Click(Sender: TObject) ;
 const
    clicks : Integer = 1; //not a true constant
 begin
   Form1.Caption := IntToStr(clicks) ;
   clicks := clicks + 1;
 end;
 
Notice that every time you click on the button, forms caption increments steadily.
Now try the following code:
 procedure TForm1.Button1Click(Sender: TObject) ;
 var
    clicks : Integer;
 begin
   Form1.Caption := IntToStr(clicks) ;
   clicks := clicks + 1;
 end;
 
We are now using uninitialized variable for the clicks counter. Notice that weird value in the forms caption after you click on the button.

Constant typed constants

You have to agree that idea of modifiable constants sound a bit strange. In 32 bit versions of Delphi Borland decided to discourage their use, but support them for Delphi 1 legacy code.

We can enable or disable Assignable typed constants on the Compiler page of the Project Options dialog box.

If you've disabled Assignable typed constants for a given project, when you attempt to compile previous code Delphi will give you 'Left side cannot be assigned to' error upon compilation. You can, however, create assignable typed constant by declaring:

 {$J+}
 const clicks : Integer = 1; 
 {$J-}
 
Therefore, the first example code looks like:
 procedure TForm1.Button1Click(Sender: TObject) ;
 const
 {$J+}
    clicks : Integer = 1; //not a true constant
 {$J-}
 begin
   Form1.Caption := IntToStr(clicks) ;
   clicks := clicks + 1;
 end;
 

Conclusion

It's up to you to decide whether you want typed constants to be assignable or not. Important thing here is that besides ideal for counters, typed constants are ideal for making components alternately visible or invisible, or we can use them for switching between any Boolean properties. Typed constants can also be used inside TTimer's event handler to keep track of how many times even has been triggered.
If you want some more beginners material check the rest of the Delphi For Beginners programming topics.

©2014 About.com. All rights reserved.