Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-11

variables in class, class variables and fields

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hello,

Following the bug discovered in the class variables (on fpc devel), I actually would like to know the need (or usage) for variables and class variables in a class (I know it's a delphi thingy, but still).

Furthermore, why can't I create a class field, so the field will have the same value on each instance of the class and it's sub classes ?

Thanks,
Ido

LINESIP websites:
http://www.linesip.com
http://www.linesip.co.il

Reply by email

José Mejuto
to ik

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é

Reply by email

leledumbo
to José Mejuto
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 ?

Yes. Usually we need this when creating a thread pool, so we know how many threads have been created.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/variables-in-class-class-variables-and-fields-tp3252382p3252858.html Sent from the Free Pascal - General mailing list .

Reply by email

ik
to leledumbo

But why class variables and not class fields ?

Ido

LINESIP websites:
http://www.linesip.com
http://www.linesip.co.il

leledumbo wrote 15 lines
> 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 ? Yes. Usually we need this when creating a thread pool, so we know how many threads have been created. -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/variables-in-class-class-variables-and-fields-tp3252382p3252858.html Sent from the Free Pascal - General mailing list . _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Sven Barth
to ik
ik wrote
But why class variables and not class fields ?

As I personally think about normal class fields as "variables per class instance", I see no difference between calling something "class field" or "class variable".

If it is the "var" keyword you are stumbling on, then let me tell you that normal fields can also be started with "var" (which is needed when you defined class types or constants before that).

E.g. (compiled with 2.5.1):

   TTest = class
   private
     const
       Foo = 42;
     var
       fTestField: Integer;
   public
     property TestField: Integer read fTestField;
   end;

Regards,
Sven

Reply by email

ik
to Paul Ishenin
Paul Ishenin wrote 9 lines
06.11.2010 21:39, ik wrote: > But why class variables and not class fields ? > What is the difference? "var" is already a reserved word. reserving a word "field" will cause more problems than using an already reserved word.

why not something like this:

TAClass = class
private
  FField : class integer;
end;

Further more, what is the difference between a "normal" field and a variable inside a class:

TAClass = class
private
  var Variable : Integer;
end;

That's what I'm trying to figure out.

Ido

Show quoted text 8 lines
Best regards, Paul Ishenin. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Paul Ishenin
to ik
ik wrote 6 lines
why not something like this: TAClass = class private FField : class integer; end;

"var" and "class var" starts new sections in the class declaration. Therefore you can define several class fields or instance fields after them.

For example
TAClass = class
private
   class var
     Field1: Integer;
     Field2: String;
   var
     Field3: Integer;
     Field4: String;
end;
Show quoted text 7 lines
Further more, what is the difference between a "normal" field and a variable inside a class: TAClass = class private var Variable : Integer; end;

var - you start a fields section
Variable : Integer; - you declare a field inside this section.

You can skip "var" in this case. But if you previosly had "const" or "type" section or had a method/property declaration and want to declare a field again then you need to explicitly start a field section by "var" keyword.

For example:

TAClass = class
private
   type
      TMyInt = type integer;
   var // this keyword is required here
     Field1: TMyInt;
end;

Best regards,
Paul Ishenin.

Reply by email

Jonas Maebe
to ik: why not something like this: TAClass = class private FField : class integer; end; Further…
ik wrote
Further more, what is the difference between a "normal" field and a variable inside a class:

Nothing. Everything defined in a "var" section is a field. The reason the "var" keyword can now be used, is because it is now also possible to define types and constants inside classes, so you need a way to switch to defining fields after a "type" or "const" section.

Jonas

Reply by email