Recent

Author Topic: [Solved] Accessing variables within the lpr file  (Read 531 times)

loaded

  • Full Member
  • ***
  • Posts: 184
[Solved] Accessing variables within the lpr file
« 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.
« Last Edit: February 27, 2021, 08:32:49 am by loaded »
Installing the spelling, google translate is used :)

440bx

  • Hero Member
  • *****
  • Posts: 2276
Re: Accessing variables within the lpr file
« Reply #1 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.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 4362
Re: Accessing variables within the lpr file
« Reply #2 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
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7029
  • Debugger - SynEdit - and more
    • wiki

loaded

  • Full Member
  • ***
  • Posts: 184
Re: Accessing variables within the lpr file
« Reply #4 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.
Installing the spelling, google translate is used :)

ASBzone

  • Hero Member
  • *****
  • Posts: 614
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Accessing variables within the lpr file
« Reply #5 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

HTH.

I see what you did there...  :D
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

 

TinyPortal © 2005-2018