Static Variable in Delphi...
One time I was discussing with a .Net programmer about static variables. Then I tried to declared a static variable in Delphi but I could not. Because there are no keyword 'Static' in Delphi. So when I checked how to declare a Static variable in Delphi, I found the 'Typed Constant' which acts as static variable in Delphi.
By default Delphi doesn't allow static variables, it only allows static methods, functions and properties.
But we can use typed constant to declare a static variable in Delphi. That will work as like static variable in C# that keep their value between routine calls.
But this facility was available in Delphi 16 bit version i.e in Delphi 1 which is disappeared in Delphi 32 version. But for backward compatibility still Delphi has an option 'Assignable Typed Constant' option in compiler tab of project option.
Project Menu -> Option -> Compiler Tab -> Assignable Typed Constant
You need to check this option for use such facility else you will get following error.
"[Error] Unit1.pas(30): Left side cannot be assigned to "
How to use..?
Procedure TForm1.Button1Click(Sender: TObject);
const
Counter: integer = 0;
begin
Counter := Counter + 1;
Showmessage(intostr(Counter));
end;
Or
Procedure TForm1.Button1Click(Sender: TObject) ;
const
{$J+}
clicks : Integer = 1; //not a true constant
{$J-}
begin
Form1.Caption := IntToStr(clicks) ;
clicks := clicks + 1;
end;
For use like 2nd part you have to user {$J+}. At staring of program 1 will be assigned to clicks. Then for rest it will be a integer parameter and value will maintain through out the program.
This technique can be used for count number of instances of a class, for count how many number of time an event is fired.
By default Delphi doesn't allow static variables, it only allows static methods, functions and properties.
But we can use typed constant to declare a static variable in Delphi. That will work as like static variable in C# that keep their value between routine calls.
But this facility was available in Delphi 16 bit version i.e in Delphi 1 which is disappeared in Delphi 32 version. But for backward compatibility still Delphi has an option 'Assignable Typed Constant' option in compiler tab of project option.
Project Menu -> Option -> Compiler Tab -> Assignable Typed Constant
You need to check this option for use such facility else you will get following error.
"[Error] Unit1.pas(30): Left side cannot be assigned to "
How to use..?
Procedure TForm1.Button1Click(Sender: TObject);
const
Counter: integer = 0;
begin
Counter := Counter + 1;
Showmessage(intostr(Counter));
end;
Or
Procedure TForm1.Button1Click(Sender: TObject) ;
const
{$J+}
clicks : Integer = 1; //not a true constant
{$J-}
begin
Form1.Caption := IntToStr(clicks) ;
clicks := clicks + 1;
end;
For use like 2nd part you have to user {$J+}. At staring of program 1 will be assigned to clicks. Then for rest it will be a integer parameter and value will maintain through out the program.
This technique can be used for count number of instances of a class, for count how many number of time an event is fired.
Comments
Post a Comment