# Const 陳述式 (Visual Basic)Const Statement (Visual Basic)

## 語法Syntax

[ <attributelist> ] [ accessmodifier ] [ Shadows ]
Const constantlist


## 組件Parts

attributelist

accessmodifier

Shadows

constantlist

constant [ , constant ... ]constant [ , constant ... ]

constantname [ As datatype ] = initializerconstantname [ As datatype ] = initializer

constantname 必要。Required. 常數的名稱。Name of the constant.
datatype 如果 Option Strict On，則為必要。Required if Option Strict is On. 常數的資料類型。Data type of the constant.
initializer 必要。Required. 在編譯時期評估並指派給常數的運算式。Expression that is evaluated at compile time and assigned to the constant.

## 規則Rules

• 宣告內容。Declaration Context. 在任何程式以外的模組層級宣告的常數是成員常數;它是宣告它的類別、結構或模組的成員。A constant declared at module level, outside any procedure, is a member constant; it is a member of the class, structure, or module that declares it.

在程式層級宣告的常數是本機常數;它是在宣告它的程式或區塊的本機。A constant declared at procedure level is a local constant; it is local to the procedure or block that declares it.

• 特性.Attributes. 您只能將屬性套用至成員常數，而不能套用至本機常數。You can apply attributes only to member constants, not to local constants. 屬性會將資訊提供給元件的中繼資料，這對暫存儲存體（例如本機常數）沒有意義。An attribute contributes information to the assembly's metadata, which is not meaningful for temporary storage such as local constants.

• 修改.Modifiers. 根據預設，所有常數都是 SharedStaticReadOnlyBy default, all constants are Shared, Static, and ReadOnly. 宣告常數時，您無法使用任何這些關鍵字。You cannot use any of these keywords when declaring a constant.

在程式層級中，您無法使用 Shadows 或任何存取修飾詞來宣告本機常數。At procedure level, you cannot use Shadows or any access modifiers to declare local constants.

• 多個常數。Multiple Constants. 您可以在同一個宣告語句中宣告數個常數，為每個常數指定 constantname 部分。You can declare several constants in the same declaration statement, specifying the constantname part for each one. 以逗號分隔多個常數。Multiple constants are separated by commas.

## 資料類型規則Data Type Rules

• 資料類型。Data Types. Const 語句可以宣告變數的資料類型。The Const statement can declare the data type of a variable. 您可以指定任何資料類型或列舉的名稱。You can specify any data type or the name of an enumeration.

• 預設類型。Default Type. 如果您未指定 datatype，常數會採用 initializer的資料類型。If you do not specify datatype, the constant takes the data type of initializer. 如果您同時指定 datatypeinitializerinitializer 的資料類型必須可轉換為 datatypeIf you specify both datatype and initializer, the data type of initializer must be convertible to datatype. 如果 datatypeinitializer 都不存在，則資料類型會預設為 [Object]。If neither datatype nor initializer is present, the data type defaults to Object.

• 不同的類型。Different Types. 您可以針對您所宣告的每個變數使用個別的 As 子句，為不同的常數指定不同的資料類型。You can specify different data types for different constants by using a separate As clause for each variable you declare. 但是，您無法使用 common As 子句，將數個常數宣告為相同類型。However, you cannot declare several constants to be of the same type by using a common As clause.

• 初始.Initialization. 您必須初始化 constantlist中每個常數的值。You must initialize the value of every constant in constantlist. 您可以使用 initializer 來提供要指派給常數的運算式。You use initializer to supply an expression to be assigned to the constant. 運算式可以是常值的任何組合、已定義的其他常數，以及已定義的列舉成員。The expression can be any combination of literals, other constants that are already defined, and enumeration members that are already defined. 您可以使用算術和邏輯運算子來結合這類元素。You can use arithmetic and logical operators to combine such elements.

您不能在 initializer中使用變數或函數。You cannot use variables or functions in initializer. 不過，您可以使用轉換關鍵字，例如 CByteCShortHowever, you can use conversion keywords such as CByte and CShort. 如果您以常數 StringChar 引數呼叫它，您也可以使用 AscW，因為這可以在編譯時期進行評估。You can also use AscW if you call it with a constant String or Char argument, since that can be evaluated at compile time.

## 行為Behavior

• 範圍.Scope. 本機常數只能從其程式或區塊記憶體取。Local constants are accessible only from within their procedure or block. 成員常數可從其類別、結構或模組內的任何位置存取。Member constants are accessible from anywhere within their class, structure, or module.

• 加.Qualification. 類別、結構或模組之外的程式碼必須使用該類別、結構或模組的名稱來限定成員常數的名稱。Code outside a class, structure, or module must qualify a member constant's name with the name of that class, structure, or module. 程式或區塊外的程式碼無法參考該程式或區塊中的任何本機常數。Code outside a procedure or block cannot refer to any local constants within that procedure or block.

## 範例Example

' The following statements declare constants.
Const maximum As Long = 459
Public Const helpString As String = "HELP"
Private Const startValue As Integer = 5


## 範例Example

Const naturalLogBase As Object = CDec(2.7182818284)
MsgBox("Run-time type of constant naturalLogBase is " &
naturalLogBase.GetType.ToString())