我从pb转到delphi快好几个月了,但还是不能完全掌握,下面这种情况大家帮我看一看  我感觉这种情况时写法能有简单的写法就好了。if checkbox1.checked then
    i:=1
else
    i :=0;可不可以写成一句话,类似于这种(笔者想的)i :=(if checkbo1.ckecked :1,0)

解决方案 »

  1.   

    没有....不过好像有个函数叫ifthen() 去查下帮助吧
      

  2.   

    i := Integer(CheckBox.Checked) and 1;
      

  3.   

    i := ifthen(checkbo1.ckecked,1,0);
    就可以了
      

  4.   

    这样就好了不过ifthen用了后提示:undclared
      

  5.   

    用了以后提示:undeclared identifier :ifthen;是怎么回事呀?
      

  6.   

    我觉得westfly(西翔) 的方法不错
      

  7.   

    Object Pascal 的优势就是书写优美哦。
    那么写对的起自己对不起观众。呵呵~写程序到现在了,打字似乎不是问题了吧。多写点,清楚点也不错。
      

  8.   

    delphi  里的 integer 和 Boolean 可以强制转换的!
    你们可以试试以下的代码,在form上放个按钮Var i : integer; b : Boolean;
    begin
      i := integer(True or False);
      b := Boolean(1);
      if b then
         showmessage(inttostr(i))
    end;
      

  9.   

    与面类似的代码在VCL Source中随地可见:const   BInt: array [Boolean] of Integer = (0, 1);
    ...
    var
        I: Integer;
    ...    I := BInt[CheckBox1.Checked];
      

  10.   

    const
       Bool_Value: array [Boolean] of Integer = (0, 1);
    begin
       i := Bool_Value[checkbox1.checked];
    end;楼上的抢先一步了。
      

  11.   

    const   BInt: array [Boolean] of Integer = (0, 1);
    ...
    var
        I: Integer;
    ...    I := BInt[CheckBox1.Checked];
    真是不错!好好学习!
      

  12.   

    噢?????object pascal有三目运算苻吗??好像没有吧...
      

  13.   

    //参考如下代码~~
    I := Ord(CheckBox1.Checked);//如果觉得不方便,可以自己写一个Iif函数~~
    //以前学foxbase的时候比较喜欢这个函数~~
    //不过现在几乎不用~~
    function Iif(mBool: Boolean; mDataA, mDataB: Variant): Variant; overload;
    begin
      if mBool then
        Result := mDataA
      else Result := mDataB;
    end; { Iif }function Iif(mBool: Boolean; mDataA, mDataB: TObject): TObject; overload;
    begin
      if mBool then
        Result := mDataA
      else Result := mDataB;
    end; { Iif }
      

  14.   

    Math单元有个现成的uses Mathi := IfThen(CheckBox1.Ckecked, 1, 0);
      

  15.   


    I:=Abs(StrToInt((BoolToStr(CheckBox1.Checked,False))));
      

  16.   

    i := Integer(CheckBox1.Checked);
      

  17.   

    晕。。delphi还有这招!我怎么不知道??呵呵!
      

  18.   

    UnitMathStrUtilsor Categorymiscellaneous routinesDelphi syntax:function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload;function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64; overload;
    function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload;
    function IfThen(AValue: Boolean; const ATrue: string; const AFalse: string = ''): string; overload;
      

  19.   

    回复人: ehom(?!) ( ) 信誉:160  
    他说的就很正确,这种方法最好了。
      

  20.   

    更多函数请见:
    http://expert.csdn.net/Expert/topic/1494/1494660.xml?temp=.7170832