Delphi Static Class

Is it in Delphi (Win32) possible to declare a whole class (not only a function of the class) as static?

Wolfhard Kupfer

8 Answers

I assume you mean static classes like in .net (and not 'static' as in traditional Delphi/Native) - and the answer to that is no.

Lars DLars D

Looks like user search for 'class functions':

This is like static method, so, call it:

YorieYorie

I would use an abstract class (not to be confused with an abstract method in a class) to prevent it from being instantiated instead of demoting the constructor to protected:

That will enforce the singleton pattern and prevent any instantiations period.

RonRon

I am not quite sure what you mean by a 'static class'. You can declare a class, that has only class methods, so these methods can be called without instantiating the class.

Is that what you want?

dummzeuchdummzeuch

Not natively.

Find clip studio serial number. It saves your time to write story and create animation character for animations.

Depending on what you need it for, if for the purposes of your code, in some use cases you could replace it with a Singleton Pattern object.

For walkthrough on implementing this I'd recommend this guide which, covers almost any version of delphi, but if you're using Delphi 2010 you could also use the new class Constructors/Destructors for improved results.

jamieijamiei

You could create a class that contains nothing but static methods. If you have to maintain some sort of state, then the state variables should be passed as var parameters. There is no way to 'properly' access static variables other than having a set of global variables in the implementation section of the class OUTSIDE the scope of the class, for example:

skamradtskamradt

You can also create a new unit called uDG_Utils for example, define a class, define a global variable for that class and in the initialization and finalization section you manage the class constructor and destructor.Now all you need to do is call it like mySuperDuperClass.SuperMethod..

delphigeist

EDITI have edited this post to remove it. The answer is admittedly bad and deserves the down-votes. I don't want it to remain here to confuse or mislead anyone further.

Phil GilmorePhil Gilmore

Static variables and methods in Delphi 6

Hi everyone,

Can anyone tell if Delphi 6 has anything like the 'static' keyword in C++.

--
Cheers

Colin
c..@picknowl.com.au
colin.shortl..@jcu.edu.au

'Falling down is a mistake, not getting up is failure' Helen Keller

Re:Static variables and methods in Delphi 6


Quote
Colin Shortland wrote:
> Can anyone tell if Delphi 6 has anything like the 'static' keyword in C++.
That keyword has different uses depending on context, doesn't it? Here
are the contexts I'm aware of:

Used on a variable inside a subroutine so the variable keeps its value
on subsequent uses of the routine. See 'typed constants' in the help.

Used on a variable, constant, or subroutine to make it only available
within that source file. In Pascal, make those declarations in the
implementation section of a unit.

Used on a method of a class to make the method callable without having
to instantiate the class. See 'class methods' in the help.

Used on a field of a class for the same purpose. Delphi has no
equivalent, although I think I heard that either Delphi 7 or the .Net
preview has added this feature. A workaround is to use a variable
declared with unit scope to hold the value and use class methods to get
and set the value.

--
Rob

Re:Static variables and methods in Delphi 6


Quote
Colin Shortland wrote in message ..
>Hi everyone,

>Can anyone tell if Delphi 6 has anything like the 'static' keyword in C++.

Yes and no. You _can't_ have them in procedures and functions
(including methods). Just forget about it, it's impossible.

But a field in a class behaves almost like one. Obviously, having
a variable that's persistent and local to an object class is not
the same as having one that's persistent and local to a function,
but it does allow for persistence and encapsulation, which is the
whole point.

Groetjes,
Maarten Wiltink

Re:Static variables and methods in Delphi 6


Quote
'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message
news:ajask2$rqd$1@news1.xs4all.nl..
Quote
> Yes and no. You _can't_ have them in procedures and functions
> (including methods). Just forget about it, it's impossible.
Writeable consts behave this way (though you have to set {$J+} or
{$WRITEABLECONST ON} in version 6):

{$WRITEABLECONST ON}

procedure RetainKnowledge;
const
MyStaticVar: Integer = 0;
begin
Inc(MyStaticVar);
Heroes of might and magic 6 walkthrough. ShowMessage('This is increasing! ' + IntToStr(MyStaticVar));
end;

procedure Test;
var
i: Cardinal;
begin
for i := 0 to 4 do
RetainKnowledge;
end;

You can simulate class-level static vars by declaring a variable after the
implementation part. You can add in a protected property to allow
sub-classes access to the var if you want.

Alistair Keys

