[DUG]Scope
Conor Boyd
Conor.Boyd at trimble.co.nz
Fri Feb 10 10:16:47 NZDT 2006
John,
That's because tbData is a field in a class called TForm1.
You'll only be able to access tbData if you have an object of type
TForm1 to play with. You probably have an object of that type assigned
to the var Form1 (or similar) which is declared just below the TForm1
class declaration.
This is OO stuff, and is slightly different from the more procedural
scope of simple functions in different units.
Without seeing more of your code, I think you'd be better if your
general printing routine gets passed a TClientDataSet object which it
uses, rather than ferreting about in a form's innards.
HTH,
Conor
-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz]
On Behalf Of John Bird
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).
More information about the Delphi
mailing list