Board index » delphi » Are there static class members in ObjectPascal

Are there static class members in ObjectPascal

                  Hi,

  What I mean is can you use the C++ system like

class foo {
int a;
static int j;

Quote
};

and have only one copy of j for all instances of foo. I tried typing static
in Delphi's help but didn't get anything related to this.

  Thanx...
                                         Boban

 

Re:Are there static class members in ObjectPascal


In article <3cc9b35c_2@dnews>, Bogdan Ribic says...

Quote
>                   Hi,

>   What I mean is can you use the C++ system like

> class foo {
> int a;
> static int j;
> };

> and have only one copy of j for all instances of foo. I tried typing static
> in Delphi's help but didn't get anything related to this.

There are no static class members (fields) in Delphi. You can emulate
them by putting a "global" variable in the implementation section of the
unit in which the classes are declared, and, if necessary, having a
property with getter and setter methods that access it.

There are static member functions though, they are called class
procedures or class functions. Unlike in C++, they have a hidden Self
(this) parameter too, but it points to the class itself, not to an
instance.
--
Rudy Velthuis (TeamB)

"If we knew what it was we were doing, it would not be called research,
 would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


Thanx

  Now, can I somehow use a variable within a procedure that will preserve
it's value between the calls or must I there too use file-scope local vars?

Rudy Velthuis (TeamB) <rvelth...@gmx.de> wrote in message
news:MPG.1733d5fe7343f8ac98cd29@newsgroups.borland.com...

Quote
> In article <3cc9b35c_2@dnews>, Bogdan Ribic says...

> >                   Hi,

> >   What I mean is can you use the C++ system like

> > class foo {
> > int a;
> > static int j;
> > };

> > and have only one copy of j for all instances of foo. I tried typing
static
> > in Delphi's help but didn't get anything related to this.

> There are no static class members (fields) in Delphi. You can emulate
> them by putting a "global" variable in the implementation section of the
> unit in which the classes are declared, and, if necessary, having a
> property with getter and setter methods that access it.

> There are static member functions though, they are called class
> procedures or class functions. Unlike in C++, they have a hidden Self
> (this) parameter too, but it points to the class itself, not to an
> instance.
> --
> Rudy Velthuis (TeamB)

> "If we knew what it was we were doing, it would not be called research,
>  would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


In article <3cc9d7e2_1@dnews>, Bogdan Ribic says...

Quote
> Thanx

>   Now, can I somehow use a variable within a procedure that will preserve
> it's value between the calls or must I there too use file-scope local vars?

Yes, you should use (hold on, this is not a joke) typed constants, using
the {$WRITEABLECONST} option.

  procedure TMyClass.DoSomething;
  {$WRITEABLECONST ON}
  const
    WillPreserveValueBetweenCalls: Integer = 0;
  {$$WRITEABLECONST OFF}
  begin
    Writeln(WillPreserveValueBetweenCalls);
    Inc(WillPreserveValueBetweenCalls);
  end;
--
Rudy Velthuis (TeamB)

"If we knew what it was we were doing, it would not be called research,
 would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


Hi Rudy;
Why use typed constants?, didn't you say ( in your previous post)
the implementaion level global variables act as static fields.
Could you shed some light on this please.

BTW : thanks for the pointing out {$WRITEABLECONST} compiler option, it will
come handy while working with some older projects.

"Rudy Velthuis (TeamB)" <rvelth...@gmx.de> wrote in message
news:MPG.1733fbe57b0a1e2d98cd37@newsgroups.borland.com...

Quote
> In article <3cc9d7e2_1@dnews>, Bogdan Ribic says...

> > Thanx

> >   Now, can I somehow use a variable within a procedure that will
preserve
> > it's value between the calls or must I there too use file-scope local
vars?

> Yes, you should use (hold on, this is not a joke) typed constants, using
> the {$WRITEABLECONST} option.

