Board index » delphi » Is There static variable in delphi class ?

Is There static variable in delphi class ?

Please Help

How To do a static variable equivelent to C++ static member of class.

int TFoo ::count = 0

class TFoo
{
  public:
  static int count;

Quote
};

I mean static The lifetime of that object is long as class lifetime not as
its object one
 

Re:Is There static variable in delphi class ?


Quote
<Rafal.Kukuc...@wind.pl> wrote in message

news:94rlh0$ddq$1@news.tpi.pl...

Quote
> Please Help

> How To do a static variable equivelent to C++ static member of
class.

> int TFoo ::count = 0

> class TFoo
> {
>   public:
>   static int count;
> };

> I mean static The lifetime of that object is long as class lifetime
not as
> its object one

Just declare a variable in the implementation section. static used to
mean private to a translation unit in C, in Pascal putting it in the
implementation section makes it private to the unit.

But you want it to be public. Maybe someone will say what the idiom
would be if you wanted to encapsulate it in a class. Member functions
to look up the implementation-section variable? That has function call
overhead, I'd be tempted to use a global variable TFooCount:Integer

Incidentally, if you want static variables in a function, you can use
typed constants unless {$J-} is defined. In C statics hold their value
from one invocation of a function to the next, so do typed constants.

FP

Re:Is There static variable in delphi class ?


Quote
"Frank Peelo" <fpe...@portablesolutions.com> wrote in message

news:94s0i0$qf8$1@kermit.esat.net...

Quote

> But you want it to be public. Maybe someone will say what the idiom
> would be if you wanted to encapsulate it in a class. Member functions
> to look up the implementation-section variable? That has function call
> overhead, I'd be tempted to use a global variable TFooCount:Integer

Two possibilities: a property with read & write accessors, or two class
methods (read/write). The first has the advantage of being a property and
the disadvantage that class references are not allowed. The second means
that one can't use the property syntax to reference the counter but one can
get to the value using class references.

a)

type tFoo = class (tSomething)
  private
    function GetFooCount : integer;
    procedure SetFooCount (newCount : integer);
  public
    property FooCount : integer read GetFooCount write SetFooCount;
  end;

Implementation

var    FooCounter : integer = 0;

function tFoo.GetFooCount : integer;

begin
result := FooCounter;
end;

b)

type tFoo = class (tSomething)
  public
    class function GetFooCount : integer;
    class procedure SetFooCount (newCount : integer);
  end;

Implementation

var FooCounter : inteteger = 0;

class function tFoo.GetFooCount : integer;

begin
result := FooCounter;
end;

Re:Is There static variable in delphi class ?


Quote
"Frank Peelo" <fpe...@portablesolutions.com> wrote in message news:94s0i0$qf8$1@kermit.esat.net...

> <Rafal.Kukuc...@wind.pl> wrote in message
> news:94rlh0$ddq$1@news.tpi.pl...
> > Please Help

> > How To do a static variable equivelent to C++ static member of
> class.

> > int TFoo ::count = 0

> > class TFoo
> > {
> >   public:
> >   static int count;
> > };

> > I mean static The lifetime of that object is long as class lifetime
> not as
> > its object one

> Just declare a variable in the implementation section. static used to
> mean private to a translation unit in C, in Pascal putting it in the
> implementation section makes it private to the unit.

> But you want it to be public. Maybe someone will say what the idiom
> would be if you wanted to encapsulate it in a class. Member functions
> to look up the implementation-section variable? That has function call
> overhead, I'd be tempted to use a global variable TFooCount:Integer

> Incidentally, if you want static variables in a function, you can use
> typed constants unless {$J-} is defined. In C statics hold their value
> from one invocation of a function to the next, so do typed constants.

like this,  J+ is normally the default

// the purpose of the following is 'none', illustrative only
//{$J-} // uncomment to test defines
{$IFOPT J-}
var CGlobalval:integer = 1;
{$ELSE}
const CGlobalval:integer = 1;
{$ENDIF}

{$IFOPT J-}
{$DEFINE JWASOFF}
{$J+}
{$ENDIF}
procedure cfunc;
const Cval:integer =1; // this is essentially static across calls
// and modifiable as long as J+ holds
begin
  {$IFDEF JWASOFF}
    form1.memo1.lines.add('J wasoff');
  {$ENDIF}
  form1.memo1.lines.add(inttostr(cval));
  inc(cval);

  form1.memo1.lines.add(inttostr(cglobalval));
  inc(cglobalval);
end;
{$IFDEF JWASOFF}
{$UNDEF JWASOFF}
{$J-}
{$ENDIF}

Other Threads