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

宣告並定義一或多個常數。Declares and defines one or more constants.

語法Syntax

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

組件Parts

attributelist
選擇性。Optional. 套用至此語句中宣告之所有常數的屬性清單。List of attributes that apply to all the constants declared in this statement. 請參閱以角括弧括住的屬性清單(「<」和「>」)。See Attribute List in angle brackets ("<" and ">").

accessmodifier
選擇性。Optional. 使用此項來指定哪些程式碼可以存取這些常數。Use this to specify what code can access these constants. 可以是公用受保護Friend受保護的 Friend用或私用保護Can be Public, Protected, Friend, Protected Friend, Private, or Private Protected.

Shadows
選擇性。Optional. 使用此專案來重新宣告和隱藏基類中的程式設計項目。Use this to redeclare and hide a programming element in a base class. 請參閱ShadowsSee Shadows.

constantlist
必要。Required. 在此語句中宣告的常數清單。List of constants being declared in this statement.

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

每個 constant 都具有下列語法和組件:Each constant has the following syntax and parts:

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

組件Part 描述Description
constantname 必要。Required. 常數的名稱。Name of the constant. 請參閱 Declared Element NamesSee Declared Element Names.
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.

備註Remarks

如果您的應用程式中有永不變更的值,您可以定義已命名的常數,並將其取代為常值。If you have a value that never changes in your application, you can define a named constant and use it in place of a literal value. 名稱比值更容易記住。A name is easier to remember than a value. 您可以只定義常數一次,並將其用於程式碼中的許多位置。You can define the constant just once and use it in many places in your code. 如果在較新的版本中,您必須重新定義值,則 Const 語句是您唯一需要進行變更的位置。If in a later version you need to redefine the value, the Const statement is the only place you need to make a change.

您只能在模組或程式層級使用 ConstYou can use Const only at module or procedure level. 這表示變數的宣告內容必須是類別、結構、模組、程式或區塊,而且不能是原始程式檔、命名空間或介面。This means the declaration context for a variable must be a class, structure, module, procedure, or block, and cannot be a source file, namespace, or interface. 如需詳細資訊,請參閱宣告內容和預設存取層級For more information, see Declaration Contexts and Default Access Levels.

本機常數(在程式內)預設為公用存取,而且您不能在其上使用任何存取修飾詞。Local constants (inside a procedure) default to public access, and you cannot use any access modifiers on them. 類別和模組成員常數(在任何程式之外)預設為私用存取,而結構成員常數則預設為公用存取。Class and module member constants (outside any procedure) default to private access, and structure member constants default to public access. 您可以使用存取修飾詞來調整其存取層級。You can adjust their access levels with the access modifiers.

規則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

下列範例會使用 Const 語句來宣告用來取代常值的常數。The following example uses the Const statement to declare constants for use in place of literal values.

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

範例Example

如果您定義了資料類型 Object的常數,Visual Basic 編譯器會提供 initializer的類型,而不是 ObjectIf you define a constant with data type Object, the Visual Basic compiler gives it the type of initializer, instead of Object. 在下列範例中,常數 naturalLogBase 具有 Decimal的執行時間類型。In the following example, the constant naturalLogBase has the run-time type Decimal.

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

上述範例會在GetType 運算子傳回的 Type 物件上使用 ToString 方法,因為 Type 無法使用 CStr轉換成 StringThe preceding example uses the ToString method on the Type object returned by the GetType Operator, because Type cannot be converted to String using CStr.

請參閱See also