Share via


Visual Basic Concepts

Speeding Object References

You can make your Visual Basic applications run faster by optimizing the way Visual Basic resolves object references. The speed with which Visual Basic handles object references can be affected by:

  • Whether or not the ActiveX component has been implemented as an in-process server or an out-of-process server.

  • Whether an object reference is early-bound or late-bound.

In general, if a component has been implemented as part of an executable file (.exe file), it is an out-of-process server and runs in its own process. If it has been implemented as a dynamic-link library, it is an in-process server and runs in the same process as the client application.

Applications that use in-process servers usually run faster than those that use out-of-process servers because the application doesn't have to cross process boundaries to use an object's properties, methods, and events. For more information about in-process and out-of-process servers, see "In-Process and Out-of-Process Servers."

Object references are early-bound if they use object variables declared as variables of a specific class. Object references are late-bound if they use object variables declared as variables of the generic Object class. Object references that use early-bound variables usually run faster than those that use late-bound variables.

For example, you could assign a reference to an Excel object to either of the following variables:

Dim xlApp1 As Excel.Application
Set xlApp1 = New Excel.Application

Dim xlApp2 As Object
Set xlApp2 = CreateObject("Excel.Application")

Code that uses variable xlApp1 is early-bound and will execute faster than code that uses variable xlApp2, which is late-bound.

Late Binding

When you declare a variable As Object, Visual Basic cannot determine at compile time what sort of object reference the variable will contain. In this situation, Visual Basic must use late binding— that is, Visual Basic must determine at run time whether or not that object will actually have the properties and methods you used in your code.

For example, Visual Basic will compile the following code without generating errors, even though it refers to a method that doesn't exist, because it uses a late-bound object variable. It doesn't check for the existence of the method until run time, so it will produce a run-time error:

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.TheImpossibleMethod   ' Method doesn't exist.

This code runs slower than code that uses an early-bound object variable because Visual Basic must include code in the compiled executable that will determine at run time whether or not the Microsoft Excel Application object has a TheImpossibleMethod method.

Although late binding is the slowest way to invoke the properties and methods of an object, there are times when it is necessary. For example, you may write a function that uses an object variable to act on any of several different classes of objects. Because you don't know in advance what class of object will be assigned to the variable, declare it as a late-bound variable using As Object.

Early Binding

If Visual Basic can detect at compile time what object a property or method belongs to, it can resolve the reference to the object at compile time. The compiled executable contains only the code to invoke the object's properties, methods, and events. This is called early binding.

When you declare an object variable using the class that defines the object, the variable can only contain a reference to an object of that class. Visual Basic can use early binding for any code that uses the variable.

Early binding dramatically reduces the time required to set or retrieve a property value, because the call overhead can be a significant part of the total time. For method calls, the improvement depends on the amount of work the method does. Short methods, where the call overhead is comparable to the time required to complete the task, will benefit the most.