共用方式為


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

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