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

The IIf function provides a counterpart for the ternary Conditional Operator: ? : in Visual C++.

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".

Visual Basic
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.

Requirements

Namespace: Microsoft.VisualBasic

Module: Interaction

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

Option Strict Statement
Type Conversion Functions
Choose Function
If...Then...Else Statement (Visual Basic)
Select...Case Statement (Visual Basic)
Switch Function

Tags :


Community Content

Stephen Weatherford MS
A generic IIf function that infers its return type from its arguments

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.

 

Tags :

Will Sullivan
Does not have the same semantics as the ? : operator

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
Tags :

Page view tracker