views:

828

answers:

1

Hello,

I googled,I binged,I already have seen the other "duplicates" here,but none of them work in Delphi 2009 updated up to update 4.

Like in C#,I want to make a static variable in on line or as short as possible.In the end it works like a global variable,but its sorted.

What's the shortest way to do this in delphi 2009?

EDIT

I followed some of your answers,but it doesn't work.

type:

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

code:

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

Error:

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

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

or shorter if you don't use a property

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

edit: Note the class in class var. You forgot that part.

Lars Truijens
I edited my question so you can see my code.I get error when uing your code.
John
I forgot "class".Now it works perfect!
John
You can also specify a default value for the property.
skamradt
You could, but it won't do you any good unless it is in the published section and you also set the default value in the constructor and you use streaming and since it is a static/class property streaming is of no use. :)
Lars Truijens