Sideway from Sideway
Draft for Information Only


VB.NET Object Characteristics
  Early and Late Binding
 Advantages of Early Binding
 See also
  Determining Object Type
 TypeName and TypeOf…Is
   To run the example
 See also
  Calling a Property or Method Using a String Name
 CallByName Function
 See also
  Working with Dynamic Objects
 See also

VB.NET Object Characteristics

Early and Late Binding

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. 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", 

Because FileStream is a specific object type, the instance assigned to FS is early bound.

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


Late binding can only be used to access type members that are declared as Public. Accessing members declared as Friend or Protected Friend results in a run-time error.

See also

Determining Object Type

Generic object variables (that is, variables you declare as Object) can hold objects from any class. When using variables of type Object, you may need to take different actions based on the class of the object; for example, some objects might not support a particular property or method. Visual Basic provides two means of determining which type of object is stored in an object variable: the TypeName function and the TypeOf...Is operator.

TypeName and TypeOf…Is

The TypeName function returns a string and is the best choice when you need to store or display the class name of an object, as shown in the following code fragment:

Dim Ctrl As Control = New TextBox

The TypeOf...Is operator is the best choice for testing an object's type, because it is much faster than an equivalent string comparison using TypeName. The following code fragment uses TypeOf...Is within an If...Then...Else statement:

If TypeOf Ctrl Is Button Then
    MsgBox("The control is a button.")
End If

A word of caution is due here. The TypeOf...Is operator returns True if an object is of a specific type, or is derived from a specific type. Almost everything you do with Visual Basic involves objects, which include some elements not normally thought of as objects, such as strings and integers. These objects are derived from and inherit methods from Object. When passed an Integer and evaluated with Object, the TypeOf...Is operator returns True. The following example reports that the parameter InParam is both an Object and an Integer:

Sub CheckType(ByVal InParam As Object)
    ' Both If statements evaluate to True when an
    ' Integer is passed to this procedure.
    If TypeOf InParam Is Object Then
        MsgBox("InParam is an Object")
    End If
    If TypeOf InParam Is Integer Then
        MsgBox("InParam is an Integer")
    End If
End Sub

The following example uses both TypeOf...Is and TypeName to determine the type of object passed to it in the Ctrl argument. The TestObject procedure calls ShowType with three different kinds of controls.

To run the example

  1. Create a new Windows Application project and add a Button control, a CheckBox control, and a RadioButton control to the form.

  2. From the button on your form, call the TestObject procedure.

  3. Add the following code to your form:

  1. Sub ShowType(ByVal Ctrl As Object)
        'Use the TypeName function to display the class name as text.
        'Use the TypeOf function to determine the object's type.
        If TypeOf Ctrl Is Button Then
            MsgBox("The control is a button.")
        ElseIf TypeOf Ctrl Is CheckBox Then
            MsgBox("The control is a check box.")
            MsgBox("The object is some other type of control.")
        End If
    End Sub
    Protected Sub TestObject()
        'Test the ShowType procedure with three kinds of objects.
    End Sub

See also

Calling a Property or Method Using a String Name

In most cases, you can discover the properties and methods of an object at design time, and write code to handle them. However, in some cases you may not know about an object's properties and methods in advance, or you may just want the flexibility of enabling an end user to specify properties or execute methods at run time.

CallByName Function

Consider, for example, a client application that evaluates expressions entered by the user by passing an operator to a COM component. Suppose you are constantly adding new functions to the component that require new operators. When you use standard object access techniques, you must recompile and redistribute the client application before it could use the new operators. To avoid this, you can use the CallByName function to pass the new operators as strings, without changing the application.

The CallByName function lets you use a string to specify a property or method at run time. The signature for the CallByName function looks like this:

Result = CallByName(Object, ProcedureName, CallType, Arguments())

The first argument, Object, takes the name of the object you want to act upon. The ProcedureName argument takes a string that contains the name of the method or property procedure to be invoked. The CallType argument takes a constant that represents the type of procedure to invoke: a method (Microsoft.VisualBasic.CallType.Method), a property read (Microsoft.VisualBasic.CallType.Get), or a property set (Microsoft.VisualBasic.CallType.Set). The Arguments argument, which is optional, takes an array of type Object that contains any arguments to the procedure.

You can use CallByName with classes in your current solution, but it is most often used to access COM objects or objects from .NET Framework assemblies.

