Total .NET Analyzer Rule Documentation  

IIF Rule

Replace the IIF function with an If...Then...Else statement.

Remarks

Visual Basic .NET supports the IIF function as an alternative to the If...Then...Else statement. For example, the following code evaluates the statement 'Price > 10.00' and calls the function Expensive if the expression is true, or Inexpensive if it is False:


Function CheckPrice(ByVal testMe As Double) As Object
IIf(price > 10.00, Expensive, Inexpensive)
End Function

Although this may seem like a shortcut to using the If...Then...Else statement, IIF works somewhat different than If...Then...Else. IIF statements must evaluate the entire statement when preparing the argument. This can lead to undesirable side effects.

For instance, if the expression in the arguments list includes function calls, the function in the False statement will be called even if the expression evaluates True.

Resolution

Consider replacing the IIF statement with If...Then...Else statement. For instance, you could change the example above to:

Function CheckPrice(ByVal testMe As Double) As Object
If price > 10.0 Then
Expensive
ElseIf price < 10.0 Then
Inexpensive
Else
MsgBox("The price is not valid.")
End If
End Function

See Also

IIf Function

If...Then...Else Statements