>   procedure TMyClass.DoSomething;
>   {$WRITEABLECONST ON}
>   const
>     WillPreserveValueBetweenCalls: Integer = 0;
>   {$$WRITEABLECONST OFF}
>   begin
>     Writeln(WillPreserveValueBetweenCalls);
>     Inc(WillPreserveValueBetweenCalls);
>   end;
> --
> Rudy Velthuis (TeamB)

> "If we knew what it was we were doing, it would not be called research,
>  would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


In article <3cc9f7c4_2@dnews>, Arjang says...

Quote
> Hi Rudy;
> Why use typed constants?, didn't you say ( in your previous post)
> the implementaion level global variables act as static fields.
> Could you shed some light on this please.

They are the classical answer to local variables that must remain static
between calls. They are of course in the same storage class as unit
globals, but not in the same scope. Only the routine can modify them.

Quote
> BTW : thanks for the pointing out {$WRITEABLECONST} compiler option, it will
> come handy while working with some older projects.

You can also use {$J}, if you are a lazy typist. <vbg>
--
Rudy Velthuis (TeamB)

"If we knew what it was we were doing, it would not be called research,
 would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


I have been surfing these news groups and any delphi related material for
past 5 years at least and this is the first time I have heard of this, Do
you have a list or repository of calssical sloutions somewhere, or is it
just the pure intelligence that makes the connection between a compiler
directive and what it could be used for.
TIA
Arjang

Quote
> They are the classical answer to local variables that must remain static
> between calls. They are of course in the same storage class as unit
> globals, but not in the same scope. Only the routine can modify them.

> > BTW : thanks for the pointing out {$WRITEABLECONST} compiler option, it
will
> > come handy while working with some older projects.

> You can also use {$J}, if you are a lazy typist. <vbg>
> --
> Rudy Velthuis (TeamB)

> "If we knew what it was we were doing, it would not be called research,
>  would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


In article <3cca03ba_1@dnews>, Arjang says...

Quote
> just the pure intelligence that makes the connection between a compiler
> directive and what it could be used for.

Of course! <vbg>

But this technique has been discussed before. It can be used to emulate
static members as well, but IMO it gets too convoluted to be useful for
that (it would preserve scope, but the solution requires a getter and a
setter and a function that contains and can change the value).
--
Rudy Velthuis (TeamB)

"If we knew what it was we were doing, it would not be called research,
 would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


And if you even more lazy, check in the project options | compiler |
assignable typed constants

BTW if you use unit scope variable, you do not need that const, just use var
var
  aVar : integer = 123;
Believe it was introduced somewhere around D3 - just to avoid writable
constants.

"Rudy Velthuis (TeamB)" <rvelth...@gmx.de> wrote in message
news:MPG.17341de46ac14b1a98cd4a@newsgroups.borland.com...

Quote
> In article <3cc9f7c4_2@dnews>, Arjang says...

> > Hi Rudy;
> > Why use typed constants?, didn't you say ( in your previous post)
> > the implementaion level global variables act as static fields.
> > Could you shed some light on this please.

> They are the classical answer to local variables that must remain static
> between calls. They are of course in the same storage class as unit
> globals, but not in the same scope. Only the routine can modify them.

> > BTW : thanks for the pointing out {$WRITEABLECONST} compiler option, it
will
> > come handy while working with some older projects.

> You can also use {$J}, if you are a lazy typist. <vbg>
> --
> Rudy Velthuis (TeamB)

> "If we knew what it was we were doing, it would not be called research,
>  would it?" -- Albert Einstein

Re:Are there static class members in ObjectPascal


While I am sure Rudy is capable of doing just that, this has been a standard
solution for emulating static variables for many moons with OP  (oops, I
mean the Delphi language)
Quote
"Arjang" <nos...@me.com> wrote in message news:3cca03ba_1@dnews...
> I have been surfing these news groups and any delphi related material for
> past 5 years at least and this is the first time I have heard of this, Do
> you have a list or repository of calssical sloutions somewhere, or is it
> just the pure intelligence that makes the connection between a compiler
> directive and what it could be used for.

Other Threads