Re:Static variables and methods in Delphi 6


Quote
Alistair Keys wrote in message ..
>'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message
>news:ajask2$rqd$1@news1.xs4all.nl..
<Can I make local static variables in Delphi?>
Quote
>> Yes and no. You _can't_ have them in procedures and functions
>> (including methods). Just forget about it, it's impossible.

>Writeable consts behave this way (though you have to set {$J+} or
>{$WRITEABLECONST ON} in version 6):

In my programs, constants don't behave that way.

Can you see the restraint that went into that sentence?

Groetjes,
Maarten Wiltink

Re:Static variables and methods in Delphi 6


Quote
> In my programs, constants don't behave that way.

> Can you see the restraint that went into that sentence?

I only informed the reader how to do it. I didn't recommend it. Note the
distinction there too.

Re:Static variables and methods in Delphi 6


Quote
Maarten Wiltink wrote:
> Alistair Keys wrote in message ..

>>'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message
>>news:ajask2$rqd$1@news1.xs4all.nl..

> <Can I make local static variables in Delphi?>

>>>Yes and no. You _can't_ have them in procedures and functions
>>>(including methods). Just forget about it, it's impossible.

>>Writeable consts behave this way (though you have to set {$J+} or
>>{$WRITEABLECONST ON} in version 6):

> In my programs, constants don't behave that way.

> Can you see the restraint that went into that sentence?

Saying 'it's impossible' isn't the same as saying 'it's impossible the
way I have configured the compiler on my system.' The qualification is
important. The fact is, you *can* have local static variables in
procedures and functions (including methods).

--
Rob

Re:Static variables and methods in Delphi 6


Quote
'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message
> In my programs, constants don't behave that way.

> Can you see the restraint that went into that sentence?

Don't get hung up on poor a poor wording choice by Borland. Writable
constants have been in the compiler since TP 1 (I believe). I'll grant you
that the name should have been changed to something like Static for
ObjectPascal, but it wasn't.

Re:Static variables and methods in Delphi 6


Quote
On Tue, 13 Aug 2002 22:56:22 +0000, Bruce Roberts wrote:
> Don't get hung up on poor a poor wording choice by Borland. Writable
> constants have been in the compiler since TP 1 (I believe). I'll grant you
> that the name should have been changed to something like Static for
> ObjectPascal, but it wasn't.
Even static wouldn't really work, unless you mean something different that
how Java uses static.

The only advantage I could see with delphi's consts, and their only use,
was that one could easily define a default/initial value for the variable.

Mark

Re:Static variables and methods in Delphi 6


Quote
Bruce Roberts <b..@bounceitattcanada.xnet> wrote in message
news:2Mj69.8451$H67.45891@tor-nn1.netcom.ca..
Quote

> 'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message

> > In my programs, constants don't behave that way.

> > Can you see the restraint that went into that sentence?

> Don't get hung up on poor a poor wording choice by Borland. Writable
> constants have been in the compiler since TP 1 (I believe). I'll grant you
> that the name should have been changed to something like Static for
> ObjectPascal, but it wasn't.

True.... but it hurts!!! What's in a name.... everything if you're
programming. However there are times when the double-take confusion caused
by declaring a variable as constant (a comment helps) is not as great as
hiding the variable in a field of the class, though it has taken me some
years to accept this amiable little inconsistancy of such a strongly typed
language.

Dave

Re:Static variables and methods in Delphi 6


Quote
'David Reeve' <drscienti..@powerup.com.au> wrote in message
news:h%C69.12946
Quote
> True.... but it hurts!!! What's in a name.... everything if you're
> programming. However there are times when the double-take confusion caused
> by declaring a variable as constant (a comment helps) is not as great as
> hiding the variable in a field of the class, though it has taken me some
> years to accept this amiable little inconsistancy of such a strongly typed
> language.
There really are three different needs that should be addressed. What I
would like to see is

1. the addition of a 'Static' or 'Persistent' modifier for local variables

procedure Foo;

var x : persistent integer = 0;

or perhaps

persistent x : integer = 0;

2. the addition of 'Class' as a modifier for class fields:

tFoo = class (tObject)
private
fField : class integer;

3. the addition of 'Class' as a property modifier:

published
class property Field : integer read fField write fField;

Re:Static variables and methods in Delphi 6


