You are currently offline, waiting for your internet to reconnect

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

This article has been archived. It is offered "as is" and will no longer be updated.
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
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.
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("abc")				
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.
More information

Steps to Reproduce Behavior

  1. In Visual Basic 6.0, create a new ActiveX DLL project, and name it InterOpVB6. Class1 is created by default.
  2. Paste the following code in the code window of Class1:
    Public Var1 As VariantPrivate Sub Class_Initialize()    Var1 = 120End Sub					
  3. Build the project.
  4. 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.
  5. Add a reference to the COM component (InterOpVB6) that you just created from the project's Add Reference dialog box.
  6. Paste the following code in the code window of Module1:
    Module Module1    Sub Main()        Dim Obj1 As New InterOpVB6.Class1()        Obj1.Var1 = "abc"    End SubEnd Module					
  7. 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
  8. Replace the following line of code
    Obj1.Var1 = "abc"						
    Rebuild the project and run. You do not receive an error message.

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.
For additional information about a similar scenario that uses an ActiveX control, click the following article number to view the article in the Microsoft Knowledge Base:
316180 FIX: System.NullReferenceException Error if You Assign a TreeView Node Tag Property

Article ID: 316138 - Last Review: 01/11/2015 04:31:56 - Revision: 6.0

Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Visual Basic .NET 2003 Standard Edition

  • kbnosurvey kbarchive kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbbug kbpending KB316138