在Delphi中访问其他单位常数
我在Delphi中有一个简单的项目:在Delphi中访问其他单位常数,delphi,undeclared-identifier,Delphi,Undeclared Identifier,我在Delphi中有一个简单的项目: program Project1; uses Forms, Unit2 in 'Unit2.pas', Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end. 第一单元: unit Unit1; interface u
program Project1;
uses
Forms,
Unit2 in 'Unit2.pas',
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
第一单元:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
function encodeData(var Data:array of Byte; var Size: Integer): Integer;
var
i: Intger
begin
...
for i := 1 to Size do
begin
Data[i] := Data[i] + unit2.SomeArray[i]
end;
...
Result := 0;
Exit;
end
...
第二个单位:
unit Unit2;
interface
implementation
const
SomeArray:Array [0..65000] of LongWord = (
...
);
end.
当我尝试构建此项目时,会出现如下错误:
[错误]Unit1.pas(41):未声明的标识符:“SomeArray”
这个代码怎么了?我检查了和其他问题,但没有找到此问题的解决方案…您需要在单元的
界面
部分定义SomeArray
。目前,它位于实现
部分,故意对其他单元隐藏。只有在界面中定义/声明的内容对其他单元可见
在您链接的文档中,描述如下:
单元的实现部分从保留字实现开始,一直持续到初始化部分开始,如果没有初始化部分,则一直持续到单元结束。实现部分定义了在接口部分中声明的过程和函数。在实施部分,这些程序和功能可以按任何顺序定义和调用。在“实现”部分中定义公共过程和函数标题时,可以忽略这些标题中的参数列表;但是,如果包含参数列表,它必须与接口部分中的声明完全匹配
除了公共过程和函数的定义外,实现部分还可以声明单元专用的常量、类型(包括类)、变量、过程和函数。也就是说,与interface部分不同,在implementation部分声明的实体对其他单元是不可访问的。
(我的重点)将CONST声明移到实现行的上方,接口行的下方。从一个单元访问另一个单元中的内容有无数的SO Q。你有没有试过看其中的任何一个,或者甚至阅读在线帮助?是的,我检查了另外10个问题…你有没有阅读这个问题并自己回答?在上面提到的问题中,我可以在哪里找到关于我的接口/实现错误的解释?初级Delphi,第一课:什么是接口和实现部分…谢谢,这非常有用!