很遗憾 delphi 中没有 iif 函数. if ... then ... else 不是比 iif 逻辑上更 明确吗, iif 一般只是用于解释执行的语言, 编译后执行的都用 if then else.要想自己写一个 iif 函数十分简单(此处置列出三个类型, 你可以增加更多的类型): function iif(Exp: boolean; TrueValue: double; FalseValue: double): double; overload; function iif(Exp: boolean; TrueValue: integer; FalseValue: integer): integer; overload; function iif(Exp: boolean; TrueValue: String; FalseValue: string): string; overload;在 implementation 段写出各自的代码: function iif(Exp: boolean; TrueValue: double; FalseValue: double): double; overload; begin if exp then result := TrueValue else result := FalseValue; end; function iif(Exp: boolean; TrueValue: integer; FalseValue: integer): integer; overload; begin if exp then result := TrueValue else result := FalseValue; end; function iif(Exp: boolean; TrueValue: String; FalseValue: string): string; overload; begin if exp then result := TrueValue else result := FalseValue; end;
case
明确吗, iif 一般只是用于解释执行的语言, 编译后执行的都用 if then else.要想自己写一个 iif 函数十分简单(此处置列出三个类型, 你可以增加更多的类型):
function iif(Exp: boolean; TrueValue: double; FalseValue: double): double; overload;
function iif(Exp: boolean; TrueValue: integer; FalseValue: integer): integer; overload;
function iif(Exp: boolean; TrueValue: String; FalseValue: string): string; overload;在 implementation 段写出各自的代码:
function iif(Exp: boolean; TrueValue: double; FalseValue: double): double; overload;
begin
if exp then result := TrueValue else result := FalseValue;
end;
function iif(Exp: boolean; TrueValue: integer; FalseValue: integer): integer; overload;
begin
if exp then result := TrueValue else result := FalseValue;
end;
function iif(Exp: boolean; TrueValue: String; FalseValue: string): string; overload;
begin
if exp then result := TrueValue else result := FalseValue;
end;