Im Artikel <aj9nsr$72..@gnamma.connect.com.au>, 'Colin Shortland'
<colic..@mydesk.net.au> schreibt:
Quote
>Can anyone tell if Delphi 6 has anything like the 'static' keyword in C++.
No such keyword, but almost the same 'behaviour' is available.

In C the 'static' keyword is used to mark procedures and variables as invisible
outside a module. In Delphi everything is 'static', what is defined in the
implementation section. Everything in the interface section is exported,
equivalent to the C 'global' attribute. Consequently all Delphi/Pascal
procedures are static by default, they must be exported explicitly by copying
the procedure header into the interface section.

There exists another difference, which is not related to 'static', but often
found at the same place. C and Delphi (Pascal) require that the declaration of
a procedure has been seen by the compiler, before the procedure can be used in
other procedures. Remember that a procedure declaration consists of only the
'header' of a procedure, whereas the definition (implementation) of a procedure
includes the code for that procedure. For exported (global) procedures the
declaration you copy the procedure header into the interface section, as
outlined above. For static procedures you copy the header to the appropriate
place in the implementation section, AND append 'forward;' to it. In many cases
such forward declarations can be omitted, when the procedures are rearranged so
that every procedure only uses those procedures, which have been implemented
before.

No real equivalent exists for static variables in subroutines. I'm don't know
whether D6 still allows for writeable constants, this {*word*99} could be used to
implement static local variables. But I think that you don't mean such
variables, they are rarely used even in C code.

No direct equivalent exists for static variables in classes, at least not in
D4. A workaround for the variable TheStaticVar can look like:

type
MyClass = class
private
function getStaticVar: integer;
procedure setStaticVar(value: integer);
public //or whatever visibility
property TheStaticVar: integer read getStaticVar write setStaticVar;
end;

implementation
..

var
MyStaticVar: integer;

function MyClass.getStaticVar: integer;
begin
Result := MyStaticVar;
end;

procedure MyClass.setStaticVar(value: integer);
begin
MyStaticVar := value;
end;

Often it's easier to give the static variable an unique name, and move it out
of the class type into the body of the unit.

DoDi

Re:Static variables and methods in Delphi 6


Thankyou you all, I didn't mean to start a war on semantics :) although now
I think about it the question was a little ambiguous. However, the answer
obviously and what I feared is a NO to all contexts which the word 'static'
is used in C/C++ and Java.

Once again thanks

Colin
c..@picknowl.com.au
colin.shortl..@jcu.edu.au

'Falling down is a mistake, not getting up is failure' Helen Keller
2017 emergency response guidebook.

Re:Static variables and methods in Delphi 6


Quote
Colin Shortland wrote in message ..
>Thankyou you all, I didn't mean to start a war on semantics :) although
now
>I think about it the question was a little ambiguous. However, the answer
>obviously and what I feared is a NO to all contexts which the word
'static'
>is used in C/C++ and Java.
Static functions map well to implementation section ones, I think.
'Static' has several meanings in C, but since all people ever
ask about is the equivalent of static local variables, I skipped
the others. They aren't asked about because they _do_ have
(obvious) equivalents.

And even static local variables have a Turbo/Object Pascal equivalent.
Which is a hack, and I don't like it. But it's a syntactic hack.
Semantically static locals require a mismatch between lifetime and
accessibility. Which is exactly how they're defined; that they're
implementated just like globals shouldn't make me feel like it's
wrong for them to be inaccessible outside the procedure call, or
to exist in a different space from auto locals. But I must admit
that it does. Or perhaps did. It's not easy, growing up at my age.

Groetjes,
Maarten Wiltink

Re:Static variables and methods in Delphi 6


Quote
Colin Shortland wrote:
> Thankyou you all, I didn't mean to start a war on semantics :) although now
> I think about it the question was a little ambiguous. However, the answer
> obviously and what I feared is a NO to all contexts which the word 'static'
> is used in C/C++ and Java.
Did my post not show up, or have I now misunderstood you twice? I think
I gave an answer of YES for three of the four contexts I'm aware of in
C++. (You didn't say anything about C or Java before.) And the fourth
context has a workaround that's nearly as good as the real thing, so I
think your fear is unwarranted.

--
Rob

1. Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)

2. class variables and static variables

3. Q. Static variables in classes in Delphi?

4. Are there static variables in Delphi functions?

5. Static variables in Delphi?

6. overiding static methods in Delphi 1.0

7. static methods in Delphi 1.0

8. static variables

9. 'Static' variables

10. Static variables???