VBA中變量和常量的命名規則

Variable Naming Conventions

You can call your variables and user-defined functions anything you want, except where there is a clash
with VBA keywords and function names. However, many programmers adopt a system whereby the
variable or object type is included, in abbreviated form, in the variable name, usually as a prefix, so
instead of declaring:
Dim SalesData As Double
you can use:
Dim dSalesData As Double
Wherever dSalesData appears in your code, you will be reminded that the variable is of type Double.
Alternatively, you could use this line of code:
Dim dblSalesData As Double
For the sake of simplicity, this approach has not been used so far in this chapter, but from here onward,
the examples will use a system to create variable names. This is the convention used in this book:
❑ One-letter prefixes for the common data types:
Dim iColumn As Integer
Dim lRow As Long
Dim dProduct As Double
Dim sName As String
Dim vValue As Variant
Dim bChoice As Boolean
❑ Two- or three-letter prefixes for object types:
Dim objExcel As Object
Dim rngData As Range
Dim wkbSales As Workbook
In addition to these characters, a lowercase a will be inserted in front of array variables, which are discussed
later in this chapter. If the variable is a module-level variable, it will also have a lowercase m
placed in front of it. If it is a public variable, it will have a lowercase g (for global) placed in front of it.
For example, malEffect would be a module-level array variable containing long integer values.




通常,各種命名規則都用小寫的前綴或後綴來指定變量的類型和作用範圍。變量本身應當用有意義的名稱來說明它是什麼或要做什麼。多字變量名由幾個字連接在一起,每個字的第一個字母大寫,並且不使用下劃線。如果您用過變量名模板,其格式應當如 prefixNoun 或 prefixNounVerb

常量名也應當有一定的意義,格式爲 NOUN 或 NOUN_VERB常量名均爲大寫,字之間用下劃線分隔。儘管給常量名添加字符以指定數據類型和作用範圍的做法不存在技術性錯誤,但通常不這麼做。常量與變量都是數據的符號表示,在此意義上,二者完全相同。區別在於變量可以變化,而常量則保持不變。

變量名和常量名最多可以包含 255 個字符,但是,超過 25 到 30 個字符的名稱比較笨拙。此外,要想取一個有實際意義的名稱,清楚地表達變量或常量的用途,25 或 30 個字符應當足夠了。

1 變量名

變量名使用大小寫混合的格式(Noun 或 NounVerb),以此指定變量是什麼以及它要做什麼。大小寫混合格式被用作變量名的說明部分,在這裏每個字的第一個字母大寫而其餘字母小寫。

變量名還有兩個或三個字符的前綴,用來指定變量的數據類型。例如,以下語句聲明的變量都用前綴指定變量的數據類型:

  1. Dim strRecipientName As String 
  2. Dim intItemsProcessed As Integer 
  3. Dim blnContinueProcessing As Boolean

兩個字符的前綴通常用來指定 Office 應用程序對象類型。例如:

  1. Dim xlApp As Excel.Application 
  2. Dim olNameSpace As Outlook.NameSpace 
  3. Dim wdNewDoc As Word.Document

在聲明一般變量或對象變量時,要使用 "obj" 前綴。即使要創建代表 Microsoft® Office 應用程序的晚期綁定對象變量,也應當使用該前綴。例如:

  1. Dim objXLApp As Object 
  2. Dim objWDDocument As Object 
  3. Dim objOLMailItem As Object

全局變量和模塊級變量還要再加一個字符前綴來表示它們的作用範圍。變量的作用範圍定義了變量的生存期和可見性。全局變量和模塊級變量都有永久的生存期。就是說,只要應用程序不關閉,變量就一直佔用分配給它的內存。過程內聲明的變量只在聲明它們的過程中有效,其生存期爲過程代碼的執行時間。但是,用關鍵字 Static 來聲明變量的情況例外。

全局變量有小寫的 "g" 作前綴,並在模塊的“聲明”部分用 Public 語句聲明。它對應用程序中所有模塊內的所有過程可見。例如,Public gstrPathToDataSource As String 是一個全局變量,它包含了一個字符串,而該字符串是應用程序中所使用的數據源的路徑。

應當儘可能始終使用最小的作用範圍來定義變量。只有在找不到其他途徑來共享變量所包含的數據時,才應當使用全局變量。全局變量會使代碼難以理解和維護。如果您使用的全局變量過多,且未經過仔細挑選,則可能需要重新設計代碼,以便減少全局變量。

模塊級變量有小寫的 "m" 前綴,是在模塊的“聲明”部分用 Dim 或 Private 語句聲明的。它們對聲明時所處模塊中的所有過程可見。例如,Dim mrstCustomerRecords As ADODB.Recordset 是用於客戶記錄的模塊級對象變量。在類模塊中,用 Private 語句聲明的模塊級變量具有前綴 "p_"。類模塊中公共的模塊級變量作爲類屬性出現,不應當有任何前綴表示它們的數據類型或作用範圍

