Ersal Şahin
Ersal Şahin

Reputation: 45

delphixe2 "if-then" condition error

I wrote the following code in delphi however it is not working as I want it to.

Is there a problem with this code?

function of this variant is returned:

   function DoRun(a:String;b:boolean):variant; 
   Begin   
       result:=a; 
   End;

this is using the above function is malfunctioning code:

 procedure TForm2.BitBtn1Click(Sender: TObject);

    var   
        a,b,c:Integer;

    begin   

        a:=10;   
        b:=20;   
        c:=30;

        if (a=1) and (b=2) and (c=3) and DoRun('',true)='0' then

            showmessage('True');

    end;

RESULT : 'True' message see.

"if" line code end brackets "(DoRun('',true)='0')" worked by right running :

procedure TForm2.BitBtn1Click(Sender: TObject); 

var   
    a,b,c:Integer;

begin   
    a:=10;   
    b:=20;   
    c:=30;

    if (a=1) and (b=2) and (c=3) and (DoRun('',true)='0') then

        showmessage('True');

end;

Please help identify the problem and show me how I can make this code work as expected.

Upvotes: 1

Views: 302

Answers (2)

Uyelik Bilgisi
Uyelik Bilgisi

Reputation: 26

All;

if ( (a=1) and (b=2) and (c=3) and DoRun('',true) ) = '0' 

not the same

if ( DoRun('',true) and (a=1) and (b=2) and (c=3) ) = '0'

this is not running !! Run time error "Invalid Argument"!

Is not that the example you gave me the opposite? :):) You have a logic error! so , your answer has nothing to do with the subject of!

Compiler interprets from left to right. compiler "DoRun()" error condition is caused by the algorithm can not be regarded as brackets. If "DoRun()" condition on the top of the conditions we get this error "RTE" that is why you will see to. Conditions at the beginning of "brackets" If you use as you will see that no compilation. In this case the compiler clearly not the final condition while allowing the brackets at the far right to the left does not permit.

Yes, the compiler incorrectly interprets! but the compiler to interpret it that way!

Upvotes: 1

LHristov
LHristov

Reputation: 1123

First piece of code is definitely wrong. There your condition is

if ( (a=1) and (b=2) and (c=3) and DoRun('',true) ) = '0' 

which equals to

if Variant(false) = Variant('0') then 
  ShowMessage('True');

and obviously for Variant type false='0'

The second piece of code is working as expected. The second comparison is OK, only boolean comparisons there, DoRun function is not executed and the result of comparison is false

Upvotes: 4

Related Questions