This article was previously published under Q316478
This article has been archived. It is offered "as is" and will no longer be updated.
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
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.
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.Class1Set obj1 = New Project1.Class1
Do not use the following code example:
Dim obj1 As ObjectSet 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 ExplicitPrivate obj1 As ObjectPublic Property Get Obj() As Object Set Obj = obj1End PropertyPublic Property Let Obj(Object As Object) Set obj1 = ObjectEnd Property
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
Steps to reproduce the problem
Create an ActiveX DLL project in Visual Basic 6.0
Start Visual Basic 6.0.
Create a new ActiveX DLL project.
By default, Class1 is created.
Type the following code in the General Declarations section of the module:
Public obj As Object
Compile this project to Project1.dll.
Create the Visual Basic .NET or Visual Basic 2005 application
Start Microsoft Visual Studio .NET, Microsoft Visual Studio 2005, or Microsoft Visual Basic 2005 Express Edition..
Create a new Visual Basic .NET or Visual Basic 2005 console application.
By default, Module1.vb is created.
On the Project menu, click Add Reference.
Click the COM tab.
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.
Click OK to close the Add Reference dialog box.
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 SubEnd Module
Run the project.
You receive the error message that is mentioned in the "Symptoms" section.