如何在对象类型过程的 in 参数中传递 nil 值
我想在声明为对象过程的参数中传递一个 nil 值
考虑此代码
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
Ì 发现,如果我将 TFooProc
声明为 procedure
类型代码被编译。 (但在我的例子中,我需要一个对象的过程
类型)
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;
问题是我如何可以传递 nil 值作为参数吗?
来使用案例 1 中的代码?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 nil 类型转换为 TFooProc:
Typecast the nil to a TFooProc: