* * *

Author Topic: Static variables  (Read 11213 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: 411
    • 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: 4525
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 »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Thaddy

  • Hero Member
  • *****
  • Posts: 4525
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 »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus