[fpc-pascal] variables in class, class variables and fields

José Mejuto joshyfun at gmail.com
Fri Nov 5 22:11:33 CET 2010


Hello FPC-Pascal,

Friday, November 5, 2010, 8:54:08 PM, you wrote:

i> Following the bug discovered in the class variables (on fpc devel), I
i> actually would like to know the need (or usage) for variables and class
i> variables in a class (I know it's a delphi thingy, but still).
i> Furthermore, why can't I create a class field, so the field will have the
i> same value on each instance of the class and it's sub classes ?

As far as I know, scope. Example:

TClass1=class
public
  var class count: integer;
  Constructor Create;
end;

constructor TClass1.Create;
begin
  inc (Count);
end;

function How(): integer;
begin
  Result:=TClass1.Count;
end;

------------------------------------

TClass1=class
public
  Constructor Create;
end;

var
  TClass1_Count: integer=0;

constructor TClass1.Create;
begin
  inc (TClass1_Count);
end;

function How(): integer;
begin
  Result:=TClass1_Count;
end;

--------------------------------------

I think both are basically identical, but the work of the first one is
much more clear. I had never used them, but I think the above code is
right, do not ?

-- 
Best regards,
 José




More information about the fpc-pascal mailing list