過程級變量是在過程內用 Dim 語句創建的。例如,Dim intCurrentMailItem As Integer 是一個用作循環計數器的過程級變量。此外,過程級變量可以用 Static 關鍵字聲明。即使聲明靜態變量的過程已經結束運行,靜態變量仍然會保留它們的值。靜態的過程級變量具有小寫的 "s" 前綴。例如,Static scurTotalSales As Currency 將創建一個過程級靜態變量,用於在一個計算當前銷售額的過程中保存累加和。

用戶定義類型變量在模塊的“聲明”部分中聲明,聲明時名稱全部大寫並且後跟 "_TYPE"。可以按如下方式聲明用戶定義類型:

  1. Type EMPLOYEEINFO_TYPE 
  2.     strFullName As String 
  3.     lngEmployeeID As Long 
  4.     datStartDate As Date 
  5.     strDepartmentCode As String * 4 
  6.     curSalary As Currency 
  7. End Type

應當使用 "udt" 前綴來聲明 EMPLOYEEINFO_TYPE 類型的模塊級變量。例如,

  1. Dim mudtEmployeeRecord As EMPLOYEEINFO_TYPE

數組變量有小寫 "a" 作前綴,而且,如果不是 Variant 型,則後跟一對圓括號。數組是可以包含多個值的變量。數組變量要使用 Dim 語句聲明,例如,Dim alngNum() 是長整型的數組變量。當需要存儲多個相同類型的值,而又不希望爲它們分別創建單個變量的時候,數組會非常有用。

下面是一些使用前述常規命名規範的變量名稱示例:

2 常量名 
常量使用由全部大寫的多個字組成的說明型名稱,每個字之間用下劃線分隔。聲明常量時,要使用 Const 語句以及常量名、它的數據類型和它的值。例如,如下常量可以在模塊的“聲明”部分聲明,以提供應用程序所使用的數據源的路徑:

  1. Public Const DATABASE_PATH As String = "C:/Solutions/Source/AppData.mdb"

注意 如果使用 Public 關鍵字來聲明常量,那麼應用程序中任何模塊的任何過程都可以使用該常量。如果不使用 Public 關鍵字,那麼常量只有模塊級的作用範圍,這意味着它只能被聲明它的模塊所包含的過程使用。如果常量是在過程內聲明的,那麼它只能用於該過程內的代碼,並且生存期僅爲過程內代碼的執行期間。

Prefixes like i, s, g, and m can be very useful in understanding the value and the scope of a constant. For example, gsNEW_LINE new line character string (g indicates that it is global to entire application and s indicates that this is a string)

下面是一些使用前述常規命名規範的常量名稱示例:

  1. ACCESS_CONNECTSTRING 
  2. API_MAX_STRINGBUFFER 
  3. SQL_STRING

注意 如果在類模塊中創建公共枚舉常量,可以使用不同的命名規則來將它們與其他常量區分開來。Constants should be prefixed by the scope prefixes m or g for module or global, respectively. A constant is indicated by appending the letter c to the end of the data type or it can have the generic tag con. For examples, gintcDiscount. g is the scope, intc indicates the datatype and that it is a constant, with the base name of Discount. conDiscount names the same constant, but conveys less information. mdblcPi indicates module level, double integer constant with the base name Pi.

除了您自己聲明的常量外,Microsoft® Visual Basic® for Applications (VBA) 以及每個 Microsoft® Office 應用程序都包含了有預定義值的內置常量(即內部常量)。應當始終使用內部常量,而不用它們代表的值。同用戶定義的常量一樣,使用內部常量的好處是它們可以使您的代碼更容易被理解。例如,比較下面兩組代碼示例,一組使用內部常量,另一組則沒有使用內部常量。請判斷內部常量是否可以使代碼更容易理解。

  1. If MsgBox("Proceed Now?", 48 + 512 + 3 + 16384, "Continue?")= 7 Then
  2.     DoCmd.OpenForm "Customers", 0, , , 1, 3
  3. End If
  4. If MsgBox("Proceed Now?", vbExclamation + vbDefaultButton3 + _ 
  5.         vbYesNoCancel + vbMsgBoxHelpButton, "Continue?")= vbNo Then 
  6.     DoCmd.OpenForm "Customers", acNormal, , , acFormEdit, acDialog 
  7. End If

要查看VBA和每個Office應用程序的內部常量的完整列表,請打開“對象瀏覽器”,然後從“項目/庫”對話框中選擇合適的類型庫,再在“搜索”文本框中鍵入相應的常量前綴,然後在“對象瀏覽器”工具欄上單擊“搜索”。下表是從內部常量的完整列表中提取的部分示例。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章