?
Solved

Static variable inside procedure

Posted on 1998-12-25
10
Medium Priority
?
183 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
Independent Software Vendors: 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 120 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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