在Delphi中将过程作为参数传递 [英] Passing procedure as a parameter in Delphi
问题描述
我有此代码>
type
TCallBack = procedure(APerc: Integer) of object;
....
procedure CallingProcedure(a, b, c: Integer; ACallBack: TCallBack = nil);
....
我从主类/对象中调用此函数,如下所示:
I call this function from my main class/object like this:
CallingProcedure(1, 2, 3, DoOnCallBack);
其中DoOnCallBack定义为:
where DoOnCallBack is defined as:
procedure DoOnCallBack(APerc: Integer);
这编译很好,这没问题-我做了无数次.
This compiles good, it's not problem - I did this tons of times.
但是在CallingProcedure中,当我想检查Assigned(ACallBack)是否为False时.
But in CallingProcedure when I want to check if Assigned(ACallBack) I'm getting False.
有人可以告诉我我做错了吗
Can someone tell me what I'm doing wrong.
我是从Thread调用的,这可能是个问题吗?
I'm calling this from Thread, is that might be a problem?
推荐答案
我与您所说的地方没有任何区别.语法是一样的.我刚刚做了这个小测试示例:
I makes no diffrence from where you call it. The syntax is the same. I've just made this small test example :
type
TCallBack = procedure(APerc: Integer) of object;
TForm20 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure CallingProcedure(a, b, c: Integer; ACallBack: TCallBack = nil);
procedure DoOnCallBack(APerc: Integer);
public
{ Public declarations }
end;
var
Form20: TForm20;
implementation
{$R *.dfm}
procedure TForm20.CallingProcedure(a, b, c: Integer; ACallBack: TCallBack);
begin
if Assigned(ACallBack) then
ACallBack(a);
end;
procedure TForm20.DoOnCallBack(APerc: Integer);
begin
ShowMessage(IntToStr(APerc));
end;
procedure TForm20.FormCreate(Sender: TObject);
begin
CallingProcedure(1, 2, 3, DoOnCallBack);
CallingProcedure(1, 2, 3, nil);
end;
它工作正常.请您编写代码,使其看起来像这样.由于您尚未发布您的真实代码,所以我只能猜测问题出在哪里.
And it works just fine. Ajust you code to look like this. Since you haven't posted your real code I can only guess what the problem is.
这篇关于在Delphi中将过程作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!