Recent

Author Topic: Static variables  (Read 22460 times)

jyeer

  • Newbie
  • Posts: 2
Static variables
« on: July 18, 2008, 09:03:51 am »
I'm sorry to ask this but i searched and can't find a clue

assume that I've a human class and I want to keep track on the number of instances in a static class variable?

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
RE: Static variables
« Reply #1 on: July 18, 2008, 12:50:10 pm »
By default Delphi (and thus FPC in delphi mode) doesn't allow static variables, only static methods. There are 2 ways to implement solution to your problem.

1. You can turn on the static variables support in FPC (not delphi compatible)
2. You can use an implementation section variable e.g:

Code: [Select]

unit x;

interface

type
  TMyClass = class
  ...
  end;

implementation

var
  MyClassCount: Integer = 0; // the counter

constructor TMyClass.Create;
begin
  Inc(MyClassCount);
  ...
end;

end;


To get the count you can either make a global read-only property (again, not delphi compatible) or function which gives the count.

jyeer

  • Newbie
  • Posts: 2
RE: Static variables
« Reply #2 on: July 18, 2008, 04:17:08 pm »
Thanks alot Almindor2 it just cut it

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: RE: Static variables
« Reply #3 on: February 22, 2017, 05:36:09 am »
By default Delphi (and thus FPC in delphi mode) doesn't allow static variables, only static methods. There are 2 ways to implement solution to your problem.
Is absolutely not true for FPC, only for Delphi... mode Delphi is to compile Delphi code, not to make FPC code Delphi compatible...

See the example below.
« Last Edit: February 22, 2017, 08:23:06 am by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Static variables
« Reply #4 on: February 22, 2017, 07:39:44 am »
@jyeer
Here's an example that answers the question:
Code: Pascal  [Select][+][-]
  1. program human;
  2. {$mode delphi}{$H+}
  3. type
  4.   THuman = class
  5.   private
  6.   var Finstances:integer;static;  // in fpc the same as class var Finstances.
  7.   public
  8.   constructor create;
  9.   destructor destroy; override;
  10.   class property instances:integer read Finstances;
  11.   end;
  12.  
  13.   constructor THuman.create;
  14.   begin
  15.     inherited;
  16.     inc(Finstances);
  17.   end;
  18.  
  19.  destructor THuman.Destroy;
  20.   begin
  21.     dec(Finstances);
  22.     inherited;
  23.   end;
  24.  
  25. var
  26.  a,b,c,d:THuman;
  27. begin
  28.   writeln(THuman.Instances);
  29.   a := THuman.create;
  30.   writeln(THuman.Instances);
  31.   b := THuman.create;
  32.   writeln(THuman.Instances);
  33.   c := THuman.create;
  34.   writeln(THuman.Instances);
  35.   d := THuman.create;
  36.   writeln(THuman.Instances);
  37.   d.free;
  38.   writeln(THuman.Instances);
  39.   c.free;
  40.   writeln(THuman.Instances);
  41.   b.free;
  42.   writeln(THuman.Instances);
  43.   a.free;
  44.   writeln(THuman.Instances);
  45. end.
  46.  

As you can see Almindor was wrong with his assumptions, but his (two) was helpful to you.

If you must have full Delphi compatibility (XE+!) , compile in Delphi too, replace
var Finstances:integer;static;
with
class var Finstance:integer;
« Last Edit: February 22, 2017, 08:26:51 am by Thaddy »
Specialize a type, not a var.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Static variables
« Reply #5 on: April 30, 2020, 02:20:52 pm »
Okay,

VB6-like static variables now exist (today, April 30, 2020) in Object Pascal ==> see https://forum.lazarus.freepascal.org/index.php/topic,49593.msg359954.html#msg359954 .
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Static variables
« Reply #6 on: April 30, 2020, 02:28:59 pm »
Okay,

VB6-like static variables now exist (today, April 30, 2020) in Object Pascal ==> see https://forum.lazarus.freepascal.org/index.php/topic,49593.msg359954.html#msg359954 .
They have existed for about 25 years?
Specialize a type, not a var.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Static variables
« Reply #7 on: July 06, 2020, 01:50:31 pm »
In fact, my previous post is wrong (i had read too fast): in Pascal the Static variables (var Finstances: integer; static;) do roughly the same thing as Class variables (Class var Finstances: integer;): they save the information in a Class, whether or not object instances are created.

In VB6, what are called static variables, it is that (thanks @TRon and @jamie): https://forum.lazarus.freepascal.org/index.php/topic,50447.msg368218.html#msg368218 .
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018