How to pass nil value in object type procedure parameter

I want to pass the nil value to a parameter that is declared as a procedure of object

Consider this code

Case 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; 

Case 2

Ì found, if I declare TFooProc as procedure , the code compiles. (but in my case I need the type of 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; 

Case 3

I also find that if I 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; 

Question: How i can pass the nil value as parameter? work with code in case 1?

+7
source share
1 answer

Typecast nil for TFooProc:

 DoSomething(Param1, TFooProc(nil)); 
+11
source

All Articles