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. Arduino 使用舵机库时 其它引脚输出怪异 解决方案

    使用Servo.h时,不管你在初始化时用的是9还是10脚,都不要把这两个脚作为舵机以外的用途! 例: servo.attach(9); digitalWrite(10,1);//错,不能把第10脚用作 ...

  2. C# GDI+学习笔记1

    —前言 本文是学习C# GDI+系列的第一篇文章,简单的介绍了GDI+的一些基本绘图内容,比较粗糙.但本文主要是让大家简单的回顾一下GDI+的基本概念.本篇文章的参考代码请在此下载 . GDIPTes ...

  3. ArcEngine下投影坐标和经纬度坐标的相互转换

    jojojojo2002 原文 ArcEngine下投影坐标和经纬度坐标的相互转换 投影转经纬度 private IPoint PRJtoGCS( double x, double y) { IPoi ...

  4. sessionFactory.getCurrentSession()的引出

    当业务逻辑中需要开启事务执行,业务逻辑也要调用底层操作数据库的函数,那函数也要开启事务操作. 如果用sessionFactory.openSession()的话会引起处理不在同一个事务中,会造成出错. ...

  5. WCF服务通过防火墙怎么设置

    设置防火墙 1.首先点击控制面板->系统与安全->Window防火墙->点击允许程序通过Windows防火墙 2.查找Windows Communication Foundation ...

  6. hdu 5254 水题

    纯暴力就能过的,可是题目描述真心不清楚,我看了好久好久才明白题目啥意思. 为了迅速打完,代码比较冗余. /* * Author : ben */ #include <cstdio> #in ...

  7. LoadRunner--内存指标介绍

    Threads——线程数当前全部线程数============================================ Available MBytes——物理内存的可用数指计算机上可用于运行 ...

  8. 类 .xml

    pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...

  9. <System.ServiceModel>

    實例: <system.serviceModel>    <diagnostics performanceCounters="All" />    < ...

  10. arm tiny6410双网卡桥接问题

    这几天做实验,想用arm tiny6410板子做个mesh网络节点.该板子本身自带一个lan网卡,我自己配了一个tp-link的usb无线网卡.其中wlan网卡工作在adhoc模式下,作为mesh骨干 ...