如何在对象类型的过程参数中传递一个nil值

时间:2011-07-06 21:16:26

标签: delphi delphi-2007 delphi-xe

我想在声明为procedure of object

的参数中传递一个nil值

考虑这段代码

<案例1
type
  TFooProc = procedure(Foo1, Foo2 : Integer) of object;


procedure DoSomething(Param1:Integer;Foo:TFooProc);overload;
var
  a, b : Integer;
begin
   a:=b*Param1;
   //If foo is assigned
   if @Foo<>nil then
    Foo(a, b);
end;

procedure DoSomething(Param1:Integer);overload;
begin      
  DoSomething(Param1,nil);//here the delphi compiler raise this message [DCC Error] E2250 There is no overloaded version of 'DoSomething' that can be called with these arguments
end;
<案例2

Ì发现,如果我将TFooProc声明为procedure类型,则编译代码。 (但在我的情况下,我需要一个procedure of object类型)

type
  TFooProc = procedure(Foo1, Foo2 : Integer);


procedure DoSomething(Param1:Integer;Foo:TFooProc);overload;
var
  a, b : Integer;
begin
   a:=b*Param1;
   //If foo is assigned
   if @Foo<>nil then
    Foo(a, b);
end;

procedure DoSomething(Param1:Integer);overload;
begin
  DoSomething(Param1,nil);
end;
<案例3

此外,我发现如果删除overload指令代码编译正确

type
  TFooProc = procedure(Foo1, Foo2 : Integer) of object;


procedure DoSomething(Param1:Integer;Foo:TFooProc);
var
  a, b : Integer;
begin
   a:=b*Param1;
   //If foo is assigned
   if @Foo<>nil then
    Foo(a, b);
end;

procedure DoSomething2(Param1:Integer);
begin
  DoSomething(Param1,nil);
end;

问题是How i can pass the nil value as parameter?使用案例1中的代码吗?

1 个答案:

答案 0 :(得分:12)

对TFooProc进行类型测试:

DoSomething(Param1, TFooProc(nil));