Board index » delphi » Are there static variables in Delphi functions?

Are there static variables in Delphi functions?

I've looked in the online help and can't seem to find references to static
variables
within functions/procedures.  Do these exist, and if not, how do folks get
around this?

-darrel

 

Re:Are there static variables in Delphi functions?


I'd say:
normal, module-global variables, declared in the implementation part.

greetings,
Peter Tiemann
Delphi Tips and Tricks: http://www.preview.org/e/help.shtml
Check out the localizing component TMultiLang.. http://www.preview.org/

Quote
Darrel Davis wrote in message <63gf69$...@news2.his.com>...
>I've looked in the online help and can't seem to find references to static
>variables
>within functions/procedures.  Do these exist, and if not, how do folks get
>around this?

>-darrel

Re:Are there static variables in Delphi functions?


In <63gf69$...@news2.his.com>, "Darrel Davis" <darr...@his.com> writes:

Quote
>I've looked in the online help and can't seem to find references to static
>variables
>within functions/procedures.  Do these exist, and if not, how do folks get
>around this?

>-darrel

You can use typed constants (a misnomer, they're really variables).

Also a var at the unit level (either interface or implementation section)
is retained over the whole program.  It can be initialized in the unit level
begin-end block.

Re:Are there static variables in Delphi functions?


On Sat, 1 Nov 1997 18:48:44 -0500, "Darrel Davis" <darr...@his.com>
wrote:

Quote
>I've looked in the online help and can't seem to find references to static
>variables
>within functions/procedures.  Do these exist, and if not, how do folks get
>around this?

There are two ways:

Typed constants are implemented as initialized static variables.  With
the right compiler option (it used to be the default) you can modify
them and treat them exactly like static variables.  This is a kluge,
and Borland appears to be moving away from supporting it (hence the
compiler option).  

The more standard Pascal method is to use a variable external to the
procedure.  This is visible to other routines as well, so it's not a
local static variable.

It would be nice if there were a way to say that any variable should
be static, but there isn't.

Duncan Murdoch

Re:Are there static variables in Delphi functions?


Quote
Peter Tiemann wrote:

> I'd say:
> normal, module-global variables, declared in the implementation part.

Oh, good. I didn't miss anything. This brings about another question:
Why, then, are there class functions and procedures (note that "static"
means something else in Object Pascal)? Couldn't the same thing be
accomplished with normal, module-global functions and procedures, or am
I missing something?

Dave

Re:Are there static variables in Delphi functions?


Quote
Dave Shapiro wrote in message <345DF8C8.3EDA9...@{*word*104}-fx.com>...

[..]

Quote
>Why, then, are there class functions and procedures (note that "static"
>means something else in Object Pascal)? Couldn't the same thing be
>accomplished with normal, module-global functions and procedures, or am
>I missing something?

hmm.. the code for functions/ procedures is compiled only one time for each
instance of the class.
They are somewhere in the code segment. From that point there's not
difference to normal functions.
Class functions are listed in the method table of an object. Each instance
does have a copy. This table contains pointers to the classes member
functions. This is how polymorphism can work.

As I understand, all instances of a class share ONE instance of that static
e.g. integer variable.. so why not just take a global.
I suggested a module-global to have some information hiding.

BTW, what do you want to use that static for?
The advantage is, that - since it is in the data segment and not on the heap
(member variables) or stack (local variables), accessing it is slightly
faster.
The disadvantage is - .. hm.. IMHO.. it's bad style.
I personally try to avoid statics; they can cause trouble - but sometimes
they are definitively needed.

greetings,
Peter Tiemann
Delphi Tips and Tricks: http://www.preview.org/e/help.shtml

Re:Are there static variables in Delphi functions?


Quote
Peter Tiemann wrote:
> Class functions are listed in the method table of an object. Each instance
> does have a copy. This table contains pointers to the classes member
> functions. This is how polymorphism can work.

Gotcha. I just haven't had use for a virtual class function. An example
of one would be great. Any situations where this has come up?

Quote
> BTW, what do you want to use that static for?
> The advantage is, that - since it is in the data segment and not on the heap
> (member variables) or stack (local variables), accessing it is slightly
> faster.
> The disadvantage is - .. hm.. IMHO.. it's bad style.
> I personally try to avoid statics; they can cause trouble - but sometimes
> they are definitively needed.

I don't see why a static variable would be worse style than a
module-global. It's *better* style, I think, because it encapsulates
things a little better, e.g. not only is the variable accessible only
within the module, but it belongs to a class. It's semantics, really.

cheers,

Dave

Re:Are there static variables in Delphi functions?


Quote
Dave Shapiro wrote:
> Why, then, are there class functions and procedures (note that "static"
> means something else in Object Pascal)? Couldn't the same thing be
> accomplished with normal, module-global functions and procedures, or am
> I missing something?

Well, as has been pointed out, class methods can be virtual and can be
overridden, which is pretty significant. Also, they get a "class of"
pointer, and can so instantiate objects of their class, ala Create.

--

Personal Pages              http://www.midnightbeach.com/jon
Programming Publications    http://www.midnightbeach.com/jon/pubs
Homeschool Resource Page    http://www.midnightbeach.com/hs

Re:Are there static variables in Delphi functions?


Dave,

Quote
>I don't see why a static variable would be worse style than a
>module-global. It's *better* style, I think, because it encapsulates

yes, that's what I meant.
Project-global (in interface section) is worst, module global.. better,
"class-global" = static - would be nice, but IMO not possible.

Quote
>within the module, but it belongs to a class. It's semantics, really.

I agree, as you see.

Peter

P.S.: Sorry - to busy/ lazy to make up an example for useful virtual
functions.

Other Threads