http://delphi.about.com/od/adptips2006/qt/functionasparam.htm

In Delphi, procedural types (method pointers) allow you to treat procedures and functions as values that can be assigned to variables or passed to other procedures and functions.

Here's how to call a function (or procedure) as a parameter of another function (or procedure) :

  1. Declare the function (or procedure) that will be used as a parameter. In the example below, this is "TFunctionParameter".
  2. Define a function that will accept another function as a parameter. In the example below this is "DynamicFunction"
type
TFunctionParameter = function(const value : integer) : string;
...
function One(const value : integer) : string;
begin
result := IntToStr(value) ;
end; function Two(const value : integer) : string;
begin
result := IntToStr( * value) ;
end; function DynamicFunction(f : TFunctionParameter) : string;
begin
result := f() ;
end;
...
//Example usage:
var s : string;
begin
s := DynamicFunction(One) ;
ShowMessage(s) ; //will display ""
s := DynamicFunction(Two) ;
ShowMessage(s) ; // will display ""
end;

Note:

  • Of course, you decide on the signature of the "TFunctionParameter": whether it is a procedure or a function, how many parameters does it take, etc.
  • If "TFunctionParameter" is a method (of an instance object) you need to add the words of object to the procedural type name, as in:
    TFunctionParameter = function(const value : integer) : string of object;
  • If you expect "nil" to be specified as the "f" parameter, you should test for this using the Assigned function.
  • Fixing the "Incompatible type: 'method pointer and regular procedure'"

How to Use a Function or a Procedure as a Parameter in another Function的更多相关文章

  1. JS function document.onclick(){}报错Syntax error on token "function", delete this token

    JS function document.onclick(){}报错Syntax error on token "function", delete this token func ...

  2. Qt error ------ no matching function for call to QObject::connect(QSpinBox*&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::*)(int))

    connect(ui->spinBox_luminosity,&QSpinBox::valueChanged, ui->horizontalSlider_luminosity, & ...

  3. JS function document.onclick(){}报错Syntax error on token "function", delete this token - CSDN博客

    原文:JS function document.onclick(){}报错Syntax error on token "function", delete this token - ...

  4. [login] 调用失败 Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID , cloud function service error code -501000, error message Environment not found;

    按照微信开放文档,创建完云开发项目,运行,点击获取openid,报如下错: [login] 调用失败 Error: errCode: -404011 cloud function execution ...

  5. 在子jsp页面中调用父jsp中的function或父jsp调用子页面中的function

    项目场景: A.jsp中有一个window,window里嵌入了一个<iframe>,通过<iframe>引入了另一个页面B.jsp.在B.jsp中的一个function中需要 ...

  6. JS面试Q&A(续2): Rest parameter,Arrow function 等

    rest parameter 和 Destructuring assignment. function fun1(...theArgs) { console.log(theArgs.length);} ...

  7. (转)D3d9c的固定渲染管道(fixed function pipeline)与可编程管道(programmable function pipeline)的异同点

    转自:http://blog.csdn.net/tspatial_thunder/article/details/5937701 现在的游戏图形部分越来多依靠GPU来渲染绘制.说起GPU先说着色器,着 ...

  8. Why we need interfaces in Delphi

    http://sergworks.wordpress.com/2011/12/08/why-we-need-interfaces-in-delphi/ Why we need interfaces i ...

  9. Super Object Toolkit (支持排序)

    (* * Super Object Toolkit * * Usage allowed under the restrictions of the Lesser GNU General Public ...

随机推荐

  1. ADB Offline

    终极可能原因:版本太旧 http://stackoverflow.com/questions/14993855/android-adb-device-offline-cant-issue-comman ...

  2. 虚拟机VMware tools作用以及其安装

    虚拟机VMware tools的作用(1). 更新虚拟机中的显卡驱动, 使虚拟机中的XWindows可以运行在SVGA模式下.在客户操作系统中安装Mware Tools非常重要.如果不安装VMware ...

  3. 理解Mysql的索引与优化

    转自:http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库 ...

  4. 13、NFC技术:读写非NDEF格式的数据

    MifareUltralight数据格式 将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位).页码从0开始(0至15).前4页(0至3)存储了NFC标签相关 ...

  5. cocos2dx 水波纹Shader

    // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////// ...

  6. char型指针与其它指针或数组的细节

    一道常见题 char * str7="abc"; char * str8="abc"; cout<<(str7==str8)<<endl ...

  7. python程序中自启动appium服务

    普通启动Appium服务方法:      打开cmd,运行命令: #>appium -a 127.0.0.1 -p 4723 当程序输出如上图信息的时候,表示appium启动成功,此时便可以运行 ...

  8. ansibleplaybook的使用

    1.简单格式要求 [root@ansibleserver ansible]# cat nagios.yml --- - hosts: nagiosserver tasks: - name: ensur ...

  9. oracle 数据库远程导出

    exp 用户名/密码@IP:端口/数据库名 file=文件路径 full=y; exp scebm1/ebm@10.3.10.16:1521/scebm file=D:scebm20140527.dm ...

  10. Pregel: A System for Large-Scale Graph Processing(译)

    [说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...