Visual Basic 中的變數宣告Variable Declaration in Visual Basic

您可以宣告變數來指定其名稱和特性。You declare a variable to specify its name and characteristics. 變數的宣告語句是Dim 語句The declaration statement for variables is the Dim Statement. 其位置和內容會決定變數的特性。Its location and contents determine the variable's characteristics.

如需變數命名規則和考慮,請參閱宣告的元素名稱For variable naming rules and considerations, see Declared Element Names.

宣告層級Declaration Levels

區域變數和成員變數Local and Member Variables

區域變數是一個在程式內宣告的變數A local variable is one that is declared within a procedure. 成員變數是 Visual Basic 類型的成員;它是在類別、結構或模組內的模組層級宣告,而不是在該類別、結構或模組內部的任何程式內進行宣告。A member variable is a member of a Visual Basic type; it is declared at module level, inside a class, structure, or module, but not within any procedure internal to that class, structure, or module.

共用和執行個體變數Shared and Instance Variables

在類別或結構中,成員變數的類別取決於其是否為共用。In a class or structure, the category of a member variable depends on whether or not it is shared. 如果它是使用shared關鍵字來宣告,它就是共用變數,而且存在於類別或結構的所有實例之間共用的單一複本。If it is declared with the Shared keyword, it is a shared variable, and it exists in a single copy shared among all instances of the class or structure.

否則,它是執行個體變數,而且會為類別或結構的每個實例建立個別的複本。Otherwise it is an instance variable, and a separate copy of it is created for each instance of the class or structure. 執行個體變數的指定複本僅適用于其建立所在之類別或結構的實例。A given copy of an instance variable is available only to the instance of the class or structure in which it was created. 它與類別或結構的任何其他實例中的執行個體變數複本無關。It is independent of a copy of the instance variable in any other instance of the class or structure.

宣告資料類型Declaring Data Type

宣告語句中的As子句可讓您定義所宣告之變數的資料類型或物件類型。The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. 您可以為變數指定下列任何類型:You can specify any of the following types for a variable:

  • 基本資料類型,例如 BooleanLongDecimalAn elementary data type, such as Boolean, Long, or Decimal

  • 複合資料型別,例如陣列或結構A composite data type, such as an array or structure

  • 在您的應用程式或另一個應用程式中定義的物件類型或類別An object type, or class, defined either in your application or in another application

  • .NET Framework 類別,例如 LabelTextBoxA .NET Framework class, such as Label or TextBox

  • 介面類別型,例如 IComparableIDisposableAn interface type, such as IComparable or IDisposable

您可以在一個語句中宣告數個變數,而不需要重複資料類型。You can declare several variables in one statement without having to repeat the data type. 在下列語句中,ijk 的變數會宣告為類型 Integerlm 做為 Long,並 xySingleIn the following statements, the variables i, j, and k are declared as type Integer, l and m as Long, and x and y as Single:

Dim i, j, k As Integer  
' All three variables in the preceding statement are declared as Integer.  
Dim l, m As Long, x, y As Single  
' In the preceding statement, l and m are Long, x and y are Single.  

如需資料類型的詳細資訊,請參閱資料類型For more information on data types, see Data Types. 如需物件的詳細資訊,請參閱物件和類別使用元件進行程式設計For more information on objects, see Objects and Classes and Programming with Components.

區域類型推斷Local Type Inference

型別推斷用來判斷未使用 As 子句宣告之區域變數的資料類型。Type inference is used to determine the data types of local variables declared without an As clause. 編譯器會從初始化運算式的類型推斷變數的類型。The compiler infers the type of the variable from the type of the initialization expression. 這可讓您宣告變數,而不需要明確陳述型別。This enables you to declare variables without explicitly stating a type. 在下列範例中,num1num2 都是強型別做為整數。In the following example, both num1 and num2 are strongly typed as integers.

Public Sub inferenceExample()

    ' Using explicit typing.
    Dim num1 As Integer = 3

    ' Using local type inference.
    Dim num2 = 3

End Sub

如果您想要使用區欄位型別推斷,Option Infer 必須設定為 OnIf you want to use local type inference, Option Infer must be set to On. 如需詳細資訊,請參閱區域型別推斷Option Infer 陳述式For more information, see Local Type Inference and Option Infer Statement.

已宣告變數的特性Characteristics of Declared Variables

變數的存留期是可供使用的時間週期。The lifetime of a variable is the period of time during which it is available for use. 一般而言,只要宣告它的元素(例如程式或類別)仍存在,變數就會存在。In general, a variable exists as long as the element that declares it (such as a procedure or class) continues to exist. 如果變數不需要繼續存在於其包含專案的存留期以外,您就不需要在宣告中執行任何特殊動作。If the variable does not need to continue existing beyond the lifetime of its containing element, you do not need to do anything special in the declaration. 如果變數需要繼續存在的時間超過其包含的元素,您可以在其 Dim 語句中包含 StaticShared 關鍵字。If the variable needs to continue to exist longer than its containing element, you can include the Static or Shared keyword in its Dim statement. 如需詳細資訊,請參閱Visual Basic 中的存留期For more information, see Lifetime in Visual Basic.

變數的範圍是一組可以參考它而不限定其名稱的程式碼。The scope of a variable is the set of all code that can refer to it without qualifying its name. 變數的範圍是由宣告的位置決定。A variable's scope is determined by where it is declared. 位於指定區域的程式碼可以使用該區域中定義的變數,而不需要限定其名稱。Code located in a given region can use the variables defined in that region without having to qualify their names. 如需詳細資訊,請參閱 Scope in Visual BasicFor more information, see Scope in Visual Basic.

變數的存取層級是有權存取它的程式碼範圍。A variable's access level is the extent of code that has permission to access it. 這是由您在 Dim 語句中使用的存取修飾詞(例如公用用)所決定。This is determined by the access modifier (such as Public or Private) that you use in the Dim statement. 如需詳細資訊,請參閱Visual Basic 中的存取層級For more information, see Access levels in Visual Basic.

請參閱See also