# 早期和晚期繫結 (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)


' 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.
' 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