Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 518
Salvador Avatar asked Jul 06 '11 21:07

Salvador


1 Answers

Typecast the nil to a TFooProc:

DoSomething(Param1, TFooProc(nil));
like image 78
Sertac Akyuz Avatar answered Oct 13 '22 21:10

Sertac Akyuz