Interaction.IIf Method

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

Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

Public Shared Function IIf ( _
	Expression As Boolean, _
	TruePart As Object, _
	FalsePart As Object _
) As Object
Dim Expression As Boolean
Dim TruePart As Object
Dim FalsePart As Object
Dim returnValue As Object

returnValue = Interaction.IIf(Expression, TruePart, FalsePart)
public static Object IIf (
	boolean Expression, 
	Object TruePart, 
	Object FalsePart
public static function IIf (
	Expression : boolean, 
	TruePart : Object, 
	FalsePart : Object
) : Object



Required. Boolean. The expression you want to evaluate.


Required. Object. Returned if Expression evaluates to True.


Required. Object. Returned if Expression evaluates to False.

Return Value

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

For more detailed information, see the Visual Basic topic IIf Function.

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.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0