Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to make a Static declaration in a Delphi Class ?

2,521 views
Skip to first unread message

Søren Ove Hansen

unread,
Jan 12, 2001, 1:43:25 PM1/12/01
to
We all know this one:

TMyClass = Class
Public
Constructor Create;
Private
......
Protected
....
end;

But I need to make a Static Variable, I know have to do it in C++, put not
in Delphi/Pascal.

Can Any one help me please.

Søren Hansen Denmark


Howard Moon

unread,
Jan 12, 2001, 2:08:40 PM1/12/01
to
Delphi does not have static member variables. It supports static methods,
but not static variables. I guess you could write a property read method
for a private member variable that returns a constant, if you really need to
have a constant value for a member variable across instances of the class.
Or, just define a constant in the unit the class is defined in, and
reference that constant instead of the class member variable.
-Howard

Arthur E.F.Heinrich

unread,
Jan 12, 2001, 4:11:55 PM1/12/01
to
Why the variable must be static?

If you create a global variable you obtain the same effect.
Static methods was created because Java is Object Oriented, but sometimes we need to use a simple function like StrToInt, Sin,
Sqrt... In Delphi we don't need to create a class to implement this kind of procedures or functions.

In Object Pascal you use Object Orientation if you want.

If you need a static variable inside a method or procedure/function, declare the variable as a typed constant.

function NextInt : Integer;
const
staticint : Integer = 0;
begin
Inc(staticint);
Result := staticint;
end;

[]s
Arthur

Søren Ove Hansen <henriett...@mail.tele.dk> escreveu nas notícias de mensagem:93njp4$rg...@bornews.inprise.com...

Robert Kuhlmann

unread,
Jan 12, 2001, 4:35:02 PM1/12/01
to
Hi Søren,

there are no generic "class constants" in Delphi. But you may declare a
class function that returns the value you need:

TMyClass = class
public
class function MyStaticClassValue : integer;
end;

implementation

class function TMyClass.MyStaticClassValue : integer;
begin
result := 17;
end;

You can now get the value even without having an instance if the class
by e.g.

MyTestVar := TMyClass.MyStaticClassValue;
{MyTestVar now has a value of 17}

Of course you will not be able to assign a different value to this.

Now if you declare a variable in the interface-section of the unit the
class is implemented in, and give your class an extra Set-method the
solution could look like this:

unit Test;

interface

TMyClass = class
public
class function MyClassValue : integer;
class procedure SetMyClassValue(AValue : integer);
end;

implementation

var
MycalssValue : integer;

class function TMyClass.MyClassValue : integer;
begin
result := MyClassValue;
end;

class procedure SetMyCalssValue(AValue : integer);
begin
MyClassValue := AValue;
end;

initaliazation
MyClassValue := 0;
end.

The users of unit Test will only be able to manipulate MyClassValue
through your class-methods, because the variable is only visible within
the implementatio of the unit.

Is this what you've requested?

Bye
Robert

P.S.: And PLEASE, not another discussion about class-varaibles in
genral. Okay?

ahmoy

unread,
Jan 14, 2001, 10:41:15 PM1/14/01
to
maybe you can use initialization and finalization to simulate the static
object.


Søren Ove Hansen <henriett...@mail.tele.dk> wrote in message
news:93njp4$rg...@bornews.inprise.com...

0 new messages