Share via


已宣告之項目的參考 (Visual Basic)

當您的程式碼參考宣告的元素時,Visual Basic 編譯器會比對參考中名稱與該名稱的適當宣告。 如果宣告多個具有相同名稱的元素,您可以藉由「限定」其名稱來控制要參考哪些元素。

編譯器會嘗試比對名稱宣告的名稱參考與最窄的範圍。 這表示編譯器會從程式碼開始進行參考,並透過連續的包含元素層級向外運作。

下列範例顯示兩個相同名稱變數的參考。 此範例會在模組 container 中宣告兩個變數,每個變數皆命名為 totalCount。 當程序 showCount 顯示 totalCount 不含限定性時,Visual Basic 編譯器會解析具有最窄範圍的宣告參考,也就是 showCount 內的本機宣告。 當編譯器以包含的模組 container 限定 totalCount時,會以更廣範圍解析宣告參考。

' 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  

限定元素名稱

如果您想要覆寫此搜尋程式,並指定在更廣範圍中宣告的名稱,則必須使用更廣範圍包含元素來「限定」名稱。 在某些情況下,您可能也必須限定包含的元素。

限定名稱表示在來源陳述式中前面加上可識別目標元素定義位置的資訊。 這項資訊稱為「限定性字串」。 限定性字串可以包含一或多個命名空間和模組、類別或結構。

限定性字串應該明確指定包含目標元素的模組、類別或結構。 容器可能接著位於另一個包含元素中,通常是命名空間。 您可能需要在限定性字串中包含數個包含元素。

若要藉由限定其名稱來存取宣告的元素

  1. 判斷已定義元素的位置。 這可能包括命名空間,甚至是命名空間的階層。 在最低層級命名空間內,元素必須包含在模組、類別或結構中。

    ' 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  
    
  2. 根據目標元素的位置來判斷限定性路徑。 從最高層級命名空間開始,繼續進行最低層級命名空間,並以包含目標元素的模組、類別或結構結尾。 路徑中的每個元素都必須包含其後置元素。

    outerSpaceinnerSpaceholdsTotalstotals

  3. 準備目標元素的限定性字串。 將句點 (.) 放在路徑中每個元素之後。 您的應用程式必須能夠存取限定性字串中的每個元素。

    outerSpace.innerSpace.holdsTotals.totals.  
    
  4. 以一般方式撰寫參考目標元素的運算式或指派陳述式。

    grandTotal = 9000  
    
  5. 在目標元素名稱前面加上限定性字串。 名稱應該緊接在包含元素的模組、類別或結構後的句號 (.)。

    ' Assume the following module is part of your code.  
    Module accessGrandTotal  
        Public Sub setGrandTotal()  
            outerSpace.innerSpace.holdsTotals.totals.grandTotal = 9000  
        End Sub  
    End Module  
    
  6. 編譯器會使用限定性字串來尋找清楚、明確的宣告,以供比對目標元素參考。

如果您的應用程式可以存取多個具有相同名稱的程式設計元素,您可能也必須限定名稱參考。 例如,System.Windows.FormsSystem.Web.UI.WebControls 命名空間都包含類別 Label (System.Windows.Forms.LabelSystem.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 中的遮蔽功能

另請參閱