BUG: "Object variable or With block variable not set" error message when you access a public object variable

Article translations Article translations
Article ID: 316478 - View products that this article applies to.
This article was previously published under Q316478
Expand all | Collapse all

On This Page

SYMPTOMS

When you set a public object variable of a Microsoft Component Object Model (COM) component in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005, you may receive the following error message:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll

Additional information: Object variable or With block variable not set

CAUSE

This behavior can occur if all the following conditions are true:
  • You define a public variable in the COM component.
  • The variable is of the Object type.
  • You reference this COM component by using late binding in Visual Basic .NET or in Visual Basic 2005.
When you access the public object by using late binding, Visual Basic .NET or Visual Basic 2005 does not correctly set the BindingFlags enumeration.

The BindingFlags enumeration is used to specify the flags that control binding and the way in which the search for members and types is conducted by reflection.

WORKAROUND

To work around this problem, use one of the following methods.

Use early binding in Visual Basic .NET or in Visual Basic 2005

Use early binding in the Visual Basic .NET or Visual Basic 2005 client application. This is the easiest solution because you do not have to rebuild the COM component. To use early binding in Visual Basic .NET or in Visual Basic 2005, define the object variable by using the type name from the interop assembly that is generated when you set the reference to the component. Do not define it by using the Object type.

When you use the Visual Basic .NET or Visual Basic 2005 code example that is in the "Steps to reproduce the problem" section, use the following code example to define the object variable.
Dim obj1 As Project1.Class1
Set obj1 = New Project1.Class1
Do not use the following code example:
Dim obj1 As Object
Set obj1 = New Project1.Class1

Define a public property procedure in the COM component

Modify the Microsoft Visual Basic 6.0 COM component to use a public property procedure to set and to return the object. Do this instead of using a global Object variable. This solution is more difficult to implement because you must modify the original Visual Basic 6.0 component. If you do this, you may have to break the binary compatibility of the component.

When you use the Visual Basic 6.0 code example that is in the "Steps to reproduce the problem" section, use the following code example to define a public property procedure within the Class1 class to set and to retrieve the object variable.
Option Explicit

Private obj1 As Object

Public Property Get Obj() As Object
    Set Obj = obj1
End Property

Public Property Let Obj(Object As Object)
    Set obj1 = Object
End Property

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the problem

Create an ActiveX DLL project in Visual Basic 6.0

  1. Start Visual Basic 6.0.
  2. Create a new ActiveX DLL project.

    By default, Class1 is created.
  3. Type the following code in the General Declarations section of the module:
    Public obj As Object		
  4. Compile this project to Project1.dll.

Create the Visual Basic .NET or Visual Basic 2005 application

  1. Start Microsoft Visual Studio .NET, Microsoft Visual Studio 2005, or Microsoft Visual Basic 2005 Express Edition..
  2. Create a new Visual Basic .NET or Visual Basic 2005 console application.

    By default, Module1.vb is created.
  3. On the Project menu, click Add Reference.
  4. Click the COM tab.
  5. Click Project1 in the list of available COM references, and then click Select. If you do not see Project1 listed, click Browse, and then locate Project1.dll on the hard disk of the computer.

    Note In Visual Studio 2005 or Visual Basic 2005 Express Edition, you do not have to click Select.
  6. Click OK to close the Add Reference dialog box.
  7. Replace the default code in Module1.vb with the following code example:
    Module Module1
         Sub Main()
            Dim obj1 As Object
            Console.WriteLine("Instantiating Project.Class1.")
            obj1 = New Project1.Class1
            Console.WriteLine("Object instantiated.")
            obj1.obj = Nothing   'This line causes the error.
            Console.WriteLine("obj member set to Nothing.")
            Console.WriteLine("Press ENTER to exit.")
            Console.Read()
        End Sub
    End Module
    					
  8. Run the project.

    You receive the error message that is mentioned in the "Symptoms" section.

Properties

Article ID: 316478 - Last Review: May 18, 2007 - Revision: 4.3
APPLIES TO
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbbug kberrmsg kbpending KB316478

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com