Class/Static Constants in Delphi

前端 未结 9 715
粉色の甜心
粉色の甜心 2021-02-05 12:06

In Delphi, I want to be able to create an private object that\'s associated with a class, and access it from all instances of that class. In Java, I\'d use:

pub         


        
9条回答
  •  囚心锁ツ
    2021-02-05 12:33

    In Delphi static variables are implemented as variable types constants :)

    This could be somewhat misleading.

    procedure TForm1.Button1Click(Sender: TObject) ;
    const
       clicks : Integer = 1; //not a true constant
    begin
      Form1.Caption := IntToStr(clicks) ;
      clicks := clicks + 1;
    end;
    

    And yes, another possibility is using global variable in implementation part of your module.

    This only works if the compiler switch "Assignable Consts" is turned on, globally or with {$J+} syntax (tnx Lars).

提交回复
热议问题