If 運算子 (Visual Basic)If Operator (Visual Basic)

使用最少運算評估,有條件地傳回兩個值之一。Uses short-circuit evaluation to conditionally return one of two values. If與三個引數或兩個引數,就可以呼叫運算子。The If operator can be called with three arguments or with two arguments.

語法Syntax

If( [argument1,] argument2, argument3 )  

如果使用三個引數來呼叫運算子If Operator Called with Three Arguments

If稱為藉由使用三個引數,第一個引數必須評估為值,可以轉換成BooleanWhen If is called by using three arguments, the first argument must evaluate to a value that can be cast as a Boolean. Boolean值將決定哪些其他兩個引數會評估並傳回。That Boolean value will determine which of the other two arguments is evaluated and returned. 下列清單時,才適用If使用三個引數呼叫運算子。The following list applies only when the If operator is called by using three arguments.

組件Parts

詞彙Term 定義Definition
argument1 必要項。Required. Boolean.Boolean. 判斷其中一個要評估及傳回的其他引數。Determines which of the other arguments to evaluate and return.
argument2 必要項。Required. Object.Object. 評估並傳回 ifargument1評估為TrueEvaluated and returned if argument1 evaluates to True.
argument3 必要項。Required. Object.Object. 評估並傳回 ifargument1評估為False或者argument1Nullable Boolean變數評估為NothingEvaluated and returned if argument1 evaluates to False or if argument1 is a NullableBoolean variable that evaluates to Nothing.

If運算子,會使用三個引數呼叫的運作方式類似IIf函式不同之處在於它會使用最少運算評估。An If operator that is called with three arguments works like an IIf function except that it uses short-circuit evaluation. IIf函式一律會評估其引數,這三個,而If有三個引數的運算子會評估只有兩個。An IIf function always evaluates all three of its arguments, whereas an If operator that has three arguments evaluates only two of them. 第一個If引數會評估和結果轉換成Boolean的值,TrueFalseThe first If argument is evaluated and the result is cast as a Boolean value, True or False. 如果值為Trueargument2會評估並傳回其值,但argument3不會評估。If the value is True, argument2 is evaluated and its value is returned, but argument3 is not evaluated. 如果的值Boolean運算式Falseargument3會評估並傳回其值,但argument2不會評估。If the value of the Boolean expression is False, argument3 is evaluated and its value is returned, but argument2 is not evaluated. 下列範例說明使用If三個引數使用時: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. 此範例示範兩次嘗試將變數number變數所divisor除非divisor為零。The example shows two attempts to divide variable number by variable divisor except when divisor is zero. 在此情況下,應該會傳回 「 0 」,並不應該嘗試執行除法運算,因為會造成執行階段錯誤。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. 因為If運算式會使用最少運算的評估,它會評估第二個或第三個引數,第一個引數的值而定。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. 如果第一個引數為 true,除數為零並不安全地評估第二個引數,並執行除法。If the first argument is true, the divisor is not zero and it is safe to evaluate the second argument and perform the division. 如果第一個引數為 false,會評估第三個引數,就會傳回 0。If the first argument is false, only the third argument is evaluated and a 0 is returned. 因此,除數為 0 時,不會嘗試執行除法並不會產生錯誤。Therefore, when the divisor is 0, no attempt is made to perform the division and no error results. 不過,因為IIf不會使用最少運算評估,即使在第一個引數為 false 時,才評估第二個引數。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

第一個引數If可以省略。The first argument to If can be omitted. 這可讓操作員使用只有兩個引數來呼叫。This enables the operator to be called by using only two arguments. 下列清單時,才適用If使用兩個引數呼叫運算子。The following list applies only when the If operator is called with two arguments.

組件Parts

詞彙Term 定義Definition
argument2 必要項。Required. Object.Object. 必須是參考或可為 null 的型別。Must be a reference or nullable type. 評估並傳回其評估結果為任何項目以外時NothingEvaluated and returned when it evaluates to anything other than Nothing.
argument3 必要項。Required. Object.Object. 評估並傳回 ifargument2評估為NothingEvaluated and returned if argument2 evaluates to Nothing.

Boolean省略引數,第一個引數必須是參考或可為 null 的型別。When the Boolean argument is omitted, the first argument must be a reference or nullable type. 如果第一個引數評估為Nothing,會傳回第二個引數的值。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