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

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

On This Page

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("abc")
				

STATUS

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 Variant
    
    Private Sub Class_Initialize()
        Var1 = 120
    End 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 Sub
    End 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"
    						
    with:
    Obj1.let_Var1("abc")
    						
    Rebuild the project and run. You do not receive an error message.
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.

REFERENCES

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

Properties

Article ID: 316138 - Last Review: February 12, 2007 - Revision: 5.1
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005 Express Edition
Keywords: 
kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbbug kbpending KB316138

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