原创 Delphi 版的IIF函数收藏

新一篇: ADO Command 命令的执行 | 

Delphi 版的IIF函数

不少编程语言都有 IIF 函数,我们也可以构造自己的IIF函数:

// ------------------------------------------------------------------------
// 格式:IIF( lExp,  vExp1,  vExp2 )
// 参数:
//  lExp  : 逻辑表达式
// vExp1, vExp2 : 将返回的值
// 说明:当 lExp 为真时,返回 vExp1;lExp 为假时,返回 vExp2
// ------------------------------------------------------------------------
Function IIF( lExp:boolean; vExp1,vExp2 : variant) : variant; overload ;
begin
  if lExp
  then Result := vExp1
  else Result := vExp2 ;
end;


举例:

  Edit1.Text := iif( Button1.visible,  '可见', '不可见' ) ;

( By ForestK )

发表于 @ 2005年06月28日 13:54:00|评论(loading...)|编辑

新一篇: ADO Command 命令的执行 | 

评论

#cb 发表于2005-06-29 13:55:00  IP: 61.186.252.*
Delphi 本身就有这样的函数
函数名为IfThen(),所在单元为Math.pas

[Delphi] function IfThen(AValue: Boolean, const ATrue: Integer, const AFalse: Integer): Integer;

[Delphi] function IfThen(AValue: Boolean, const ATrue: Int64, const AFalse: Int64): Int64;

[Delphi] function IfThen(AValue: Boolean, const ATrue: Double, const AFalse: Double): Double;

在StrUtils.pas中也有一个
[Delphi] function IfThen(AValue: Boolean, const ATrue: string, AFalse: string): string;

不过你的使用了Variant类型,其实也可以声明无类型参数
#ForestK 发表于2005-06-29 16:46:00  IP: 61.186.252.*
谢谢提醒。我也想Delphi应该有这样的函数,但没找到。有时要想找一个合用的Delphi函数,还真有点困难。
#cb 发表于2005-07-01 09:05:00  IP: 61.186.252.*
多看帮助阿
发表评论  


登录
Csdn Blog version 3.1a
Copyright © ForestK