宣告項目名稱 (Visual Basic)Declared Element Names (Visual Basic)

每個宣告的專案都有名稱, 也稱為「識別碼」 ( identifier), 這就是程式碼用來參考它的專案。Every declared element has a name, also called an identifier, which is what the code uses to refer to it.

規則Rules

Visual Basic 中的元素名稱必須遵守下列規則:An element name in Visual Basic must observe the following rules:

  • 其開頭必須是字母字元或底線 (_)。It must begin with an alphabetic character or an underscore (_).

  • 它必須只包含字母字元、十進位數和底線。It must only contain alphabetic characters, decimal digits, and underscores.

  • 如果開頭是底線, 它必須包含至少一個字母字元或十進位數。It must contain at least one alphabetic character or decimal digit if it begins with an underscore.

  • 長度不得超過1023個字元。It must not be more than 1023 characters long.

1023個字元的長度限制也適用于完整名稱的整個字串, 例如outerNamespace.middleNamespace.innerNamespace.thisClass.thisElementThe length limit of 1023 characters also applies to the entire string of a fully qualified name, such as outerNamespace.middleNamespace.innerNamespace.thisClass.thisElement.

下列範例顯示一些有效的元素名稱。The following example shows some valid element names.

aB123__45

_567

下列範例顯示一些不正確元素名稱。The following example shows some invalid element names. 第一個只包含底線, 第二個開頭為十進位數, 而第三個包含不正確字元 ($)。The first contains only an underscore, the second begins with a decimal digit, and the third contains an invalid character ($).

' Three INVALID element names

_

12ABC

xyz$wv

警告

以底線 (_) 開頭的元素名稱不是語言獨立性和與語言無關的元件(CLS) 的一部分, 因此符合 CLS 標準的程式碼無法使用定義這類名稱的元件。Element names starting with an underscore (_) are not part of the Language Independence and Language-Independent Components (CLS), so CLS-compliant code cannot use a component that defines such names. 不過, 元素名稱中任何其他位置的底線都符合 CLS 標準。However, an underscore in any other position in an element name is CLS-compliant.

名稱長度指導方針Name Length Guidelines

實際上, 您的名稱應該越短越好, 同時仍能清楚地識別元素的本質。As a practical matter, your name should be as short as possible while still clearly identifying the nature of the element. 這可以改善程式碼的可讀性, 並縮短行長度和原始程式檔大小。This improves the readability of your code and reduces line length and source-file size.

另一方面, 您的名稱應該不會太短, 因為它不會適當地描述專案所代表的內容, 以及您的程式碼使用它的方式。On the other hand, your name should not be so short that it does not adequately describe what the element represents and how your code uses it. 這對於您的程式碼可讀性很重要。This is important for the readability of your code. 如果有人嘗試瞭解它, 或如果您在撰寫完之後, 想要很長的時間查看它, 適當的元素名稱可以節省大量的時間。If somebody else is trying to understand it, or if you yourself are looking at it a long time after you wrote it, suitable element names can save a considerable amount of time.

轉義的名稱Escaped Names

一般來說, 專案名稱不能符合 Visual Basic 所保留的任何關鍵字, 例如Case或。 FriendGenerally, an element name must not match any of the keywords reserved by Visual Basic, such as Case or Friend. 不過, 您可以定義以方括弧 ([ ]) 括住的轉義名稱However, you can define an escaped name, which is enclosed by brackets ([ ]). 轉義的名稱可以符合任何 Visual Basic 關鍵字, 因為方括弧會移除任何不明確的。An escaped name can match any Visual Basic keyword, since the brackets remove any ambiguity. 當您稍後在程式碼中參考名稱時, 也會使用括弧。You also use the brackets when you refer to the name later in your code.

一般而言, 只有在下列情況下, 才應該使用轉義名稱:In general, you should use escaped names only when:

  • 您的程式碼已從舊版的 Visual Basic 進行遷移, 但未保留做為名稱使用的關鍵字;或Your code has migrated from a previous version of Visual Basic that did not reserve the keyword being used as a name; or

  • 您正在使用以另一種語言撰寫的程式碼, 其中指定的關鍵字不是保留的。You are working with code written in another language in which the given keyword is not reserved.

否則, 如果元素的名稱與關鍵字衝突, 您應該考慮重新命名專案。Otherwise, you should consider renaming the element if its name conflicts with a keyword. 整合式開發環境 (IDE) 提供簡單的方法來執行此動作。The integrated development environment (IDE) provides an easy way to do this. 如需詳細資訊, 請參閱重構For more information, see Refactoring.

名稱區分大小寫Case Sensitivity in Names

Visual Basic 中的元素名稱不區分大小寫。Element names in Visual Basic are case-insensitive. 這表示當編譯器只比較字母大小寫不同的兩個名稱時, 它會將它們解讀為相同的名稱。This means that when the compiler compares two names that differ in alphabetic case only, it interprets them as the same name. 例如,它會將 ABCabc 視為相同的宣告元素。For example, it considers ABC and abc to refer to the same declared element.

不過, common language runtime (CLR) 使用區分大小寫的系結。However, the common language runtime (CLR) uses case-sensitive binding. 因此,當您產生一個組件或 DLL 並讓其他組件使用時,您的名稱將會區分大小寫。Therefore, when you produce an assembly or a DLL and make it available to other assemblies, your names are no longer case-insensitive. 例如,如果您使用名為 ABC的元素來定義類別,而其他組件透過 Common Language Runtime 使用您的類別,則這些組件必須以 ABC來表示該元素。For example, if you define a class with an element called ABC, and other assemblies make use of your class through the common language runtime, they must refer to the element as ABC. 如果您接著重新編譯類別, 並將專案的名稱變更abc為, 則其他使用您類別的元件將無法再存取該元素。If you subsequently recompile your class and change the element's name to abc, the other assemblies using your class could no longer access that element. 因此,當您發行組件的更新版本時,不應該變更任何公用元素的字母大小寫。Therefore, when you release an updated version of an assembly, you should not change the alphabetic case of any public elements.

名稱和地區設定Names and Locales

名稱的比較與地區設定無關。Comparison of names is independent of locale. 如果兩個名稱符合一個地區設定, 則會保證所有地區設定的相符。If two names match in one locale, they are guaranteed to match in all locales.

另請參閱See also