Solved

Static variable inside procedure

Posted on 1998-12-25
10
177 Views
Last Modified: 2010-04-04
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;
0
Comment
Question by:qwertyuiopasdfghjkl
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1353051
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1353052
Well, I answered this question and it is still in the Unlocked section?!?!?! What's wrong??
0
 
LVL 1

Expert Comment

by:slautin
ID: 1353053
use global variable or define new class (Trigger for example)
and get your counter every time!
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353054
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1353055
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1353056
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
 

Author Comment

by:qwertyuiopasdfghjkl
ID: 1353057

thank Raymond......now post it as answer...i will Accept it...
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1353058
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
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 30 total points
ID: 1353059
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1353060
i mean feature... ;-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question