访问 Delphi 中的其他单位常量
Accessing other unit constant in Delphi
我在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.
Unit1:
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.
当我尝试构建这个项目时,我遇到了这样的错误:
[Error] Unit1.pas(41): Undeclared identifier: 'SomeArray'
这段代码有什么问题?我检查了 Delphi wiki 和其他问题,但没有找到解决此问题的方法...
您需要在单元的 interface
部分定义 SomeArray
。目前,您将它放在 implementation
部分,这是故意对其他单位隐藏的。只有 interface
中的 defined/declared 对其他单位可见。
在您链接的文档中,描述了:
The implementation section of a unit begins with the reserved word implementation and continues until the beginning of the initialization section or, if there is no initialization section, until the end of the unit. The implementation section defines procedures and functions that are declared in the interface section. Within the implementation section, these procedures and functions may be defined and called in any order. You can omit parameter lists from public procedure and function headings when you define them in the implementation section; but if you include a parameter list, it must match the declaration in the interface section exactly.
In addition to definitions of public procedures and functions, the implementation section can declare constants, types (including classes), variables, procedures, and functions that are private to the unit. That is, unlike the interface section, entities declared in the implementation section are inaccessible to other units.
(强调我的)
我在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.
Unit1:
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.
当我尝试构建这个项目时,我遇到了这样的错误:
[Error] Unit1.pas(41): Undeclared identifier: 'SomeArray'
这段代码有什么问题?我检查了 Delphi wiki 和其他问题,但没有找到解决此问题的方法...
您需要在单元的 interface
部分定义 SomeArray
。目前,您将它放在 implementation
部分,这是故意对其他单位隐藏的。只有 interface
中的 defined/declared 对其他单位可见。
在您链接的文档中,描述了:
The implementation section of a unit begins with the reserved word implementation and continues until the beginning of the initialization section or, if there is no initialization section, until the end of the unit. The implementation section defines procedures and functions that are declared in the interface section. Within the implementation section, these procedures and functions may be defined and called in any order. You can omit parameter lists from public procedure and function headings when you define them in the implementation section; but if you include a parameter list, it must match the declaration in the interface section exactly.
In addition to definitions of public procedures and functions, the implementation section can declare constants, types (including classes), variables, procedures, and functions that are private to the unit. That is, unlike the interface section, entities declared in the implementation section are inaccessible to other units.
(强调我的)