Recent

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

loaded

  • Hero Member
  • *****
  • Posts: 824
[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 »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

440bx

  • Hero Member
  • *****
  • Posts: 3945
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) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6090
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: 9793
  • Debugger - SynEdit - and more
    • wiki

loaded

  • Hero Member
  • *****
  • Posts: 824
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.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • 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.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

 

TinyPortal © 2005-2018