VB is a piece of cake--------early binding and late binding

Source: Internet
Author: User

Early binding and late binding Visual Studio 2005Other versions

When an object is assigned to an object variable, the Visual Basic compiler executes a process called binding. If an object is assigned to a variable declared as a specific object type, the object is early bound. Early bound objects allow the compiler to allocate memory and perform other optimizations before the application executes. For example, the following code fragment declares a variable to be of type FileStream:

Vb
'  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)

Because FileStream is a specific type of object, an instance assigned to FS is an early binding.

Conversely, if an object is assigned to a variable declared as object type, the object is late-bound. Objects of this type can store references to any object, but there are many advantages to early-bound objects. For example, the following code snippet declares an object variable to store the object returned by the CreateObject function:

Vb
' To use this example, you must has 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
Benefits of early binding

Early bound objects should be used whenever possible, because they allow the compiler to make important optimizations to produce more efficient applications. Early-bound objects are much faster than late-bound objects and can make code easier to read and maintain by declaring the kind of objects they use. Another advantage of 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 using when you edit your code. Because early binding allows the compiler to report errors when compiling a program, it reduces the number and severity of run-time errors.

Attention

Late binding can only be used to access a type member that is declared public. Accessing a member that is declared as friend or Protected friend causes a run-time error.

VB is a piece of cake--------early binding and late binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.