Static Classes in Delphi (Win32)

Is it possible in Delphi (Win32) to declare a whole class (and not just a class function) static?

+6
oop delphi
source share
8 answers

I assume that you are referring to static classes such as .net (and not "static" like in traditional Delphi / Native), and there is no answer to this question.

+5
source share

Looks like a user search for "class functions":

type TSomeClass = class class procedure MyFunction(...); 

This is similar to a static method, so name it:

 TSomeClass.MyFunction(...); 
+8
source share

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

 TMyClass = class abstract public class procedure DoStuff; static; end; 

This will ensure that the singleton pattern is respected and prevent any instance period.

+5
source share

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

 TSomeClass.SomeMethod; 

Is this what you want?

+4
source share

Not initially.

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

For walkthroughs on implementing this, I would recommend this guide that covers almost any version of delphi, but if you are using Delphi 2010 you can also use the new class constructors / destructors to improve the results.

+2
source share

You can also create a new module called uDG_Utils, for example, define a class, define a global variable for this class, and in the initialization and completion section you control the constructor and destructor of the class. Now all you have to do is call it as mySuperDuperClass.SuperMethod ...

+1
source share

You can create a class that contains nothing but static methods. If you need to maintain some kind of state, state variables should be passed as var parameters. It is impossible to "access" static variables differently than having a set of global variables in the implementation section of the OUTSIDE class for the class, for example:

 UNIT TestUnit; INTERFACE Type TStaticClass = Class public procedure Foo(var Bar:String); static; end; IMPLEMENTATION var LastBar : String; // the last bar LastRes : string; // the last result Procedure TStaticClass.Foo(var Bar:String); begin if Bar <> LastBar then LastRes := SomeCPUExpensiveProcess(Bar); LastBar := Bar; Bar := LastRes; end; INITIALIZATION LastBar := ''; LastRes := SomeCPUExpensiveProcess(''); END. 
+1
source share

(Yes, I know this thread is outdated, but I thought I would post it for posterity.)

It was pointed out that class functions and class procedures implement static methods. But I will add that the next notable behavior of the static class (against the Delphi class) is that the static class cannot be created.

Delphi classes receive an open default constructor (without parameters) if you do not specify it, so any instance can be created. If you declare one or more constructors explicitly, this constructor is not provided.

You can remove all constructors by declaring the constructor in a private or protected section of your class. This removes your constructor from the scope of the consumer. Now there is only one constructor, it is not displayed and the class cannot be created.

Example:

 type TMyStaticClass = class(TObject) private // Hide the default constructor, suppressing hint H2219. {$HINTS OFF} constructor Create; {$HINTS ON} public class procedure Method1; // A static method end; implementation constructor TMyStaticClass.Create; begin // Do nothing. This cannot be called. end; class procedure TMyStaticClass.Method1(); begin // Do something here. end; 

If you have one of the newer versions of Delphi, you can also consider sealing the class to be a little more correct. The COULD descendant class is created if your constructor is protected, not private.


Edit

Here's an Ken-inspired alternative for newer versions of Delphi (which support private classes) that don't require compiler hints. This is not an ideal solution, but here it is.

 unit Unit1; interface type TMyStaticClass = class sealed (TObject) protected constructor Create; public class procedure Method1; // A static method end; implementation constructor TMyStaticClass.Create; begin // Do nothing. This cannot be called. end; class procedure TMyStaticClass.Method1(); begin // Do something here. end; end. 
-one
source share

All Articles