[DUG]Scope

John Bird johnkbird at paradise.net.nz
Fri Feb 10 09:58:03 NZDT 2006


This is my understanding of accessing variables and procedures and functions
in other units and forms:

Functions and Procedures I found summarised nicely in a post:

"From: James Getz <supporti... at borland.com>
Subject: Re: Calling a procedure in another form

  The answer to your question depends on where you declare the function.
If the function is declared as part of the form object, then you would use
form.function (it is a method of the form class at that point) to access
it.  If it's a freestanding function (not part of a class) then you would
access it as unit.function."

I have also found that if the other unit is in the uses clause, then a
freestanding function or procedure does not even need the unit name - the
compiler and linker can find it using the uses clause.  This is how I use a
unit full of library procedures and functions, with just the procedure or
function name.  Delphi also does this all the time, for example to find
FieldbyName is defined in Delphi 2006 hovering over this statement reveals
the tip "Declared in DB.TDataSet", and when using FieldbyName no unit prefix
is required.

Variables  - if declared in the interface section of say unit1, and unit2
has a "uses unit1"  statement then unit2 can see these global variables in
unit1.

QUESTION:

Now - I have moved some code from one unit to another and it won't compile

Unit1 declares in the interface section

type
  TForm1 = class(TForm)
    tbData: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;


Unit2 declares

implementation

{$R *.dfm}
uses Unit1,Unit3...etc;


But statements in unit2 such as 

	    tbData.First;
	    while not tbData.eof do
	    begin
            setfont('arial',8); 
	      printtab(tbData.fieldbyname('Field1').asString);
	      printtab(tbData.fieldbyname('Field2').asString);
 	      printtab(tbData.fieldbyname('Field3').asString);
 

Are all giving errors about tbdata saying undeclared identifier and won't
compile.  I am thinking Unit2 should be able to see tbData in Unit1.
(I am trying to put some Rave Report code into a separate general printing
routine so I can re-use it).

John




More information about the Delphi mailing list