Lazarus

Programming => General => Topic started by: loaded on February 26, 2021, 08:53:40 pm

Title: [Solved] Accessing variables within the lpr file
Post by: loaded on February 26, 2021, 08:53:40 pm
Hi All,
I'm trying to create a library file,
From the main library file (.lpr), I can access the variables within the units.
Code: Pascal  [Select][+][-]
  1. library Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, unit1;
  7.  
  8. {$R *.res}
  9.  var
  10.    library_variable:integer;
  11.  
  12. begin
  13.    unit_variable:=2021;
  14. end.
  15.  

But;
I cannot access the variables in the main library file (.lpr) from within the units.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.    Classes, SysUtils;
  9.  
  10.  var
  11.    unit_variable:integer;
  12.  
  13. implementation
  14.  
  15.   procedure acces_lpr_variable_test;
  16.   begin
  17.      library_variable:=1999;   // I want to access the variable from here.
  18.   end;
  19.  
  20. end.      
  21.  

What route should I follow to access the variable?
I would be very glad if anyone could help. Respects.
Title: Re: Accessing variables within the lpr file
Post by: 440bx on February 26, 2021, 10:28:18 pm
The "proper" way to make the "library_variable" accessible in other units is to have the "library_variable" definition in a separate unit which other units that want to have access to it use.  IOW, library Project1 and unit1 would both use "unit2" which is where "library_variable" would be defined.  IOW, put the variables that are shared in their own unit.

There is another way of making the "library_variable" accessible in unit1 without having to create another unit.  You can find that method in the thread https://forum.lazarus.freepascal.org/index.php/topic,45707.msg323530.html#msg323530

HTH.
Title: Re: Accessing variables within the lpr file
Post by: jamie on February 27, 2021, 12:02:51 am
known as  a PROXY unit,...

Make up a unit that contains all code and variables you need accessible from all units, including the main...

Just include it in all requires units via the USES list
Title: Re: Accessing variables within the lpr file
Post by: Martin_fr on February 27, 2021, 12:45:01 am
https://forum.lazarus.freepascal.org/index.php/topic,53408.0.html
Title: Re: Accessing variables within the lpr file
Post by: loaded on February 27, 2021, 08:32:33 am
440bx and jamie and Martin_fr thank you individually.
I had seen the links before but could not understand them.
I understand very well now. Thanks.
Title: Re: Accessing variables within the lpr file
Post by: ASBzone on February 28, 2021, 05:03:05 am
There is another way of making the "library_variable" accessible in unit1 without having to create another unit.  You can find that method in the thread https://forum.lazarus.freepascal.org/index.php/topic,45707.msg323530.html#msg323530 (https://forum.lazarus.freepascal.org/index.php/topic,45707.msg323530.html#msg323530)

HTH.

I see what you did there...  :D
TinyPortal © 2005-2018