Originally Posted By: Glenn
You can't put a UDF or most other functions in the test part.


Yes you can. Not to sure why you think that you cannot - do you have an example?

The tricky thing about IIf() is that BOTH the True/False sections are ALWAYS evaluated so you need to be very careful what you put there.

Here is a very simple example that illustrates a working scenario:
 Code:
$Var1=10
$Var2=5
 
$Action="Divide"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF
$Action="Multiply"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF
 
Function doMult($a,$b)
	$doMult=$a*$b
EndFunction
 
Function doDiv($a,$b)
	$doDiv=$a/$b
EndFunction


This works as you'd expect as even though both doMult() and doDiv() are called, the unused return value is simply discarded. This is dangerous though, as (in most languages) if $Var2 was zero you would get a "divide by zero" error even if the action was "Multiply". KiXtart is a bit more forgiving so you don't see the error.

Here is an example of when using functions causes an unexpected side effect:
 Code:
If 6=IIF($sType="New"
	,MessageBox("Thing does not exist - do you want to add it?","Add Item",4)
	,MessageBox("Thing already exists - do you want to change it?","Change Item",4)
	)
	"Ok, Thing is being actioned"+@CRLF
Else
	"Ok, you chose not to update Thing"+@CRLF
EndIf


If you run this then *both* MessageBox()'s will appear, though only the return value from the "Change Item" box will be used.