Thursday, March 12, 2020

Understanding Typed Constants in Delphi

Understanding Typed Constants in Delphi 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, well 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 youll 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 an 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 sounds 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 youve 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 Its up to you to decide whether you want typed constants to be assignable or not. The 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 TTimers event handler to keep track of how many times even has been triggered.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.