DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

iif ssems to evaluate allways both parameters, the true AND the false case

Solved!
Go to solution

Hello,

 

trying the new IIF command I'm a little surprised. Run the following code:

 

Dim a,b

a=1
b=3
logfilewrite(IIF(a>0, b/a, a/b))

a=0
b=3
logfilewrite(IIF(a>0, b/a, a/b))

 

 

And you'll get an division by 0 error in the second case.  But that's exactly what I would like to prevent with this IIF statement.  This example is not verry usefull but there are many cases where this behaviour leads to trouble. Like in the following "usefull" line of code:

 

logfilewrite(IIF(RootPropExist("MyProp"),RootPropValGet("MyProp"),"DefaultValue"))

 

Has anyone a workaround keeping this an inline statement?

 

Best Regards

 

Martin Liese

0 Kudos
Message 1 of 3
(4,365 Views)
Solution
Accepted by topic author mliese

Hello Martin!

IIF is like any other VBS function calls. All paramters are evaluated before the function is called. In general it is OK in the case of IIF it is sometimes annoying. The solution is to build your own IIF equivalent function:
Public Function IIFX( ByRef Compare, ByRef FalseExpr, ByRef TrueExpr )
  If Compare <> 0 Then
    IIFX = Eval(TrueExpr)
  Else
    IIFX = Eval(FalseExpr)
  End If
End Function


The drawback is the you have put the false and true expression in a string. Bad to read and sometimes a little difficult to write. Your example has to bechanged to:

logfilewrite(IIFX(RootPropExist("MyProp"),"RootPropValGet(""MyProp"")","""DefaultValue"""))


Matthias

Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 3
(4,347 Views)

For anyone finding this question from google, I had a similiar problem with this solution. http://www.adamjamesnaylor.com/2012/10/29/SQL-Server-Reporting-Services-IIf-Function-Evaluating-True...

0 Kudos
Message 3 of 3
(3,650 Views)