Class field (static field) in Delphi

delphi, static, tdictionary

Solution

You can declare a class variable:

type 
  TMyClass = class
  private
    class var
      FMyClassVar: Integer;
   end;

Obviously you can use whatever type you like for the class variable.

Class variables have global storage. So there is a single instance of the variable. A Delphi class variable is directly analagous to a C# static field.

Problem

There is a class TPerson. It is known that FSecondName unique to each object. ``` type TPerson = class(TObject) private FAge: Integer; FFirstName: String; FSecondName: String; public property Age: Integer read FAge; property FirstName: String read FFirstName; property SecondName: String read FSecondName; constructor Create; end; ``` How can I add a class field (like static field in C#) Persons: TDictionary (String, TPerson), where the key is SecondName and the value is an object of class TPerson. Thanks!

Original source