Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 469
Andrew Avatar asked Aug 09 '13 11:08

Andrew


1 Answers

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.

like image 86
David Heffernan Avatar answered Sep 24 '22 14:09

David Heffernan