Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IIF command not working correctly?

Status
Not open for further replies.

Gilesy

Programmer
Jul 2, 2002
40
0
0
GB
Hi all
I have had it reported that the IIF will evaluate all conditions regardless of the return value.
For Example
<test> = IIF(Procedure1() AND procedure2(),1,0)
If procedure1 returns false then it still (apparently) evaluates procedure2.
I cannot however recreate this. Has anyone ever heard of this or something similar happening in VFP?

Thank you
 
Gilesy,

If procedure1 returns false then it still (apparently) evaluates procedure2.

No, that's not my understanding. In general, if you have multiple conditions joined by ANDs and ORs, VFP will evaluate them in the order it finds them until it knows the outcome for certain, In other words, if the first condition is .F., it will not bother to evaluate any further conditions after an AND.

The fact that you are using a condition within an IIF() makes no difference to that behaviour.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Hi Mike
I thought that was the case too, however I beleive I have stumbled on where it happens:

close data all
use <table>
SELECT * from <table> WHERE <field> = iif(con1() and con2(),1,0)
PROCEDURE CON1
WAIT WINDOW "Processing Condition 1"
RETURN .F.

PROCEDURE CON2
WAIT WINDOW "Processing Condition 2"
RETURN .F.

Both procedures will now be evaluated. It only seems to happen in a select statement. Also if you change the select to something like:
SELECT * from <table> WHERE <field> = iif(<condition>, con1(), con2())
Again it seems both procedures will be evaluated.

Pete

 
even worse:

Code:
Select IIf(MessageBox("",4,"")=6 and MessageBox("",4,"")=7,;
"now what?","and now? ") as cfield;
from browser.dbf where .f.

:eek:)

Bye, Olaf.
 
I think that what you've both proven is that the rules for "shortcut" logical expression evaluation are different in xBase code versus an SQL SELECT command. This is not really surprising since SELECTS attempt to follow ANSI standards, and not FoxPro "rules".

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top