[英]Error: “Undeclared identifier” using a form from another Unit - Delphi 2010
I'm trying to use a Form from another Unit, but the code isn't recognizing the other Unit. 我正在尝试使用另一个单元的表单,但是代码无法识别另一个单元。
Example: 例:
unit uImpressao;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
uniGUIClasses, uniGUIForm, uniGUIBaseClasses, uniPanel, uniURLFrame;
type
TfImpressao = class(TUniForm)
ufRelatorio: TUniURLFrame;
UniImage1: TUniImage;
procedure UniImage1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
uses
MainModule, Main, uBancoHoras;
procedure TfImpressao.UniImage1Click(Sender: TObject);
begin
fBh.iTeste.Visible := false;
end;
end.
unit uBancoHoras;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
uniGUIClasses, uniGUIForm, uniLabel, pngimage, uniImage, uniGUIBaseClasses,
uniPanel, uniPageControl, uniButton, uniBitBtn, uniSpeedButton, uniCanvas,
uniDateTimePicker, uniMemo, uniMultiItem, uniComboBox, uniEdit, uniBasicGrid,
uniDBGrid, uniDBMemo, uniRadioButton, uniDBText, uniRadioGroup, frxClass,
frxDBSet;
type
TfBH = class(TUniForm)
iTeste : TUniImage;
private
{ Private declarations }
public
{ Public declarations }
end;
var
url: string;
function fBH: TfBH;
implementation
{$R *.dfm}
uses
MainModule, Main, uImpressao;
function fBH: TfBH;
begin
Result := TfBH(MM.GetFormInstance(TfBH));
end;
procedure TfBH.iTesteClick(Sender: TObject);
begin
fImpressao.ShowModal;
end;
When I try using the uImpressao unit in uBancohoras unit, returns the error "Undeclared identifier 'fImpressao'". 当我尝试在uBancohoras单元中使用uImpressao单元时,返回错误“未声明的标识符'fImpressao'”。 Using uBancoHoras unit in the uImpressao unit, works fine.
在uImpressao单元中使用uBancoHoras单元,效果很好。
I don't understand why this error is happening with one unit, but not with the other. 我不明白为什么一个单元而不是另一个单元会发生此错误。
I hope you can help me! 我希望你能帮帮我!
Note : Please note that this answer was based on the original code posted in the question, which was subsequently replaced in its entirety with new and vastly different code. 注意 :请注意,此答案基于问题中发布的原始代码,随后该代码全部替换为新的和完全不同的代码。
fBH is not declared in unit A
,so fBH.iTeste.Visible := True;
fBH未在单元
A
声明,因此fBH.iTeste.Visible := True;
can't possibly work; 不可能工作; there is no such variable.
没有这样的变量。 You deleted the global variable
fBH
that the IDE creates for you (although, interestingly you left the var
statement above it right above the implementation
keyword). 您删除了IDE为您创建的全局变量
fBH
(尽管有趣的是,您将var
语句放在了它上面的implementation
关键字的正上方)。
Either add back in the declaration (by adding var fBH: TfBH;
between the end of the class declaration and the implementation
keyword), or create an instance of the form in unit B
when you need to use it and access it through the local variable from there. 重新添加声明(通过在类声明的末尾和
implementation
关键字之间添加var fBH: TfBH;
),或者在需要使用它并通过局部变量访问它时在单元B
创建表单的实例。从那里。
(Whichever direction you go, you never address a form using the variable from within that form's methods; use Self
instead. Don't use fBH.ShowModal
; use either Self.ShowModal
or ShowModal
instead.) (无论您沿哪个方向前进,都永远不要使用该表单方法中的变量来寻址该表单;请改用
Self
。不要使用fBH.ShowModal
;请改用Self.ShowModal
或ShowModal
。)
In uBancoHoras
you have defined 在
uBancoHoras
您已定义
function fBH: TfBH;
...
implementation
...
function fBH: TfBH;
begin
Result := TfBH(MM.GetFormInstance(TfBH));
end;
So you have defined a global function called fBH
that returns an instance of the TfBH
form class, seemingly through some sort of factory method (probably defined in MainModule
?). 因此,您已经定义了一个名为
fBH
的全局函数,该TfBH
似乎是通过某种工厂方法(可能在MainModule
定义的)返回TfBH
表单类的实例。
There is no corresponding method or variable in uImpressao
with the name fImpressao
, however - the the compiler error that fImpressao
is an undeclared identifier . uImpressao
中没有名为fImpressao
相应方法或变量,但是fImpressao
是未声明的标识符的编译器错误。
Assuming that MM.GetFormInstance
is suited to the task, and also assuming you wish to keep this design pattern, you would have to define (in uImpressao
)something like : 假设
MM.GetFormInstance
适合该任务,并且还假设您希望保留此设计模式,则必须定义(在uImpressao
)类似以下内容:
function fImpressao: TfImpressao;
...
implementation
...
function fImpressao : TfImpressao;
begin
Result := TfImpressao(MM.GetFormInstance(TfImpressao));
end;
We can't see the implementation details of MM.GetFormInstance
, however, so there is no guarantee that this will work - it only follows the pattern that fBH
has set. 但是,我们看不到
MM.GetFormInstance
的实现细节,因此不能保证它会起作用-它仅遵循fBH
设置的模式。 Agreed with Ken that you should perhaps consider a better way to manage your forms. 与Ken达成协议,您也许应该考虑一种更好的方式来管理表格。 Resorting to global variables or global methods that reach across units to dig up a class instance feels like a headache waiting to happen...
诉诸于跨单元遍历的全局变量或全局方法来挖掘一个类实例感觉就像是在等待着头疼...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.