Suppose you add a reference to an assembly that contains a class named MathClass, which has a new function named SquareRoot, as shown in the following code:

Class MathClass
    Function SquareRoot(ByVal X As Double) As Double
        Return Math.Sqrt(X)
    End Function
    Function InverseSine(ByVal X As Double) As Double
        Return Math.Atan(X / Math.Sqrt(-X * X + 1))
    End Function
    Function Acos(ByVal X As Double) As Double
        Return Math.Atan(-X / Math.Sqrt(-X * X + 1)) + 2 * Math.Atan(1)
    End Function
End Class

Your application could use text box controls to control which method will be called and its arguments. For example, if TextBox1 contains the expression to be evaluated, and TextBox2 is used to enter the name of the function, you can use the following code to invoke the SquareRoot function on the expression in TextBox1:

Private Sub CallMath()
    Dim Math As New MathClass
    Me.TextBox1.Text = CStr(CallByName(Math, Me.TextBox2.Text, 
       Microsoft.VisualBasic.CallType.Method, TextBox1.Text))
End Sub

If you enter "64" in TextBox1, "SquareRoot" in TextBox2, and then call the CallMath procedure, the square root of the number in TextBox1 is evaluated. The code in the example invokes the SquareRoot function (which takes a string that contains the expression to be evaluated as a required argument) and returns "8" in TextBox1 (the square root of 64). Of course, if the user enters an invalid string in TextBox2, if the string contains the name of a property instead of a method, or if the method had an additional required argument, a run-time error occurs. You have to add robust error-handling code when you use CallByName to anticipate these or any other errors.


While the CallByName function may be useful in some cases, you must weigh its usefulness against the performance implications — using CallByName to invoke a procedure is slightly slower than a late-bound call. If you are invoking a function that is called repeatedly, such as inside a loop, CallByName can have a severe effect on performance.

See also

Working with Dynamic Objects

Dynamic objects provide another way, other than the Object type, to late bind to an object at run time. A dynamic object exposes members such as properties and methods at run time by using dynamic interfaces that are defined in the System.Dynamic namespace. You can use the classes in the System.Dynamic namespace to create objects that work with data structures that do not match a static type or format. You can also use the dynamic objects that are defined in dynamic languages such as IronPython and IronRuby. For examples that show how to create dynamic objects or use a dynamic object defined in a dynamic language, see Walkthrough: Creating and Using Dynamic Objects, DynamicObject, or ExpandoObject.

Visual Basic binds to objects from the dynamic language runtime and dynamic languages such as IronPython and IronRuby by using the IDynamicMetaObjectProvider interface. Examples of classes that implement the IDynamicMetaObjectProvider interface are the DynamicObject and ExpandoObject classes.

If a late-bound call is made to an object that implements the IDynamicMetaObjectProvider interface, Visual Basic binds to the dynamic object by using that interface. If a late-bound call is made to an object that does not implement the IDynamicMetaObjectProvider interface, or if the call to the IDynamicMetaObjectProvider interface fails, Visual Basic binds to the object by using the late-binding capabilities of the Visual Basic runtime.

See also




ID: 201000001 Last Updated: 1/10/2020 Revision: 0 Ref:



  1. Active Server Pages,  ,
  2. ASP Overview,  ,
  3. ASP Best Practices,  ,
  4. ASP Built-in Objects,  ,
  5. Response Object,  ,
  6. Request Object,  ,
  7. Server Object (IIS),  ,
  8. Application Object (IIS),  ,
  9. Session Object (IIS),  ,
  10. ASPError Object,  ,
  11. ObjectContext Object (IIS),  ,
  12. Debugging Global.asa Files,  ,
  13. How to: Debug Global.asa files,  ,
  14. Calling COM Components from ASP Pages,  ,
  15. IIS ASP Scripting Reference,  ,
  16. ASP Keywords,  ,
  17. Creating Simple ASP Pages,  ,
  18. Including Files in ASP Applications,  ,
  19. ASP Overview,  ,
  20. FileSystemObject Object,  ,
  21.,  , ADO Object Model
  22. ADO Fundamentals,  ,

Home 5






Hobbies 8


Chinese 1097

English 337

Reference 67


Hardware 151


Application 198

Digitization 25

Latex 9

Manim 121

Numeric 19


Web 283

Unicode 494


CSS 58


OS 389

DeskTop 7

Python 19



Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2


Electric 27

Biology 1

Geography 1

Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019