早期和晚期繫結 (Visual Basic)Early and Late Binding (Visual Basic)

binding當物件指派給物件變數時,Visual Basic 編譯器會執行呼叫的進程。The Visual Basic compiler performs a process called binding when an object is assigned to an object variable. 將物件指派給宣告為特定物件型別的變數時,該物件即為「早期繫結」**。An object is early bound when it is assigned to a variable declared to be of a specific object type. 早期繫結物件讓編譯器能夠配置記憶體,並在應用程式執行之前執行其他最佳化。Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes. 例如,下列程式碼片段會將變數宣告為 FileStream 類型:For example, the following code fragment declares a variable to be of type FileStream:

'  Create a variable to hold a new object.
Dim FS As System.IO.FileStream
' Assign a new object to the variable.
FS = New System.IO.FileStream("C:\tmp.txt",
    System.IO.FileMode.Open)

因為 FileStream 是特定的物件類型,所以指派給 FS 的執行個體就是早期繫結。Because FileStream is a specific object type, the instance assigned to FS is early bound.

反之,將物件指派給宣告為 Object 型別的變數時,該物件即為「晚期繫結」**。By contrast, an object is late bound when it is assigned to a variable declared to be of type Object. 此型別的物件可以保存對任何物件的參考,但缺少許多早期繫結物件的優點。Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects. 例如,下列程式碼片段會宣告一個物件變數來保存 CreateObject 函式所傳回的物件:For example, the following code fragment declares an object variable to hold an object returned by the CreateObject function:

' To use this example, you must have Microsoft Excel installed on your computer.
' Compile with Option Strict Off to allow late binding.
Sub TestLateBinding()
    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlSheet As Object
    xlApp = CreateObject("Excel.Application")
    ' Late bind an instance of an Excel workbook.
    xlBook = xlApp.Workbooks.Add
    ' Late bind an instance of an Excel worksheet.
    xlSheet = xlBook.Worksheets(1)
    xlSheet.Activate()
    ' Show the application.
    xlSheet.Application.Visible = True
    ' Place some text in the second row of the sheet.
    xlSheet.Cells(2, 2) = "This is column B row 2"
End Sub

早期繫結的優點Advantages of Early Binding

您應該盡可能使用早期繫結物件,因為它們可讓編譯器進行重要的最佳化,以產生更有效率的應用程式。You should use early-bound objects whenever possible, because they allow the compiler to make important optimizations that yield more efficient applications. 早期繫結物件的速度很明顯地比晚期繫結物件還快,並藉由確實描述正在使用哪種物件,讓您的程式碼更容易閱讀和維護。Early-bound objects are significantly faster than late-bound objects and make your code easier to read and maintain by stating exactly what kind of objects are being used. 早期繫結的另一個優點是,它會啟用像是自動程式碼自動完成和動態說明等有用的功能,因為 Visual Studio 整合式開發環境 (IDE) 可以精確地判斷您在編輯程式碼時所使用的物件類型。Another advantage to early binding is that it enables useful features such as automatic code completion and Dynamic Help because the Visual Studio integrated development environment (IDE) can determine exactly what type of object you are working with as you edit the code. 早期繫結可降低發生執行階段錯誤的次數和嚴重性,因為它讓編譯器能夠在編譯程式時報告錯誤。Early binding reduces the number and severity of run-time errors because it allows the compiler to report errors when a program is compiled.

注意

晚期繫結只能用來存取宣告為 Public 的型別成員。Late binding can only be used to access type members that are declared as Public. 存取宣告為 FriendProtected Friend 的成員會導致執行階段錯誤。Accessing members declared as Friend or Protected Friend results in a run-time error.

另請參閱See also