
Returns one of two objects, depending on the evaluation of an expression.
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.
The IIf function provides a counterpart for the ternary Conditional Operator: ? : in Visual C++.
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 Return CStr(IIf(testMe > 1000, "Large", "Small")) End Function
Note that if Option Strict is On, you must use the CStr keyword to explicitly convert the return from Object to String.
Namespace: Microsoft.VisualBasic
Module: Interaction
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Although the remarks above state that the IIf function is a counterpart the for C++ (and C#) ternary conditional operator (?:); it has does not have the same semantics.
For example, have a look at the following C# method which returns the length of a string:
public int GetLength(string value)
{ return string.IsNullOrEmpty(value) ? 0 : value.Length;
}
As you can see, if value is a null reference (Nothing in Visual Basic) or is empty, then the method returns 0; otherwise, it returns the length of the value parameter.
You might be tempted to write the same code in Visual Basic like so (with Option Strict On):
Public Function GetLength(ByVal value As String) As Integer
Return CInt(IIf(value Is Nothing, 0, value.Length))
End Function
However, unlike the ternary conditional operator above, IIf is simply a function call where both the true part and the false part are evaluated whether the expression returns true or false. This means that at runtime when Nothing is passed for the parameter value, a NullReferenceException will be thrown when the false part attempts to retrieve value.Length.
Instead of using IIf, you will need to do the following to achieve the sementics as the C# code:
Public Function GetLength(ByVal value As String) As Integer
If (value Is Nothing) Then
Return 0
End If
Return value.Length
End Function

- 6/12/2006
- David M. Kean
- 6/12/2006
- Will Sullivan
As is mentioned in the Example above, if you have Option Strict On, it is usually necessary to cast the return value of the IIf function because the return value is always Object. Otherwise you may get compile errors about implicit type conversions.
Function checkIt(ByVal testMe As Integer) As String
Return CStr(IIf(testMe > 1000, "Large", "Small")) 'CStr required to convert Object return value from IIf to String value needed for function return
End Function
With the new generics feature of Visual Basic, you can create a version of IIf that does not require this cast:
Public Function IIf(Of T)(ByVal Expression As Boolean, ByVal TruePart As T, ByVal FalsePart As T) As T
If Expression Then
Return TruePart
Else
Return FalsePart
End If
End Function
With the above version of IIf defined in a Module, the following code will now work:
Function checkIt(ByVal testMe As Integer) As String
Return IIf(testMe > 1000, "Large", "Small")
End Function
This is because the compiler is able to figure out that the TruePart and FalsePart arguments are both of type string, and will automatically cause the return value of the function to be the same type ("T", in the IIf function definition).
For more information on creating generic functions, see http://msdnwiki.microsoft.com/en-us/mtpswiki/ms235246(VS.80).aspx.
For more information on generic types in general, see http://msdnwiki.microsoft.com/en-us/mtpswiki/w256ka79(VS.80).aspx.

- 6/10/2006
- David M. Kean
- 6/9/2006
- Stephen Weatherford MS
