Boolean 資料類型 (Visual Basic)Boolean Data Type (Visual Basic)

可以只保留值TrueFalseHolds values that can be only True or False. 關鍵字TrueFalse對應至兩個狀態的Boolean變數。The keywords True and False correspond to the two states of Boolean variables.

備註Remarks

使用布林資料類型 (Visual Basic)是/否或是開/關包含兩個狀態的值,例如 true/false。Use the Boolean Data Type (Visual Basic) to contain two-state values such as true/false, yes/no, or on/off.

Boolean 的預設值為 FalseThe default value of Boolean is False.

Boolean 值不會儲存為 數字,並儲存的值並非為等同於數字。Boolean values are not stored as numbers, and the stored values are not intended to be equivalent to numbers. 您不需撰寫程式碼,以針對對等數值TrueFalseYou should never write code that relies on equivalent numeric values for True and False. 可能的話,您應該限制的使用方式Boolean它們設計的邏輯值的變數。Whenever possible, you should restrict usage of Boolean variables to the logical values for which they are designed.

類型轉換Type Conversions

當 Visual Basic 數值資料型別將值轉換成Boolean,0 會變成False和所有其他值會變成TrueWhen Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True. 當 Visual Basic 會將轉換Boolean為數字類型,值False會變成 0 和True變成-1。When Visual Basic converts Boolean values to numeric types, False becomes 0 and True becomes -1.

當您將轉換之間Boolean值和數值資料類型,請記住,.NET Framework 轉換方法不一定會產生與 Visual Basic 轉換關鍵字相同的結果。When you convert between Boolean values and numeric data types, keep in mind that the .NET Framework conversion methods do not always produce the same results as the Visual Basic conversion keywords. 這是因為 Visual Basic 轉換會保留與舊版相容的行為。This is because the Visual Basic conversion retains behavior compatible with previous versions. 如需詳細資訊,請參閱"布林類型不會無法轉換以數值類型正確 」 中疑難排解資料型別For more information, see "Boolean Type Does Not Convert to Numeric Type Accurately" in Troubleshooting Data Types.

程式設計提示Programming Tips

  • 負數的數字。Negative Numbers. Boolean 不是數值類型,因此無法表示為負數值。Boolean is not a numeric type and cannot represent a negative value. 在任何情況下,您不應該使用Boolean來保存數字值。In any case, you should not use Boolean to hold numeric values.

  • 類型字元。Type Characters. Boolean 沒有任何常值類型字元或識別項類型字元。Boolean has no literal type character or identifier type character.

  • Framework 型別。Framework Type. 在 .NET Framework 中對應的類型為 System.Boolean 結構。The corresponding type in the .NET Framework is the System.Boolean structure.

範例Example

在下列範例中,runningVBBoolean儲存是/否設定簡單的變數。In the following example, runningVB is a Boolean variable, which stores a simple yes/no setting.

Dim runningVB As Boolean  
' Check to see if program is running on Visual Basic engine.  
If scriptEngine = "VB" Then  
    runningVB = True  
End If  

另請參閱See also