Microsoft KB Archive/317887

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

Article ID: 317887

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft .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 &quot;Type Mismatch&quot; error when you use the set_  property method of the Component Object Model (COM) VARIANT, where  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.



Steps to Reproduce the Problem
  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  Compile ComVarTest.dll, and then register it. Create a new Visual Basic .NET Console application.  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  Press F5 to run the Visual Basic .NET application. The application fails with a System.Runtime.InteropServices.COMException exception.</li></ol>

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

Keywords: kbbug kbcominterop kbpending KB317887

-

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

© Microsoft Corporation. All rights reserved.