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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Insight on IIF()

Status
Not open for further replies.

kamweng

Programmer
Jan 20, 2012
36
MY
Normally my statement on IIF() worked.

But I am unable to convert this:

IF mYY = '2012'
mPDF = '_1306.pdf'
ENDIF

into this:

IIF(mYY='2012',mPDF='_1306.pdf',mPDF=' ')

Is it true that I cannot store value into variables when I used IIF()?
Or is it my coding is wrong?

--------------------------------------------------

By the way, some of my IIF() do not need the "eExpression2" (which is the "ELSE" in the IF...ENDIF). This occurs when I conditionally changed the color of an object but I do not know its previous color. Therefore, I have problem with the "eExpression2".

Presently, I used the IF...ENDIF to bypass this type of problem. Is there a better way to deal with the "eExpression2" in the IIF()?

--------------------------------------------------

All hints, tips, wisdom is valued.
Thank you.






 
Please ignore the first part of my post because now I understand where my code gone wrong. Thank you.
 
Code:
mPDF = IIF(mYY='2012', '_1306.pdf', ' ')

*** end where you don't need ELSE just use the same variable in ELSE part:
mPDF = IIF(mYY='2012', '_1306.pdf', mPDF)

Borislav Borissov
VFP9 SP2, SQL Server
 
If you understood your error, then also the second part of your post should be clear. What do you not understand?
IIF() unlike if does not execut code, it's simply a function which either returns eExpression1 or eExpression2, depending on the logical value of lExpression

So in general you can rewrite IIF(lExpression, eExpression1, eExpression2) as user defined function

Code:
Function myIIF(lExpression, eExpression1, eExpression2)
   If lExpression
      RETURN eExpression1
   Else
      RETURN eExpression2
   Endif
Endfunc

And using IIF() then compares to calling myIIF.

But: IIF() does only evaluate expression1 or expression2, not both. myIIF would get the expression results passed as input, so both would be evaluated.

You can use anything as eExpression1/2 that evaluates to some result value, but not execute an assignment. You can evaluate("a=1"), but that result merely would be .T., if a=1 and .F. if a<>1, it doesn't assign 1 to a. You can execute any function, as a function has a return value, eg you can execute iif(.f.,1,Messagebox("test")), as Messagebox is a function, but you can't execute a command, as it does not elevate. In short: expression is a term clearly specifying what is possible and what not. An expression always needs to be a term evaluating to a result value, so you have to be able to EVAL("eExpression").

And the diff in evaluating only one of the expressions get's clear if you'd execute
Code:
  IIF(.t., MessageBox("true1"), MessageBox("false1"))
  IIF(.f. MessageBox("true2"), MessageBox("false2"))
myIIF(.t., MessageBox("true3"), MessageBox("false3"))
myIIF(.f., MessageBox("true4"), MessageBox("false4"))

Function myIIF(lExpression, eExpression1, eExpression2)
   If lExpression
      RETURN eExpression1
   Else
      RETURN eExpression2
   Endif
Endfunc

So overall, IIF() isn't there to shorten normal IF statements, it's not a shortened IF syntax, it's a function with the specialty about the evaluation of only one of the expressions. You always have to specify both expressions, as either of them is returned, so unlike if you can't have an empty if or else branch. change a variable only on condition .T. you have to do as Boris suggests and return the variable value as is in case of .F., eg variable = iif(condition,"new value",variable), but it's faster to do a normal IF then, because in case of condition=.f. you don't execute variable = variable.

Bye, Olaf.
 
Thanks Boris for your suggestion.

Thanks Olaf for your in-depth explanation. You are a good teacher.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top