自己写也一样// ITE : IF THEN ELSE function ITE(exp: Boolean, trueV, falseV: Variant): Variant; begin if exp then Result := trueV else Result := falseV; end'function decode(exp,con1,val1,con2,val2,elseV: Variant): Variant; begin if exp = con1 then Result := val1 else if exp = con2 then Result := val2 else Result := elseV; end;
function decodeX(exp, cons, vals, elseV: Variant): Variant; var i, L1, H1, L2, H2: Integer; begin Result := null;
// if not (VarIsArray(cons) and VarIsArray(vals)) then // raise exeception.create('parameter"cons, vals" should be Variant Array');
L1 := VarArrayLowBound(cons, 1); H1 := VarArrayHighBound(cons,1); L2 := VarArrayLowBound(vals, 1); H2 := VarArrayHighBound(vals,1); // if (L1 <> L2) or (L2 <> H2) then // raise exeception.create('parameter"cons, vals" should be in same structure');
for i := L1 to H1 do begin if exp = cons[i] then begin Result := vals[i]; Break; end; end; if VarIsNull(Result) then // 如果就是有返回null的情况,那...再改改 begin Result := elseV; end; end; ps: Oracle PL/SQL的decode真是好用;上面这个只是一个模仿的花瓶,不很适用,汗用法示例: decode(age,VarArrayOf([30,40,50]),VarArrayOf(['而立','不惑','知天命']), '@@@'); age为30的话, 返回值 ‘而立’ age为40的话,返回值 ‘不惑’ age为50的话,返回值 ‘知天命’ age为20的话,返回值 ‘@@@’
// if (L1 <> L2) or (L2 <> H2) thenshould by // if (L1 <> L2) or (H1 <> H2) then
Delphi自己有个 IfThen 函数,在Maths单元里,但是只能对数值型变量进行赋值。还是自己写一个吧,VB也有类似函数IIF,就用这个名字好了:function IIF(F: Boolean; const A, B: Variant): Variant; begin if F then Result := A else Result := B; end;
楼上说的极是,但是如果你不能确定你传入的表达式是否有值时,用下面的语句: class function TCommon.GetNodeValue(xn: IXMLNode; defaultValue: variant): variant; begin if xn=nil then Result := defaultValue else Result := xn.Text; end;
function ITE(exp: Boolean, trueV, falseV: Variant): Variant;
begin
if exp then
Result := trueV
else
Result := falseV;
end'function decode(exp,con1,val1,con2,val2,elseV: Variant): Variant;
begin
if exp = con1 then
Result := val1
else if exp = con2 then
Result := val2
else
Result := elseV;
end;
''): string; overload; $[StrUtils.pas
功能 返回指定的逻辑字符串
说明 IfThen(True, '是', '否') = '是';IfThen(False, '是', '否') = '否'
参考 <NULL>
例子 Edit3.Text := IfThen(CheckBox1.Checked, Edit1.Text, Edit2.Text);
var
i, L1, H1, L2, H2: Integer;
begin
Result := null;
// if not (VarIsArray(cons) and VarIsArray(vals)) then
// raise exeception.create('parameter"cons, vals" should be Variant Array');
L1 := VarArrayLowBound(cons, 1);
H1 := VarArrayHighBound(cons,1);
L2 := VarArrayLowBound(vals, 1);
H2 := VarArrayHighBound(vals,1);
// if (L1 <> L2) or (L2 <> H2) then
// raise exeception.create('parameter"cons, vals" should be in same structure');
for i := L1 to H1 do
begin
if exp = cons[i] then
begin
Result := vals[i];
Break;
end;
end;
if VarIsNull(Result) then // 如果就是有返回null的情况,那...再改改
begin
Result := elseV;
end;
end;
ps: Oracle PL/SQL的decode真是好用;上面这个只是一个模仿的花瓶,不很适用,汗用法示例:
decode(age,VarArrayOf([30,40,50]),VarArrayOf(['而立','不惑','知天命']), '@@@'); age为30的话, 返回值 ‘而立’
age为40的话,返回值 ‘不惑’
age为50的话,返回值 ‘知天命’
age为20的话,返回值 ‘@@@’
// if (L1 <> L2) or (H1 <> H2) then
begin
if F then
Result := A
else
Result := B;
end;
class function TCommon.GetNodeValue(xn: IXMLNode;
defaultValue: variant): variant;
begin
if xn=nil then
Result := defaultValue
else
Result := xn.Text;
end;
他提供了 ifthen 函数,本质且别就是它使函数,c里面是运算符。