Rediger

Del via


If Operator (Visual Basic)

Uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments.

Syntax

If( [argument1,] argument2, argument3 )

If operator called with three arguments

When If is called by using three arguments, the first argument must evaluate to a value that can be cast as a Boolean. That Boolean value will determine which of the other two arguments is evaluated and returned. The following list applies only when the If operator is called by using three arguments.

Parts

Term Definition
argument1 Required. Boolean. Determines which of the other arguments to evaluate and return.
argument2 Required. Object. Evaluated and returned if argument1 evaluates to True.
argument3 Required. Object. Evaluated and returned if argument1 evaluates to False or if argument1 is a NullableBoolean variable that evaluates to Nothing.

An If operator that is called with three arguments works like an IIf function except that it uses short-circuit evaluation. An IIf function always evaluates all three of its arguments, whereas an If operator that has three arguments evaluates only two of them. The first If argument is evaluated and the result is cast as a Boolean value, True or False. If the value is True, argument2 is evaluated and its value is returned, but argument3 is not evaluated. If the value of the Boolean expression is False, argument3 is evaluated and its value is returned, but argument2 is not evaluated. The following examples illustrate the use of If when three arguments are used:

' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

The following example illustrates the value of short-circuit evaluation. The example shows two attempts to divide variable number by variable divisor except when divisor is zero. In that case, a 0 should be returned, and no attempt should be made to perform the division because a run-time error would result. Because the If expression uses short-circuit evaluation, it evaluates either the second or the third argument, depending on the value of the first argument. If the first argument is true, the divisor is not zero and it is safe to evaluate the second argument and perform the division. If the first argument is false, only the third argument is evaluated and a 0 is returned. Therefore, when the divisor is 0, no attempt is made to perform the division and no error results. However, because IIf does not use short-circuit evaluation, the second argument is evaluated even when the first argument is false. This causes a run-time divide-by-zero error.

number = 12

' When the divisor is not 0, both If and IIf return 4.
Dim divisor = 3
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))

' When the divisor is 0, IIf causes a run-time error, but If does not.
divisor = 0
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
' Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))

If operator called with two arguments

The first argument to If can be omitted. This enables the operator to be called by using only two arguments. The following list applies only when the If operator is called with two arguments.

Parts

Term Definition
argument2 Required. Object. Must be a reference or nullable value type. Evaluated and returned when it evaluates to anything other than Nothing.
argument3 Required. Object. Evaluated and returned if argument2 evaluates to Nothing.

When the Boolean argument is omitted, the first argument must be a reference or nullable value type. If the first argument evaluates to Nothing, the value of the second argument is returned. In all other cases, the value of the first argument is returned. The following example illustrates how this evaluation works:

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

See also