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. 配置php支持curl

    curl是一个利用URL语法在命令行方式下工作的文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP.cu ...

  2. every day english

    job is in your freedom, not your compliance. through no fault of his own. as far as I understand you ...

  3. hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

    废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...

  4. rand.Read() 和 io.ReadFull(rand.Reader) 的区别?

    golang的随机包 rand.go 中我们可以看到 rand.Read 其实是调用的io.Reader.Read() 1: // Package rand implements a cryptogr ...

  5. 用python3破解wingIDE

    值得注意的是,python2的整除/在python3中变成了//,sha方法细化成了sha1和sha256,所以破解文件需要更改加密方式和整除部分的编码方式,经过修改后,这个文件可以完美演算出破解码, ...

  6. Javascript兼容和CSS兼容总结

    javascript部分 1. document.form.item 问题问题:代码中存在 document.formName.item(“itemName”) 这样的语句,不能在FF下运行解决方法: ...

  7. JS操作文件

    ) ;   ;   fso );   f1.Close();   // 读取文件的内容.  // Response.Write("Reading file <br>") ...

  8. Macbook pro内存升级

    http://support.apple.com/kb/HT1270?viewlocale=zh_CN&locale=zh_CN#link1 https://support.apple.com ...

  9. sqlite使用小结

    官方网站 http://www.sqlite.org/index.html 图形前端 http://www.sqlabs.net/sqlitemanager.php 个人觉得firefox的sqlit ...

  10. hadoop-1.2.0源码编译

    以下为在CentOS-6.4下hadoop-1.2.0源码编译步骤. 1. 安装并且配置ant 下载ant,将ant目录下的bin文件夹加入到PATH变量中. 2. 安装git,安装autoconf, ...