2009-07-08 49 views
6

我一派,我猛吃,我已經看到了其他的「重複」在這裏,但他們都不德爾福2009年更新了工作在C#中更新4.如何在Delphi 2009中聲明靜態變量?

一樣,我想在一個靜態變量行或儘可能短。最後,它像一個全局變量,但它的排序。

在delphi 2009中做這件事的最簡單方法是什麼?

編輯

我跟着你的一些答案,但它不工作。

類型:

type 
TmyClass = class(TObject) 
    var staticVar:integer; 
end; 

代碼:

procedure TForm1.Button1Click(Sender: TObject); 
var a:integer; 
begin 
    TMyClass.staticVar := 5; // Line 31 
    a := TMyClass.staticVar; // Line 32 
    MessageBox(0,IntToStr(a),'',0); 
end; 

我收到以下錯誤:

[DCC Error] Unit1.pas(31): E2096 Method identifier expected 

[DCC Error] Unit1.pas(32): E2096 Method identifier expected 
+0

「...但它的排序」 - 這是什麼意思? – jpfollenius 2009-07-08 06:23:14

+0

你想在哪個範圍級別聲明你的靜態變量?功能級別?課程級別?單元級別? – jpfollenius 2009-07-08 06:24:19

回答

18
type 
    TMyClass = class(TObject) 
    private 
    class var FX: Integer; 
    public 
    class property X: Integer read FX write FX; 
    end; 

,或者如果你不使用屬性短

type 
    TMyClass = class(TObject) 
    public 
    class var X: Integer; 
    end; 

編輯:注意類變種的。你忘了那部分。

相關問題