Microsoft KB Archive/316936

= PRB: Error When You Access an Array Field of a .NET Structure from COM =

Article ID: 316936

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition

-



This article was previously published under Q316936



SYMPTOMS
When you access an Array field of a structure that is defined in .NET from inside a COM DLL, you may receive the following error message:

&quot;An unhandled exception of type 'System.ArgumentException' occurred in &quot;

Additional information: Wrong number of arguments or invalid property assignment.



CAUSE
Because of the late-bound method that is used in this case, the Visual Basic 6.0 runtime cannot get the type information for the Array field that you are attempting to access.



RESOLUTION
Before you invoke the Array field of the structure, assign the array to another Variant variable. The following sample resembles the modified code: temp = Arg1.Ary MsgBox temp(arg2, arg3) NOTE: If you use Option Explicit, you must declare the variable temp. Refer to the &quot;More Information&quot; section of this article for the complete code list.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create a new Visual Basic 6.0 ActiveX DLL. Class1 is added by default.  Paste the following sample code in Class1: Public Sub Goo(ByVal Arg1 As Variant, ByVal arg2 As Variant, ByVal arg3 As Variant) MsgBox TypeName(Arg1.Ary)       'This returns Decimal as expected. MsgBox Arg1.Ary(arg2, arg3) 'This array access returns an unexpected exception. End Sub  Compile the ActiveX DLL to register the Project1.dll. Create a new Visual Basic .NET console application. Add a reference to Project1.dll.</li>  Replace the existing code in Module1.vb with the following sample code: Public Module Module1 Public Structure S1       Public x As Integer Dim Ary As Decimal End Structure

Dim s As S1

Sub Main Dim Obj As Project1.Class1 = New Project1.Class1 ReDim s.Ary(100, 0) s.Ary(0, 0) = 10 Obj.Goo(s, 0, 0)   'This call causes the exception. End Sub End Module </li> Press F5 to compile and then run the program.</li></ol>

Keywords: kbprb KB316936

-

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

© Microsoft Corporation. All rights reserved.