【问题标题】:Delphi: trouble calling function returning interfaceDelphi:调用函数返回接口时出错
【发布时间】:2010-06-28 13:53:39
【问题描述】:

我正在尝试调用从另一个单元返回接口的函数;例如考虑以下内容:

program intf_sb1;
{$APPTYPE CONSOLE}
uses
  myunit in 'myunit.pas';
var
  MyBL: ISomeInterface;
begin
  MyBL := GetInterface;
end.

其中myunit.pas的内容如下:

unit myunit;
interface
type
  ISomeInterface = interface
    ['{D25A26ED-7665-4091-9B0F-24DF37545E2A}']
  end;
implementation

function GetInterface : ISomeInterface;
begin
end;

end.

我的问题是当我尝试运行此程序时收到错误“E2003 Undecleared identifier GetInterface”。为什么不允许这样做?提前致谢!

【问题讨论】:

  • 建议使用除“GetInterface”之外的其他名称,因为这是每个 TObject 中的预定义方法。
  • 调解员,您的问题措辞好像问题的根源与函数返回接口类型的事实有关。在得出这个结论之前,你应该自己做一些调查。如果这确实是问题所在,那么您应该能够更改返回类型 以使函数正常工作。你显然没有尝试过。不要轻易放弃;它让你看起来懒惰
  • Rob:确实如此。我为明显的懒惰道歉;我想我只是还不习惯 Delphi,在接口和实现部分拆分代码。 Uwe:谢谢,这是一个很好的观点。

标签: delphi function interface


【解决方案1】:

还要在接口部分声明 GetInterface 函数。如果您不这样做,则它是该单元的“私有”。

IE:

type
  ISomeInterface
  ...
  end;

  function GetInterface: ISomeInterface;

implementation

function GetInterface: ISomeInterface;
begin
...
end;

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 2011-01-17
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多