Visual Basic Language Reference  

IIf Function

Returns one of two objects, depending on the evaluation of an expression.

Public Function IIf( _
   ByVal Expression As Boolean, _ 
   ByVal TruePart As Object, _ 
   ByVal FalsePart As Object _ 
) As Object

Parameters

Expression
Required. Boolean. The expression you want to evaluate.
TruePart
Required. Object. Returned if Expression evaluates to True.
FalsePart
Required. Object. Returned if Expression evaluates to False.

Remarks

Note   The expressions in the argument list can include function calls. As part of preparing the argument list for the call to IIf, the Visual Basic compiler calls every function in every expression. This means that you cannot rely on a particular function not being called if the other argument is selected by Expression.

Example

This example uses the IIf function to evaluate the TestMe parameter of the CheckIt procedure and returns the word "Large" if the amount is greater than 1000; otherwise, it returns the word "Small".

Function CheckIt (ByVal TestMe As Integer) As String
   CheckIt = IIf(TestMe > 1000, "Large", "Small")
End Function

See Also

Choose Function | If...Then...Else Statements | Select...Case Statements | Switch Function