Board index » delphi » static class members

static class members

Is it possible, in delphi, to make a member of a class static (retain its
value in any instance of the class)

Thanks
Phil Jackson

 

Re:static class members


wacko <355@mailandnews|remove this|.com> > wrote in message
<3aad6be3_2@dnews>...

Quote
>Is it possible, in delphi, to make a member of a class static (retain
its
>value in any instance of the class)

Object Pascal doesn't currently support class variables, but they can be
emulated with variables defined in the implementation section:

type
    TMyClass = class
    private
        procedure SetClassVar(value: integer);
        function GetClassVar: integer;
    public
        property ClassVar: integer read GetClassVar write SetClassVar;
    end;

implementation

var MyClassVar: integer;

procedure TMyClass.SetClassVar(value: integer);
begin
    MyClassVar := value;
end;

function TMyClass.GetClassVar: integer;
begin
    value := MyClassVar;
end;

--
Wayne Niddery (WinWright Inc.)
RADBooks - http://members.home.net/wniddery/RADBooks/delphibooks.html
"At the apex of every great tragedy of mankind there stands the figure
of an incorruptible altruist" - Ayn Rand

Re:static class members


That seems a bit messy, I dont like using public vars when i dont need to is
there no other way?

Phil Jakcson

"Wayne Niddery (TeamB)" <winwri...@chaffhome.com> wrote in message
news:3aad7eaa$1_2@dnews...

Quote
> wacko <355@mailandnews|remove this|.com> > wrote in message
> <3aad6be3_2@dnews>...
> >Is it possible, in delphi, to make a member of a class static (retain
> its
> >value in any instance of the class)

> Object Pascal doesn't currently support class variables, but they can be
> emulated with variables defined in the implementation section:

> type
>     TMyClass = class
>     private
>         procedure SetClassVar(value: integer);
>         function GetClassVar: integer;
>     public
>         property ClassVar: integer read GetClassVar write SetClassVar;
>     end;

> implementation

> var MyClassVar: integer;

> procedure TMyClass.SetClassVar(value: integer);
> begin
>     MyClassVar := value;
> end;

> function TMyClass.GetClassVar: integer;
> begin
>     value := MyClassVar;
> end;

> --
> Wayne Niddery (WinWright Inc.)
> RADBooks - http://members.home.net/wniddery/RADBooks/delphibooks.html
> "At the apex of every great tragedy of mankind there stands the figure
> of an incorruptible altruist" - Ayn Rand

Re:static class members


wacko <355@mailandnews|remove this|.com> > wrote in message
<3aae48ed_2@dnews>...

Quote
>That seems a bit messy, I dont like using public vars when i dont need
to is
>there no other way?

Because it's in the implementation section, it's only "public" to that
unit. Nothing outside that unit can see or touch it, it can only be
accessed via your class' public property (and you only provide that
public property of you want).

I agree true class (static) variables would be better, and maybe they'll
be added in future, but I don't consider this solution that bad.

--
Wayne Niddery (WinWright Inc.)
RADBooks - http://members.home.net/wniddery/RADBooks/delphibooks.html
"At the apex of every great tragedy of mankind there stands the figure
of an incorruptible altruist" - Ayn Rand

Re:static class members


Quote
"wacko" <355@mailandnews|remove this|.com> wrote in message

news:3aae48ed_2@dnews...

Quote
> That seems a bit messy, I dont like using public vars when i dont need to
is
> there no other way?

In addition to Wayne's comments, given Delphi's source file "friendships",
even private class variables (if they existed) would be visible to the
entire source module.  Therefore, implementation section globals (private to
source file), though seemingly clumbsy, serve the same purpose for now.

If you want to simulate a read-only class property, you could create a class
function that returns the appropriate type.  However, writing isn't
possible...

Re:static class members


Quote
> If you want to simulate a read-only class property, you could create a
class
> function that returns the appropriate type.  However, writing isn't
> possible...

There is a solution to this in thread "Does Delphi support class variables?"

Other Threads