Andrew
Andrew

Reputation: 167

Class field (static field) in Delphi

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!

Upvotes: 8

Views: 4859

Answers (1)

David Heffernan
David Heffernan

Reputation: 612914

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.

Upvotes: 14

Related Questions