Solved

Static variable inside procedure

Posted on 1998-12-25
10
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 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