IIF gotcha

Applies To

OS:
VB:
NT, 9x, 2000
5, 6

The "Conditional If" (IIf) function is great.  It allows you to reduce code from 5 lines to one.  For instance


If Condition = 10 Then
     SomeValue = True
Else
     SomeValue = False  
End If

is the same as 

SomeValue =  IIf(Condition = 10, True, False)

The only problem is that IIf doesn't shortcircuit. In other words, both the true and the false parts will execute regardless of the condition.  For instance, execute the following statement:

SomeValue = IIf(Condition = 10, MsgBox("True Condition"), MsgBox("False Condition"))

will show both messages boxes. So if you call subs or functions from IIf - watch out.