如何从另一个单元运行过程?

10
这是一个有点新手问题,但我仍然无法弄清楚。我有一个名为main的单元,其中包含过程Discard()。现在我有另一个单元engine,我想从中运行main单元中的Discard()过程。我在engine.pasuses部分中引用了main。我尝试使用main.Discard()来调用该过程,但不起作用。我做错了什么?
2个回答

12

你需要在接口中添加过程的签名,像这样:

unit main;

interface

procedure Discard();

implementation

procedure Discard();
begin
//do whatever
end;

其他单元只能看到在接口部分列出的内容。


9
在“Main”单元中的“interface”部分声明Discard:

unit Main;

interface

uses ...

procedure Discard (...); // only the declaration, not the entire procedure

implementation

... // code

现在在“Engine”单元中,您需要将“Main”添加到“uses”部分。
uses Main, ...

现在你可以调用 Discard(...) 了。如果有多个 Discard(),你可以通过使用 Main.Discard() 显式调用 Discard()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接