Delphi里有没有像C 里的<表达式1>?<表达式2>:<表达式3>这样的三目运算呀?

解决方案 »

  1.   

    自己写也一样// 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;
      

  2.   

    我记得有个ifthen(),不知道什么版本支持,我的是用不了
      

  3.   

    首部 function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = 
          ''): string; overload; $[StrUtils.pas
          功能 返回指定的逻辑字符串
          说明 IfThen(True, '是', '否') = '是';IfThen(False, '是', '否') = '否'
          参考 <NULL>
          例子 Edit3.Text := IfThen(CheckBox1.Checked, Edit1.Text, Edit2.Text);
      

  4.   

    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的话,返回值 ‘@@@’
      

  5.   

    // if (L1 <> L2) or (L2 <> H2) thenshould by 
     // if (L1 <> L2) or (H1 <> H2) then
      

  6.   

    Delphi自己有个 IfThen 函数,在Maths单元里,但是只能对数值型变量进行赋值。还是自己写一个吧,VB也有类似函数IIF,就用这个名字好了:function IIF(F: Boolean; const A, B: Variant): Variant;
    begin
      if F then
        Result := A
      else
        Result := B;
    end;
      

  7.   

    楼上说的极是,但是如果你不能确定你传入的表达式是否有值时,用下面的语句:
    class function TCommon.GetNodeValue(xn: IXMLNode;
      defaultValue: variant): variant;
    begin
      if xn=nil then
        Result := defaultValue
      else
        Result := xn.Text;
    end;
      

  8.   

    pascal 里面没有这类的三目运算。
    他提供了 ifthen 函数,本质且别就是它使函数,c里面是运算符。