共用方式為


當多個變數擁有相同名稱時解析參考

更新:2007 年 11 月

當編譯器嘗試將名稱參考對應至名稱宣告時,它會尋找「最窄範圍」的對應宣告。這代表編譯器會從使用參考的程式碼開始,依循包含項目的連續層級向外運作。

如果要覆寫這個搜尋程序並指定較廣範圍中所宣告的名稱,則必須以較廣範圍的包含項目來「限定」名稱。在某些情況下,您可能還必須限定包含項目。如需名稱限定的詳細資訊,請參閱 HOW TO:限定宣告的項目名稱

如果應用程式存取一個以上的同名程式設計項目,則可能也必須限定名稱參考。相關的範例,請參考本說明頁面的「名稱相同的類別」以及 HOW TO:區分兩個相同名稱的元素

最窄範圍

下列範例顯示對兩個具有相同名稱之變數的參考。

' 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

上述範例中宣告兩個名為 totalCount 的變數,它們位於模組 container 中不同的範圍層級。當程序 showCount 以不限定名稱方式顯示 totalCount 時,Visual Basic 編譯器會將參考解析到具有最窄範圍的宣告,也就是 showCount 內的區域宣告。當以包含模組 container 限定 totalCount 時,編譯器會將參考解析到較廣範圍的宣告。

其他包含項目的成員

當您使用其他類別或結構中的非共用成員時,必須先以指向該類別或結構的執行個體之變數或運算式完整描述成員名稱。在下列範例中,demoClass 是一個稱為 class1 的類別執行個體。

Dim demoClass As class1 = New class1()
demoClass.someSub[(argumentlist)]

您不能用類別名稱本身來限定非 Shared (Visual Basic) 成員名稱。必須先建立物件變數的執行個體 (本案例中的 demoClass),然後以變數名稱進行參考。

如果類別或結構具有 Shared 成員,就可以使用類別或結構名稱或者是指向執行個體的變數或運算式,來限定該成員。

模組並沒有任何個別的執行個體,而其所有成員都預設值為 Shared。因此,您可以使用模組名稱來限定模組成員。

限定參考的範例

下列範例顯示對模組成員程序的限定參考。

' 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

上述範例中宣告兩個 Sub 程序,都命名為 perform,但位於專案的不同模組中。每一個都可以在自己的模組中指定,而不需要完整名稱,但若自其他地方參考則必須為完整名稱。因為 module3 中最後的參考並沒有限定 perform,所以編譯器無法解析該參考。

專案參考

若要使用其他專案中定義的 Public (Visual Basic) 項目,您必須對該專案的組件或型別程式庫設定「參考」。若要設定參考,請按一下 [專案] 功能表上的 [加入參考],或使用 /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 陳述式來匯入模組、類別、結構和列舉。之後不需要完整描述名稱就可以使用這些匯入項目的成員。但是,您必須永遠要使用評估為類別或結構執行個體的變數或運算式,來限定類別和結構的非共用成員名稱。

名稱相同的類別

當您建立一個物件的新執行個體時,可能必須以類別所屬的命名空間或型別程式庫完整描述類別名稱。例如,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()

命名方針

如果您定義兩個以上具有相同名稱的程式設計項目,則當編譯器嘗試解析該名稱的參考時,可能會造成「名稱語意模糊」。當範圍中有一個以上的定義或是沒有定義時,就無法解析參考。相關的範例,請參考本說明頁面的「限定參考的範例」。

您可以為每一個項目提供專屬的名稱,以避免名稱語意模糊。那麼,不必使用命名空間、模組或類別限定名稱,就可以使用任何項目的參考。這樣也能降低不小心參考到錯誤項目的機會。

請參閱

工作

HOW TO:修改專案屬性和組態設定

HOW TO:限定宣告的項目名稱

HOW TO:區分兩個相同名稱的元素

概念

Visual Basic 中的變數

參考

Imports 陳述式 (.NET 命名空間和型別)

New (Visual Basic)

Public (Visual Basic)

其他資源

已宣告之項目的參考