[Solved] Class variables lazarus / 62-General ---------------------------------------------------------------------- MarkMLl · 2021-09-18 19:52 If a class has a field which is itself (an instance of) a class, where should that be instantiated? I have a class TStation, where instances are usually accessed via a name/object stringlist. It occurs to me that the stringlist, together with a TCriticalSection protecting addition/deletion/reference operations, could usefully be a class variable... but that leaves me with a bootstrapping problem since until the stringlist and critical section exist I can't do anything useful. MarkMLl ---------------------------------------------------------------------- ASerge · 2021-09-19 04:35 Quote from: MarkMLl on September 18, 2021, 09:52:17 pmIf a class has a field which is itself (an instance of) a class, where should that be instantiated? General fields - in constructor/destructor. Class fields - in class constructor/destructor. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$LONGSTRINGS ON}{$APPTYPE CONSOLE} uses SysUtils, Classes; type  TClassWithClassField = class(TObject)  strict private  class var FClassVar: TStringList;    var FGeneralVar: TStringList;  public    class constructor Create;    class destructor Destroy;    constructor Create;    destructor Destroy; override;    property GeneralVar: TStringList read FGeneralVar;    class property ClassVar: TStringList read FClassVar;  end; class constructor TClassWithClassField.Create;begin  FClassVar := TStringList.Create;  FClassVar.Append('Class var');  Writeln('class constructor');end; class destructor TClassWithClassField.Destroy;begin  FClassVar.Free;  Writeln('class destructor');end; constructor TClassWithClassField.Create;begin  FGeneralVar := TStringList.Create;  FGeneralVar.Append(Format('General var Self=%p', [Pointer(Self)]));  Writeln('constructor');end; destructor TClassWithClassField.Destroy;begin  FGeneralVar.Create;  Writeln('destructor');end; procedure Test;var  C1, C2: TClassWithClassField;begin  Writeln('--Start');  C1 := TClassWithClassField.Create;  try    C2 := TClassWithClassField.Create;    try      Write(C1.ClassVar.Text);    Write(C1.GeneralVar.Text);      Write(C2.ClassVar.Text);      Write(C2.GeneralVar.Text);    finally      C2.Free;    end;  finally  C1.Free;  end;  Writeln('--End');end; begin  Test;  Readln;end. ---------------------------------------------------------------------- MarkMLl · 2021-09-19 07:08 Thanks for that, I admit that I'd overlooked the "class constructor" syntax (and am unsure where it's documented). However experimenting... the class constructor appears to be being called implicitly before the unit startup, so is of no help when it comes to setting up a list of stations with an internal critical section where the station list can be created/destroyed as part of a thread. So it looks as though I have to have a separate TStationList as the container, and that in this particular case class variables etc. aren't much help. MarkMLl ---------------------------------------------------------------------- ASerge · 2021-09-19 07:14 Quote from: MarkMLl on September 19, 2021, 09:08:31 am... the class constructor appears to be being called implicitly before the unit startup Yes, as documented. ---------------------------------------------------------------------- MarkMLl · 2021-09-19 08:06 Quote from: ASerge on September 19, 2021, 09:14:12 amQuote from: MarkMLl on September 19, 2021, 09:08:31 am... the class constructor appears to be being called implicitly before the unit startup Yes, as documented. Thanks for that, I was looking in the wrong place. MarkMLl