Recent

Author Topic: Use an object in all units.  (Read 734 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Use an object in all units.
« on: September 18, 2023, 04:06:25 pm »
I have a Logging unit with a TLofFile class. In the main form (TFrmMain) I add Logging to uses. In the public part of TFrmMain = class(TForm) I put


Code: Pascal  [Select][+][-]
  1.   public
  2.     Logging : TLogFile;


Within the main form I can use Logging directly, but if I create a new unit and want to use logging there too, I have to put the main form in the uses and then I can use logging using mainform.Logging.<some procedure>. So in all units I have to add the main form to uses to access Logging. That could certainly be done better.
How can I access the Logging created in the main form in the individual units without adding the main form to the uses? Or where can i create the logging object outzide the main form so all units can access it?

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Use an object in all units.
« Reply #1 on: September 18, 2023, 04:36:26 pm »
Depends on what/how you log. If your log functions themselves do not use any visual components then you can store it in a separate unit. I use that myself in a simplelog unit that has all the log functions in it. As an alternative you could use a datamodule but that requires you to use the name of the instance of the datamodule class.

It becomes a tad more difficult if you use visual components but that depends on what visual components it depends.


As an example something like:
Code: Pascal  [Select][+][-]
  1. unit Hansvblog;
  2.  
  3. interface
  4.  
  5. type
  6.   TLogfile = ....
  7.  
  8. var
  9.   Logging : TLogfile;
  10.  
  11. implementation
  12.  
  13. procedure TLogfile.Foo;
  14. begin
  15.   < code of function >
  16. end;
  17.  
  18. function TLogFile.Bar;
  19. begin
  20.   < code of function >
  21. end.
  22.  
  23. initialization
  24.   Logging := TLogFile.Create;
  25.  
  26. finalization
  27.   Logging.Free;
  28.  
  29. end.
  30.  
« Last Edit: September 18, 2023, 04:41:51 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Use an object in all units.
« Reply #2 on: September 18, 2023, 04:42:12 pm »
Thera are no visual components in the log unit. And it does not use other units.

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Use an object in all units.
« Reply #3 on: September 18, 2023, 04:44:33 pm »
Thera are no visual components in the log unit. And it does not use other units.
In that case the example as shown should be enough to fulfill your needs. If there are more questions on how the example works then feel free to ask.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Use an object in all units.
« Reply #4 on: September 19, 2023, 06:12:24 pm »
The example works. I never knew that is was so easy.
I was searching for more documentation of initialization. The only link i found is:

Quote
https://wiki.lazarus.freepascal.org/Initialization


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: Use an object in all units.
« Reply #5 on: September 19, 2023, 06:27:00 pm »
FPC parameter -Fa ?

 

TinyPortal © 2005-2018