[Solved] How to call a class from several units once in OOP lazarus / 21-General ---------------------------------------------------------------------- nikel · 2025-04-19 13:22 Hello, I made a console application which has a config class required by 5 other units. I noticed that the class is called tens of times from functions and classes. I want to obey object oriented programming techniques but don't know how to. Here's the top part of the class. 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 unit Config; {$mode objfpc}{$m+}{$TYPEDADDRESS ON} // token 0.1 interface uses  Classes, SysUtils, StrUtils,  Token, PathsHandler; type  {TConfig}  PConfig = ^TConfig;  TConfig = class  private    FDayLongStartTime      : shortstring;    FDayLongEndTime      : shortstring;    FDayLongMinBpm         : Byte;    FDayLongMaxBpm         : Byte;    FDayLongPath           : string;    FNightLongStartTime    : shortstring;    FNightLongEndTime      : shortstring;    FNightLongMinBpm       : Byte;    FNightLongMaxBpm     : Byte;    FNightLongPath         : string;    FFileType            : shortstring;    FLastDirectionOfSort   : shortstring;    FDayLongParams         : shortstring;    FNightLongParams       : shortstring;    FBasePath              : string;    FMinBpm              : Byte;    FMaxBpm                : Byte;    FLastPeriod          : shortstring;     FNameOfIndex           : shortstring;    FConfigFile            : string;    FDelimiter             : Char;    FCurrentDir            : string;    FCurrentPeriod         : shortstring;    FListOfSort            : TStringList;    FListOfImport          : TStringList;  protected    procedure DoRun;  public    constructor Create(TheOwner: TComponent; Cf: string; Dl: Char);    destructor Destroy;     property DayLongStartTime      : shortstring read FDayLongStartTime write FDayLongStartTime;    property DayLongEndTime        : shortstring read FDayLongEndTime write FDayLongEndTime;    property DayLongMinBpm         : Byte read FDayLongMinBpm write FDayLongMinBpm;    property DayLongMaxBpm       : Byte read FDayLongMaxBpm write FDayLongMaxBpm;    property DayLongPath           : string read FDayLongPath write FDayLongPath;  property NightLongStartTime    : shortstring read FNightLongStartTime write FNightLongStartTime;    property NightLongEndTime      : shortstring read FNightLongEndTime write FNightLongEndTime;    property NightLongMinBpm       : Byte read FNightLongMinBpm write FNightLongMinBpm;    property NightLongMaxBpm       : Byte read FNightLongMaxBpm write FNightLongMaxBpm;    property NightLongPath       : string read FNightLongPath write FNightLongPath;    property FileType              : shortstring read FFileType write FFileType;  property LastDirectionOfSort   : shortstring read FLastDirectionOfSort write FLastDirectionOfSort;    property DayLongParams         : shortstring read FDayLongParams write FDayLongParams;    property NightLongParams       : shortstring read FNightLongParams write FNightLongParams;    property BasePath              : string read FBasePath write FBasePath;    property MinBpm                : Byte read FMinBpm;    property MaxBpm                : Byte read FMaxBpm;  property LastPeriod            : shortstring read FLastPeriod write FLastPeriod;     property NameOfIndex: shortstring read FNameOfIndex write FNameOfIndex;    property ConfigFile: string read FConfigFile write FConfigFile;    property Delimiter: Char read FDelimiter write FDelimiter;    property CurrentDir: string read FCurrentDir write FCurrentDir;    property CurrentPeriod: shortstring read FCurrentPeriod;    property ListOfSort: TStringList read FListOfSort write FListOfSort;    procedure WriteConfig(Prop: shortstring; Val: string);    function ReadConfig(Req: shortstring): string;    function TrimLeadingZeros(const S: string): string;    function StrToMinutes(S: string): Integer;  end; implementation... How can I make it accessible by other units and execute once? I want to execute it once because it reads / saves settings and I don't want to do this multiple times. ---------------------------------------------------------------------- dseligo · 2025-04-19 13:36 Maybe something like this: 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     unit Config;     ...         type      {TConfig}      PConfig = ^TConfig;    TConfig = class      private...      end; var MyConfig: TConfig;      implementation    ...initialization  MyConfig := TConfig.Create; finalization  MyConfig.Free;   And now you can use MyConfig in your other units. ---------------------------------------------------------------------- cdbc · 2025-04-19 14:01 Hi edit dseligo beat me to it... Simplest solution is to make it a Poorman's 'Singleton', implement it in the implementation section of your unit and access it through a Singleton-function, that returns the same class over and over again, but only creates it once on first use. On program stop, you free it in the unit's 'finalization' section... Something like this: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 unit singleton.config; {$mode ObjFPC}{$H+}{$m+}{$TYPEDADDRESS ON}interface uses  Classes, SysUtils, StrUtils,  Token, PathsHandler; type  {TConfig}  PConfig = ^TConfig;  TConfig = class  private    FDayLongStartTime      : shortstring;    FDayLongEndTime      : shortstring;    FDayLongMinBpm         : Byte;    FDayLongMaxBpm         : Byte;    FDayLongPath           : string;    FNightLongStartTime    : shortstring;    FNightLongEndTime      : shortstring;    FNightLongMinBpm       : Byte;    FNightLongMaxBpm     : Byte;    FNightLongPath         : string;    FFileType            : shortstring;    FLastDirectionOfSort   : shortstring;    FDayLongParams         : shortstring;    FNightLongParams       : shortstring;    FBasePath              : string;    FMinBpm              : Byte;    FMaxBpm                : Byte;    FLastPeriod          : shortstring;     FNameOfIndex           : shortstring;    FConfigFile            : string;    FDelimiter             : Char;    FCurrentDir            : string;    FCurrentPeriod         : shortstring;    FListOfSort            : TStringList;    FListOfImport          : TStringList;  protected    procedure DoRun;  public    constructor Create(TheOwner: TComponent; Cf: string; Dl: Char);    destructor Destroy;     property DayLongStartTime      : shortstring read FDayLongStartTime write FDayLongStartTime;    property DayLongEndTime        : shortstring read FDayLongEndTime write FDayLongEndTime;    property DayLongMinBpm         : Byte read FDayLongMinBpm write FDayLongMinBpm;    property DayLongMaxBpm       : Byte read FDayLongMaxBpm write FDayLongMaxBpm;    property DayLongPath           : string read FDayLongPath write FDayLongPath;  property NightLongStartTime    : shortstring read FNightLongStartTime write FNightLongStartTime;    property NightLongEndTime      : shortstring read FNightLongEndTime write FNightLongEndTime;    property NightLongMinBpm       : Byte read FNightLongMinBpm write FNightLongMinBpm;    property NightLongMaxBpm       : Byte read FNightLongMaxBpm write FNightLongMaxBpm;    property NightLongPath       : string read FNightLongPath write FNightLongPath;    property FileType              : shortstring read FFileType write FFileType;  property LastDirectionOfSort   : shortstring read FLastDirectionOfSort write FLastDirectionOfSort;    property DayLongParams         : shortstring read FDayLongParams write FDayLongParams;    property NightLongParams       : shortstring read FNightLongParams write FNightLongParams;    property BasePath              : string read FBasePath write FBasePath;    property MinBpm                : Byte read FMinBpm;    property MaxBpm                : Byte read FMaxBpm;  property LastPeriod            : shortstring read FLastPeriod write FLastPeriod;     property NameOfIndex: shortstring read FNameOfIndex write FNameOfIndex;    property ConfigFile: string read FConfigFile write FConfigFile;    property Delimiter: Char read FDelimiter write FDelimiter;    property CurrentDir: string read FCurrentDir write FCurrentDir;    property CurrentPeriod: shortstring read FCurrentPeriod;    property ListOfSort: TStringList read FListOfSort write FListOfSort;    procedure WriteConfig(Prop: shortstring; Val: string);    function ReadConfig(Req: shortstring): string;    function TrimLeadingZeros(const S: string): string;    function StrToMinutes(S: string): Integer;  end; { Singleton-function provides access to 1 TConfig instance, you need only call  it once with parameters, after that just call it without 'cause they're all default }function gConfig(TheOwner: TComponent = nil; Cf: string = ''; Dl: Char = ' '): TConfig; implementation var Singleton: TConfig = nil;                                                function gConfig(TheOwner: TComponent = nil; Cf: string = ''; Dl: Char = ' '): TConfig;begin  if not Assigned(Singleton) then Singleton:= TConfig.Create(TheOwner,Cf,Dl);  Result:= Singleton;end; finalization  if Assigned(Singleton) then Singleton.Free; end. On program startup you call the 'gConfig(Self,'Somestring',aChar);' once with parameters, after that you can just use it as/like a variable 'gConfig.CurrentDir:= /home/you/tmp/'... Innit cool  HTH Regards Benny ---------------------------------------------------------------------- Thaddy · 2025-04-19 14:19 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   {TConfig}  PConfig = ^TConfig;  TConfig = classTConfig is already a pointer. don't f*ck up your code by using ppointers.... Such constructs are only relevant for records, not for classes.  Then a solution: Use the initialization section of the class's unit. Instantiate the class and make sure the variable is public. ---------------------------------------------------------------------- nikel · 2025-04-19 14:24 Thaddy, thanks for the hint. I was in doubt about it. ---------------------------------------------------------------------- Thaddy · 2025-04-19 14:25 I added a solution. ---------------------------------------------------------------------- nikel · 2025-04-19 15:09 dseligo, cdbc, your mysterious code works fine. Thanks a lot. You're all great programmers.