Define class in unit without other units being capable lazarus / 21-General ---------------------------------------------------------------------- wildfire · 2023-03-02 18:31 Hi guys and gals, Ok I'm cutting to the quik, here's what I want to do... Can I create a class in a unit without any other unit or program being able to create another instance of that class? The Idea is to create the instance in the initialization section and destroy it in the finalization section. However I don't want other units to be able to create other instances of the class, as far as they are concerned "uses uExample" then an instance is available... e.g. Code: [Select]type   tExample: class          // I don't want other units to have the ability to create tExample.     public       msg: string   end;   var     MyExample: tExample;       // I do want other units to have access to MyExample. I have thought about a forward declaration to define the class in the implementation section or to put the constructor in the private defination of the class but OOP is fairly new to me so I thought, best ask the experts... What would you do ---------------------------------------------------------------------- Thaddy · 2023-03-02 19:01 When you only want to use that class in only that unit, you are on the right track: Just declare it in the implementation section. If you want to use that class instance from other units it gets more complex. ---------------------------------------------------------------------- Blaazen · 2023-03-02 19:13 It's called singleton. Here's ten years old topic: https://forum.lazarus.freepascal.org/index.php/topic,19761.0.html - the first post. If someone will try to create a new instance, he will get already existing instance (if exists). ---------------------------------------------------------------------- cdbc · 2023-03-02 19:37 Hi I would declare an interface in the interface section and then implement it in the implementation section: 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 interfaceconst SGUIDIExample = '{CFB68445-3868-46CA-8E02-00266B891710}';type{$interfaces corba} { NO reference counting! }   IExample = interface[SGUIDIExample]      function get_msg: string;    procedure set_msg(aValue: string);    property msg: string read get_msg write set_msg;  end;{$interfaces com} { reference counted! } var    MyExample: IExample;implementationtype  TExample = class(TObject,IExample)  private    fmsg: string;    function get_msg: string;     procedure set_msg(aValue: string);   public    property msg: string read get_msg write set_msg;  end; var LocExample: TExample; function TExample.get_msg: string;begin  result:= fmsg;end; procedure TExample.set_msg(aValue: string);begin  fmsg:= aValue;end; initialization  LocExample:= TExample.Create;  LocExample.GetInterface(SGUIDIExample,MyExample); finalization   LocExample.Free;end. Something along these lines... (Untested code) Hth - Regards Benny ---------------------------------------------------------------------- Thaddy · 2023-03-02 20:18 Quote from: Blaazen on March 02, 2023, 08:13:30 pmIt's called singleton. Here's ten years old topic: https://forum.lazarus.freepascal.org/index.php/topic,19761.0.html - the first post. If someone will try to create a new instance, he will get already existing instance (if exists). It is NOT called a singleton if the class can be instanciated in the same unit more than once(eg. in a loop) A singleton is a completely different pattern, although OP might not have his theory right. Comes to mind the old Delphi grids.pas where that happens all the time. (multiple local instances of a cell object) Quote from: cdbc on March 02, 2023, 08:37:12 pmHi I would declare an interface in the interface section and then implement it in the implementation section: Something along these lines... (Untested code) Hth - Regards Benny That won't work, since you can not instanciate the class. Better test it.. What IS possible is to declare a variable as an interface in the interface section and initialize it. You can't inherit from that easily. But you probably end up with an interface that needs to implement every public method in the class! And as/is is not possible, since you can not reference the class. ---------------------------------------------------------------------- cdbc · 2023-03-02 21:18 Hi @Thaddy: You're right, it's best to test, so i did. Here's a little something i cooked up for you... Hth - Regards Benny ---------------------------------------------------------------------- wildfire · 2023-03-03 07:34 Thanks guys, I'll take some time to digest the information supplied, I'm sure a solution is in there somewhere. Thanks again and if anyone has any further input I'm all ears. ---------------------------------------------------------------------- Thaddy · 2023-03-03 09:18 Quote from: cdbc on March 02, 2023, 10:18:04 pmHi @Thaddy: You're right, it's best to test, so i did. Here's a little something i cooked up for you... Hth - Regards Benny Thaddy is right, because what you do is a copy operation to a variable in the interface section. That is not only cheating, it is plain wrong if you want create a class instance from an interface reference. Your code works only by accident. My point is, stupid CORBA or not, you can't do this: Code: [Select]unit stupid; {$mode objfpc}{$interfaces CORBA} interface type   IMyInterface:IInterface; var    MyInterface: TMyInterface; implementation    TMyClass = class(Tobject,IMyInterface); end.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 Program stupiddemo;{$mode objfpc}{$interfaces CORBA}uses stupid;var  SomeInterface:TMyInterface;begin  SomeInterface := TMyClass.Create as TMyInterface; // error  SomeInterface := MyInterface;  writeln((SomeInterface as TMyClass).Classname);  // BOOMend. If you want to cheat, plz do so, but actually you are indirectly exposing information that should stay hidden as per the question.