Microsoft KB Archive/316478

= BUG: &quot;Object variable or With block variable not set&quot; error message when you access a public object variable =

Article ID: 316478

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q316478



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 &quot;Steps to reproduce the problem&quot; 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 &quot;Steps to reproduce the problem&quot; 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 &quot;Applies to&quot; section.



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..</li> Create a new Visual Basic .NET or Visual Basic 2005 console application.

By default, Module1.vb is created.</li> On the Project menu, click Add Reference.</li> Click the COM tab.</li> 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.</li> Click OK to close the Add Reference dialog box.</li>  Replace the default code in Module1.vb with the following code example: Module Module1 Sub Main Dim obj1 As Object Console.WriteLine(&quot;Instantiating Project.Class1.&quot;) obj1 = New Project1.Class1 Console.WriteLine(&quot;Object instantiated.&quot;) obj1.obj = Nothing  'This line causes the error. Console.WriteLine(&quot;obj member set to Nothing.&quot;) Console.WriteLine(&quot;Press ENTER to exit.&quot;) Console.Read End Sub End Module </li> Run the project.

You receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

Keywords: kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbbug kberrmsg kbpending KB316478

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.