加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何在对象类型的过程的in参数中传递nil值

发布时间:2020-12-15 04:11:58 所属栏目:大数据 来源:网络整理
导读:我想在参数中传递一个nil值,该参数被声明为object的过程 考虑这段代码 情况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 a
我想在参数中传递一个nil值,该参数被声明为object的过程

考虑这段代码

情况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声明为过程类型,则编译代码. (但在我的情况下,我需要一个对象类型的过程)

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


procedure DoSomething(Param1:Integer;Foo:TFooProc);overload;
var
  a,b);
end;

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

案例3

另外我发现如果删除过载指令,代码编译好了

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


procedure DoSomething(Param1:Integer;Foo:TFooProc);
var
  a,b);
end;

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

问题是如何将nil值作为参数传递?使用案例1中的代码?

解决方法

对TFooProc进行类型测试:
DoSomething(Param1,TFooProc(nil));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读