开发者

How to passing a nil value in a in parameter of procedure of object type

I want to pass a nil value in a parameter which is declarated as procedure of object

Consider this code

Case 1

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


procedure 开发者_JS百科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;

Case 2

Ì found, if I declare TFooProc as procedure type the code is compiled. (but in my case I need a procedure of object type)

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;

Case 3

Also I discover which if remove the overload directive the code compiles fine

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;

The question is How i can pass the nil value as parameter? to work with the code in the case 1?


Typecast the nil to a TFooProc:

DoSomething(Param1, TFooProc(nil));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