Solved

Static variable inside procedure

Posted on 1998-12-25
10
176 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 263
Multiple image collision 13 69
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 57
Firemonkey webbrowser scrollbars ? 1 29
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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now