BUG: .NET Method That Takes a ByRef Value Type Parameter Fails When You Pass a Variant Type (317887)



The information in this article applies to:

  • Microsoft Windows .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework Class Libraries 1.0

This article was previously published under Q317887

SYMPTOMS

When you pass an unmanaged VARIANT type, a Microsoft Visual Basic .NET method defined to take a ByRef value type formal parameter may fail with a System.Runtime.InteropServices.COMException (0x800A000D) exception.

CAUSE

When the Visual Basic .NET method that takes a ByRef value type formal parameter returns, the new property value is written by using the set_MyProp property method instead of the let_MyProp property method. The exception is thrown because of a "Type Mismatch" error when you use the set_MyProp property method of the Component Object Model (COM) VARIANT, where MyProp is the property in use.

RESOLUTION

To pass the property to this method, access it in a late-bound fashion.

STATUS

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

MORE INFORMATION

Steps to Reproduce the Problem

  1. Create a Visual Basic 6.0 ActiveX DLL file. Rename Project1 as ComVarTest and Class1 as TestCls. Paste the following code in TestCls:
    Public x As Variant
    					
  2. Compile ComVarTest.dll, and then register it.
  3. Create a new Visual Basic .NET Console application.
  4. Paste the following code into the module file, overwriting the existing code:
    Module Module1
    	Sub TestMethod(ByRef Arg As Short)
    		Arg += 20
    	End Sub
    
    	Sub Main()
    		Dim Obj As New ComVarTest.TestCls()
    		Obj.let_x(10)
    		TestMethod(Obj.x)  'Exception here
    		MsgBox(Obj.x)
    	End Sub
    End Module
    					
  5. Press F5 to run the Visual Basic .NET application. The application fails with a System.Runtime.InteropServices.COMException exception.
To work around this issue, use the following code in step 4:
Module Module1
	Sub TestMethod(ByRef Arg As Short)
		Arg += 20
	End Sub

	Sub Main()
		Dim Obj As New ComVarTest.TestCls()
		Obj.let_x(10)
		TestMethod(CObj(Obj).x)  
		MsgBox(Obj.x)
	End Sub
End Module
				

Modification Type:MajorLast Reviewed:9/22/2003
Keywords:kbbug kbCOMInterop kbpending KB317887