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



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0

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:
"An unhandled exception of type 'System.ArgumentException' occurred in ProjectName"

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 "More Information" section of this article for the complete code list.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Basic 6.0 ActiveX DLL. Class1 is added by default.
  2. 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
    					
  3. Compile the ActiveX DLL to register the Project1.dll.
  4. Create a new Visual Basic .NET console application.
  5. Add a reference to Project1.dll.
  6. 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
    					
  7. Press F5 to compile and then run the program.

Modification Type:MajorLast Reviewed:5/13/2003
Keywords:kbprb KB316936