Static variable inside procedure

how to write a static variable inside the procedure? consider the following code, every time i call this procedure it will redeclare the variable x that's what i dont want to. i want the variable to be declare at the first call and keeping value after the rest of the calls.

procedure Test;
var
  x : integer;
Begin
  inc(x) //x will be the same value every time get call. i            want x to be increase every call.
End;
qwertyuiopasdfghjklAsked:
Who is Participating?
 
rwilson032697Connect With a Mentor Commented:
Cheers Madshi!

Do it like this:

       procedure Test;
           const
             x : integer = 0;
           Begin
             inc(x) //x will be increased every call.
           End;

       Cheers,

       Raymond.
0
 
viktornetCommented:
Delphi(Pascal) is not C/C++ so you can't have a real static variable. The only way to at least try to imitate that is to use a global variable that will keep its value.

Merry Christmas!

-Viktor
--Ivanov
0
 
viktornetCommented:
Well, I answered this question and it is still in the Unlocked section?!?!?! What's wrong??
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
slautinCommented:
use global variable or define new class (Trigger for example)
and get your counter every time!
0
 
rwilson032697Commented:
Sorry Viktor - you are wrong. (Although if it was for a static member in a CLASS or METHOD,. you would be right).

Do it like this:

procedure Test;
    const
      x : integer = 0;
    Begin
      inc(x) //x will be increased every call.
    End;

Cheers,

Raymond.
0
 
MadshiCommented:
Yes, Raymond seems to be right.

I always wondered why Delphi likes something like this:
  procedure blaBla;
  const x : integer = 0;
  ...
and refuses to accept something like this:
  procedure blaBla;
  var x : integer = 0;
  ...
Now I know (thanx, Raymond) that the "const" declaration has a special meaning if used locally.

Regards... Madshi.
0
 
MadshiCommented:
Yes, Viktor, that's really strange! Why is this question still in the unlocked area???
Perhaps the XX system is getting more and more intelligent so that it now is able to detect that your answer is wrong...   :-)))
0
 
qwertyuiopasdfghjklAuthor Commented:

thank Raymond......now post it as answer...i will Accept it...
0
 
viktornetCommented:
I forgot about the fucken typed constants in Delphi.... I really hate the way that they made it 'cuz you can never be sure it's a constant... I just hate that future of Delphi...

-Viktor
0
 
viktornetCommented:
i mean feature... ;-)
0
All Courses

From novice to tech pro — start learning today.