Delphi Static Class

Posted on
  1. Delphi Class Example
  2. Delphi Static Class

In my class, i need to use a static variable ( static int next_id; in C++)

Recipes to master Delphi for IoT integrations, cross-platform, mobile. Class function GetHomePath: string; static; class function GetDocumentsPath: string;. Real Delphi class methods are a nice OO concept, one you won't even find in C# or Java, since each Delphi class methods have a 'Self' pseudo-parameter ('this' in C# and Java), which is however not an instance, it is a class. Static methods have none of that, and are no more OO than standalone functions. -- Rudy Velthuis [TeamB].

I use

I get Error : PROCEDURE or FUNCTION expected. How to declare some variable with Delphi 5 ?

user4054093

3 Answers

Expanding on Rudy's answer...

Delphi 5 did not yet have this available. But you could at least declare a global variable. I won't copy Rudy's code, but I will add that in order to initialize them (and clean them up if necessary), you should use the initialization (and finalization) sections of a unit. These go on the very bottom of a Delphi unit, like so...

Or in your case...

And your scenario in particular won't require a finalization section.

Jerry DodgeJerry Dodge

In Delphi 5, you can't. No class vars in Delphi 5 yet.

The next best thing is a global variable in the implementation section of the unit, though.

Or alternatively, at the very bottom:

Rudy VelthuisRudy Velthuis

Sample of class variable declaration:

usage sample from other unit:

you don't need to create and destroy this class. it will be created automatically before everything else.

what is wrong in your sample: class variable must be declared inside class. i don't see class declaration in your sample. plus, as was mentioned before, Delphi 5 (very very old) do not support this feature.

ZamZam
Class

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

Delphi Class Example

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.

Delphi Static Class

Lars DLars D
Static

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.

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
Delphi Static Class

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