Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #11  
Old 10-04-2004, 01:12 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,816
Default RE: how to make static variable in delphi?

Hi ...

And what was wrong with my reply ???

Code:
procedure Test;
const
   x: Integer = 0;
begin
   Inc(x);
   ShowMessage(IntToStr(x));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   i : Integer;
begin
   for i := 1 to 3 do Test;
end;
The const value will be held as long as the application runs ...

MvG
Peter

Objective reality is a delirium caused by lack of alcohol in blood.
24 hous in a day, 24 beers in a case. Coincidence ???
There are 10 kinds of people: Those who understand binary and those who don't
Reply With Quote
  #12  
Old 10-04-2004, 11:20 AM
phpl phpl is offline
Member
 
Join Date: May 2004
Posts: 58
Default RE: how to make static variable in delphi?

http://64.233.167.104/search?q=cache:LkDI67raleAJ:www.tietovayla.fi/borland/delphi/info/vb2del.txt+%22static+variable%22+delphi&hl=en

Text from the web page:

Likewise if you add a variable to the var section of a procedure in Delphi, you are defining a local variable. There is no equivalent to a static variable in Delphi, but a static variable is really just a module level variable which is used in only one procedure. It is a naming convenience more than anything else.
In Visual Basic, if you use the Dim keyword in the declarations section of a module or form, you are creating a module level variable. Likewise, if you place a variable in the Var clause of the Implementation section of a unit, you are defining a unit level variable (i.e. one which can only be seen from within that unit).
In Visual Basic, if you use the Global keyword in the declarations section of a module, you are defining a global variable. In Delphi, you simply put a variable in the var clause of the interface section of a unit and that variable is available to any other unit which includes the variable's unit in its uses clause. The following code segment helps to clarify:

Code:
unit MyUnit;

interface

uses
   WinProcs, WinTypes;  {these are the units used by this unit}

var
   globalInt:integer;
   globalStr:string;

implementation

var
   unitInt:Integer;
   unitStr:String;

procedure unitProc;
const
   staticInt:Integer = 0;
   staticStr:String = '';
var
   localInt:Integer;
   localStr:String;
begin
   {code goes here}
end;

end.
Reply With Quote
  #13  
Old 10-04-2004, 11:24 AM
phpl phpl is offline
Member
 
Join Date: May 2004
Posts: 58
Default RE: how to make static variable in delphi?

Also, I am not sure if this i true as I have never used delphi 8, BUT I read somewhere that:

Delphi versions have always had static methods that could be called on the class... static variables are a new introduction in Delphi 8.
Reply With Quote
  #14  
Old 10-04-2004, 04:29 PM
gretags gretags is offline
Member
 
Join Date: Dec 2003
Posts: 78
Default RE: how to make static variable in delphi?

You know whats the problem with const. you cant assign a value to it. i did not tried inc but if it does increment the value it still doesnt help(because that means the value is limited only to inc and dec).

gretags..
Reply With Quote
  #15  
Old 10-04-2004, 10:21 PM
phpl phpl is offline
Member
 
Join Date: May 2004
Posts: 58
Default RE: how to make static variable in delphi?

did you see my post below?
have you ever tried this.. puting vars within the implementation section, rather than the public/private/global....and rather than within the procedure.
I haven't done this much, but I will start experimenting to see how this all works and what it can be used for.
Code:
implementation

var
   unitInt:Integer;
   unitStr:String;    //note where Var is..

procedure
...
..
Reply With Quote
  #16  
Old 10-04-2004, 10:33 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,816
Default RE: how to make static variable in delphi?

Hi ...

And why not try my code ??? YOU CAN ASSIGN VALUES TO A CONSTANT EXPRESSION !!!
You only need to define the const to a type (in my example, an integer) and give it a default value ...

As for your lack of interest: This is my last post at this toppic, as for you don't seem to try reply's which in your eyes cannot be done ...

And the Inc function, it was just an example of a static variable (Read: FUNCTION CONST !!!) ... And if you took the time to read the Delphi Help on Inc you would have read that it has an optional parameter so isn't limited !!!

MvG
Peter

Objective reality is a delirium caused by lack of alcohol in blood.
24 hous in a day, 24 beers in a case. Coincidence ???
There are 10 kinds of people: Those who understand binary and those who don't
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 05:41 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.