Class field (static field) in Delphi

There is a TPerson class. FSecondName is known to be 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 to add a class field (for example, a static field in C #) Persons: TDictionary (String, TPerson), where the key is SecondName and the value is an object of the TPerson class.

Thanks!

+6
source share
1 answer

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. Thus, there is one instance of the variable. A Delphi class variable is directly analogous to a C # static field.

+8
source

Source: https://habr.com/ru/post/951389/


All Articles