Microsoft KB Archive/316138

= BUG: You receive a &quot;Type mismatch&quot; error message when you assign a value type variable to a property through COM InterOp in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 316138

Article Last Modified on 2/12/2007

-

APPLIES TO


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

-



This article was previously published under Q316138



SYMPTOMS
When you assign a value to a property of a Component Object Model (COM) object in .NET, you may receive the following error message when you run your application:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in InterOpDemo.exe

Additional information: Type mismatch



CAUSE
The problem occurs if all of the following conditions are true:
 * 1) You are using the COM object in early bound mode.
 * 2) The property in the COM object has both Set and Let methods.
 * 3) You are trying to pass a value type variable to that property.

In early bound mode, Microsoft Visual Basic .NET and Visual Basic 2005 always call the Set method of the property if it is available. If you want to call the Let method, you must explicitly specify it.



RESOLUTION
Explicitly tell the compiler to use the Let method. For example, you can use code similar to the following to tell the compiler to use Let instead of Set: Dim Obj1 As New InterOpVB6.Class1 Obj1.let_Var1(&quot;abc&quot;)



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
 In Visual Basic 6.0, create a new ActiveX DLL project, and name it InterOpVB6. Class1 is created by default.  Paste the following code in the code window of Class1: Public Var1 As Variant

Private Sub Class_Initialize Var1 = 120 End Sub  Build the project. Open Visual Studio .NET. Click New Project, click Visual Basic Projects, select Console Application, and then name the application InterOpDemo. Module1 is created by default.

Note In Visual Studio 2005, click Visual Basic under Project Types. Add a reference to the COM component (InterOpVB6) that you just created from the project's Add Reference dialog box.</li>  Paste the following code in the code window of Module1: Module Module1

Sub Main Dim Obj1 As New InterOpVB6.Class1 Obj1.Var1 = &quot;abc&quot; End Sub End Module </li> Build and run the project. You receive the following exception:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in InterOpDemo.exe

Additional information: Type mismatch

</li>  Replace the following line of code Obj1.Var1 = &quot;abc&quot; with: Obj1.let_Var1(&quot;abc&quot;) Rebuild the project and run. You do not receive an error message. </li></ol>

NOTES:

If a variant type variable is declared as Public in a class module, Visual Basic implicitly creates Let, Get, and Set accessor methods for this property.

The scenario described in this article also applies to ActiveX controls that are used through COM Interop. The resolution to the problem is the same, but the error message that you receive may be different. When you work with an ActiveX control, the error message returned is a NullReferenceException. <div class="references_section">