* * *

Author Topic: Global declarations between units  (Read 379 times)

jbmckim

  • New member
  • *
  • Posts: 17
Global declarations between units
« on: May 19, 2017, 12:52:30 am »
I have a unit that does not contain a form, dedicated to global functions and variables.    I'm trying to access it from a Unit that contains a form.

The functions share nicely but I can't seem to get the right location/syntax in the global unit to "globalize" a TStringList variable.  When I try to call the variable in the target unit from the Form unit, the compile complains with:  "Error: Identifier not found "StrLst."  The variable I'm trying to make global, StrLst is denoted below by a string of ///////////////.  I've tried every combination of public/private declarations I can think of and they either leave me with a cryptic, "Expected ;..." or other.

Please be kind.  It's been a couple decades now since I last used any flavor of Pascal.  I've apparently gotten sloppy at declaring things pretty much where I want to use them.

Thanks in advance.

Code: Pascal  [Select]
  1. unit DevCommonUnit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.      function OpenDevices(VAR iNumDevices : integer) : boolean;
  7.      function GetSerialNumbers(SpecNumber : integer) : String;
  8.  
  9. implementation
  10.  
  11. uses
  12.   Classes, SysUtils, Dialogs;
  13.  
  14. type
  15.   DeviceTypes = (OO, Undefined);
  16.  
  17. const
  18.   DONE : Integer = 1;
  19.   //ERROR_NO_DEVICE : Integer = 2;
  20.   EPROM_RETURN : Integer = 17;
  21.  
  22. var
  23.  
  24.   ActiveDevice : STypes = OO;
  25.   StrLst : TStringList;//////////////////////////////////////////////////////////////////
  26.  
  27.   function get_formatted (iSpecNumber:Integer; errorcode:PInteger; VAR buffer:Double; buffer_length:Integer):Integer;
  28.            cdecl; external 'C:\windows\system32\devicedriver.dll';
  29.  
  30.   function get_formatted_length(iSpecNumber : Integer; errorcode:PInteger):Integer; cdecl;
  31.            external 'C:\windows\system32\devicedriver.dll';
  32.  
  33.   function open_Device(iWorking:Integer; errorcode:PInteger):Integer; cdecl;
  34.            external 'C:\windows\system32\devicedriver.dll';
  35.  
  36.   function get_serial_number(index : Integer; errorcode : PInteger; VAR buf : array of byte; buffer_length : integer) : Integer;
  37.            cdecl; external 'C:\windows\system32\devicedriver.dll';
  38.  
  39.   function OpenDevices(VAR iNumDevices : integer) : boolean;
  40.   var
  41.         DoneOrNot : integer = 0;
  42.         iWorking  : integer = 0;
  43.         errorCode : integer = 0;
  44.         i         : integer = 0;
  45.         strSerial : String = '';
  46.  
  47.   begin // OpenDevices
  48.    try
  49.  
  50.      try
  51.  
  52.        case ActiveDevice of
  53.  
  54.           OO:
  55.             while true do
  56.               begin
  57.                 DoneOrNot := open_Device(iWorking, @errorCode);
  58.                 If (DoneOrNot = DONE) Then
  59.                    Break
  60.                 Else
  61.                    Begin
  62.                       iWorking := iWorking + 1;
  63.                    end;
  64.  
  65.               end;
  66.  
  67.           Undefined: ;
  68.  
  69.        end;
  70.  
  71.      except
  72.         On E :Exception do begin
  73.         ShowMessage(E.Message);
  74.      end;
  75.  
  76.      end;
  77.  
  78.    finally
  79.  
  80.    end;
  81.  
  82.   iNumDevices := iWorking;
  83.   OpenDevices := TRUE;
  84.  
  85.   StrLst := TStringList.Create;
  86.  
  87.   for i := 1 to iNumDevices do
  88.   begin
  89.        strSerial := GetSerialNumbers(i);
  90.        StrLst.Add(strSerial);
  91.   end;
  92.  end; // OpenDevices    
  93. ...

molly

  • Hero Member
  • *****
  • Posts: 1676
Re: Global declarations between units
« Reply #1 on: May 19, 2017, 01:02:45 am »
variables that needs to be accessed globally are to be defined in the interface section of a unit.

TStringList is part of classes, so you need to defined that unit inside your uses clause of the interface section as well.

BTW: are you sure the compiler is complaining about that line ?

i read in line 24:
Code: [Select]
ActiveDevice : STypes = OO;
But where is type STypes declared ? Is it declared in classes, Sysutils or dialogs ?

« Last Edit: May 19, 2017, 01:10:04 am by molly »

HeavyUser

  • Full Member
  • ***
  • Posts: 118
Re: Global declarations between units
« Reply #2 on: May 19, 2017, 01:05:50 am »
I have a unit that does not contain a form, dedicated to global functions and variables.    I'm trying to access it from a Unit that contains a form.
The functions share nicely but I can't seem to get the right location/syntax in the global unit to "globalize" a TStringList variable.  When I try to call the variable in the target unit from the Form unit, the compile complains with:  "Error: Identifier not found "StrLst."  The variable I'm trying to make global, StrLst is denoted below by a string of ///////////////.  I've tried every combination of public/private declarations I can think of and they either leave me with a cryptic, "Expected ;..." or other.
A pascal unit is divided in to two portions
1) The interface part which you can thing of as the .h of C/C++. Anything declared in the interface part of a unit is accessible from other units that uses it.
2) The implementation part is considered private to the unit. nothing declared there can be accessed from outside units.
You have only functions declared in the interface of your unit, you need to move your global variables there too. Keep in mind that  in contrast with the functions that need to be declared both in interface and implementation section the variable must be declared only once in either the interface or the implementation section.

jbmckim

  • New member
  • *
  • Posts: 17
Re: Global declarations between units
« Reply #3 on: May 19, 2017, 08:00:17 am »
HeavyUser - That's it.  I'd tried moving the uses to the interface section once but your bit put me on to the fact that I hadn't commented out the implementation version...that is, it has to be in one place or the other.  It throws an error that really isn't germane to the line the error catches.  That but about the header file and "has to be in one or the other" got me deleting.

I recompiled and now I have the right problem.  The calling unit sees my TStringList but I'm not using it correctly  :P  Now I'll sort that!

Thanks again.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus