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. C# 使用NPlot绘图

    首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot. 一.入门 1. 对所绘的图进行打印与保存 private void print() { myPlot.P ...

  2. delphi7如何实现 科学计数的转换。 比如我输入2,触发之后会转换成2.000000E+00.求赐教

    uses SysUtils; function StrToExp(s: string): string;var f: Extended;begin f := StrToFloat(s); Result ...

  3. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

  4. OFBIZ安装

    1. 安装SVN客户端,从Apache OFBiz Source Repository获取OFBIZ下载地址.此处以12.04为例,下载地址为http://svn.apache.org/repos/a ...

  5. Oracle优化器介绍

    Oracle优化器介绍 本文讲述了Oracle优化器的概念.工作原理和使用方法,兼顾了Oracle8i.9i以及最新的10g三个版本.理解本文将有助于您更好的更有效的进行SQL优化工作. RBO优化器 ...

  6. Php 笔记3-----php与 asp的等价关系

    对比asp.net 与 php的对比  ,有助于进一步理解 php. 1  输出. asp.net 输出    Response.Write(str);    // 将string 写入到 服务器向浏 ...

  7. [转]Linux文件权限详解

    转自:http://blog.chinaunix.net/uid-25052030-id-174343.html 在linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问 ...

  8. 从cocos2dx中寻找函数指针传递的方法

    目的 看到群里有个朋友搞了好几天函数指针传递,没搞好.所以写一篇文章,旨在从cocos2dx中帮朋友们找到如何传递指针. 旧版本的函数指针传递 全局函数函数指针调用 一般在C++11之前,我们一般是这 ...

  9. C#学习7

    一.变量交换 ; ; Console.WriteLine("开始a={0},b={1}",a,b); a = a + b; b = a - b; a = a - b; Consol ...

  10. Linux-sort用法

    本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html sort命令是帮我们依据不同的数据类型进行排序, ...