當程式碼參考宣告的元素時,Visual Basic 編譯器會比對參考中的名稱與該名稱的適當宣告。 如果以相同名稱宣告多個元素,您可以藉由 限定 其名稱來控制要參考哪些元素。
編譯程式會嘗試將名稱參考與名稱宣告匹配在最窄的範圍內。 這表示它會從程式代碼開始建立參考,並透過連續的包含元素層級向外展開。
下列範例顯示引用至同名的兩個變數。 這個範例會在模組totalCount中的不同範圍層級宣告兩個變數,每個變數分別命名container為 。 當程序 showCount 未經過限定條件就顯示 totalCount 時,Visual Basic 編譯器會參照範圍最窄的宣告來解析該引用,即 showCount 內的本地宣告。 當限定totalCount為包含模組container的一部分時,編譯器會根據更大範圍的宣告來解析參考。
' Assume these two modules are both in the same assembly.
Module container
Public totalCount As Integer = 1
Public Sub showCount()
Dim totalCount As Integer = 6000
' The following statement displays the local totalCount (6000).
MsgBox("Unqualified totalCount is " & CStr(totalCount))
' The following statement displays the module's totalCount (1).
MsgBox("container.totalCount is " & CStr(container.totalCount))
End Sub
End Module
Module callingModule
Public Sub displayCount()
container.showCount()
' The following statement displays the containing module's totalCount (1).
MsgBox("container.totalCount is " & CStr(container.totalCount))
End Sub
End Module
元素名稱限定
如果您想要覆蓋此搜尋程序並指定在較廣泛範圍中宣告的名稱,您必須將名稱與該較廣範圍的包含元素進行限定。 在某些情況下,您可能也必須限定包含的元素。
限定名稱表示在來源語句中前面加上可識別目標元素定義位置的資訊。 這項信息稱為 限定字串。 它可以包含一或多個命名空間和模組、類別或結構。
限定性字串應該明確指定包含目標元素的模組、類別或結構。 容器可能接著位於另一個包含元素中,通常是命名空間。 您可能需要在限定字串中包含數個包含元素。
若要藉由限定名稱來存取宣告的元素
找出元素已定義的位置。 這可能包括命名空間,甚至是命名空間的階層。 在最低層級命名空間內,元素必須包含在模組、類別或結構中。
' Assume the following hierarchy exists outside your code. Namespace outerSpace Namespace innerSpace Module holdsTotals Public Structure totals Public thisTotal As Integer Public Shared grandTotal As Long End Structure End Module End Namespace End Namespace根據目標元素的位置確定資格路徑。 從最上層命名空間開始,繼續進行最低層級命名空間,並以包含目標元素的模組、類別或結構結尾。 路徑中的每個元素都必須包括其後面的元素。
outerSpaceinnerSpace→ →holdsTotals→totals準備目標元素的限定性字串。 將句點 (
.) 放在路徑中的每個元素之後。 您的應用程式必須能夠存取限定性字串中的每個元素。outerSpace.innerSpace.holdsTotals.totals.以一般方式撰寫參考目標項目的表達式或指派語句。
grandTotal = 9000在目標專案名稱前面加上限定字串。 名稱應該緊接在包含 元素的模組、類別或結構後面的句號 (
.)。' Assume the following module is part of your code. Module accessGrandTotal Public Sub setGrandTotal() outerSpace.innerSpace.holdsTotals.totals.grandTotal = 9000 End Sub End Module編譯程式會使用資格字串來尋找與目標元素參考相匹配的清楚明確宣告。
如果您的應用程式可以存取多個具有相同名稱的程式元素,您可能也必須限定名稱參考。 例如, System.Windows.Forms 和 System.Web.UI.WebControls 命名空間都包含 類別 Label (System.Windows.Forms.Label 和 System.Web.UI.WebControls.Label)。 如果您的應用程式同時使用這兩者,或它定義自己的 Label 類別,您必須區分不同的 Label 物件。 在變數宣告中包含命名空間或匯入別名。 下列範例使用匯入別名。
' The following statement must precede all your declarations.
Imports win = System.Windows.Forms, web = System.Web.UI.WebControls
' The following statement references the Windows.Forms.Label class.
Dim winLabel As New win.Label()
其他所含元素的成員
當您使用另一個類別或結構的非共享成員時,您必須先使用指向類別或結構的實例的變數或表達式來限定成員名稱。 在下列範例中, demoClass 是名為 class1的類別實例。
Dim demoClass As class1 = New class1()
demoClass.someSub[(argumentlist)]
您無法使用類別名稱本身來限定不是 共享的成員。 您必須先在物件變數中建立實例(在此案例 demoClass中為 ),然後依變數名稱加以參考。
如果類別或結構有 Shared 成員,您可以使用類別或結構名稱或指向 實例的變數或表達式來限定該成員。
模組沒有任何個別的實例,而且其所有成員預設為 Shared 。 因此,您會使用模組名稱來限定模組成員。
以下範例顯示對模組成員程序的具體參考。 此範例會在專案中的不同模組中宣告兩 Sub 個名為 perform的程式。 每個元素都可以在自己的模組中不加限定地指定,但如果要從其他地方引用,則必須加以限定。 因為 module3 中的最終引用未能限定 perform,所以編譯器無法解析該引用。
' Assume these three modules are all in the same assembly.
Module module1
Public Sub perform()
MsgBox("module1.perform() now returning")
End Sub
End Module
Module module2
Public Sub perform()
MsgBox("module2.perform() now returning")
End Sub
Public Sub doSomething()
' The following statement calls perform in module2, the active module.
perform()
' The following statement calls perform in module1.
module1.perform()
End Sub
End Module
Module module3
Public Sub callPerform()
' The following statement calls perform in module1.
module1.perform()
' The following statement makes an unresolvable name reference
' and therefore generates a COMPILER ERROR.
perform() ' INVALID statement
End Sub
End Module
項目的參考
若要使用在另一個項目中定義的 Public 元素,您必須先設定該專案元件或類型連結庫的 參考 。 若要設定參考,請按兩下 [專案] 功能表上的 [新增參考],或使用 -reference (Visual Basic) 命令行編譯程序選項。
例如,您可以使用 .NET Framework 的 XML 物件模型。 如果您設定命名空間中的 System.Xml 參考,您可以宣告並使用其任何類別,例如 XmlDocument。 下列範例使用 XmlDocument。
' Assume this project has a reference to System.Xml
' The following statement creates xDoc as an XML document object.
Dim xDoc As System.Xml.XmlDocument
匯入包含的元素
您可以使用Imports 語句 (.NET 命名空間和類型)來匯入包含您想使用的模組或類別的命名空間。 這可讓您參考匯入命名空間中定義的元素,而不需要完整限定其名稱。 下列範例會重寫上一個範例以匯入 System.Xml 命名空間。
' Assume this project has a reference to System.Xml
' The following statement must precede all your declarations.
Imports System.Xml
' The following statement creates xDoc as an XML document object.
Dim xDoc As XmlDocument
此外, Imports 語句可以為每個匯入的命名空間定義 匯入別名 。 這可讓原始程式碼更短且更容易閱讀。 下列範例會重寫上一個範例,以 xD 作為命名空間的 System.Xml 別名。
' Assume this project has a reference to System.Xml
' The following statement must precede all your declarations.
Imports xD = System.Xml
' The following statement creates xDoc as an XML document object.
Dim xDoc As xD.XmlDocument
Imports語句不會讓來自其他項目的元素可供您的應用程式使用。 也就是說,它不會取代設置參考點。 匯入命名空間只會移除限定該命名空間中所定義名稱的需求。
您也可以使用 Imports 語句匯入模組、類別、結構和列舉。 然後,您可以使用這類匯入項目的成員,而不需要限制。 不過,您必須總是使用能夠評估出類別或結構實例的變數或表示式來限定類別和結構的非共用成員。
命名指導方針
當您定義兩個或多個具有相同名稱的程式設計專案時,如果編譯器在解析該名稱的參考時出現衝突,就會導致名稱模棱兩可的情況。 如果範圍中有多個定義,或者範圍內沒有定義,則參考無法解決。 如需範例,請參閱本說明頁面上的「合格參考範例」。
您可以藉由提供所有元素的唯一名稱來避免名稱模棱兩可。 然後,您可以參考任何元素,而不需使用命名空間、模組或類別來限定其名稱。 您也會減少不小心參考錯誤元素的機會。
陰影
當兩個程式設計元素共用相同的名稱時,其中一個元素可以隱藏或 遮蔽另一個。 被遮蔽的元素無法供參考;相反地,當您的程式代碼使用被遮蔽的元素名稱時,Visual Basic 編譯程式會將它解析為遮蔽元素。 如需範例的詳細說明,請參閱 Visual Basic 中的陰影。