BUG: Visual Basic 6.0 cannot use the .NET method with the ParamArray parameter in Visual Studio .NET (327084)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition

This article was previously published under Q327084

SYMPTOMS

Microsoft Visual Basic version 6.0 cannot use the .NET method with the ParamArray parameter in Microsoft Visual Studio .NET. A compile-time error is generated by Visual Basic version 6.0 when it tries to consume the .NET method that has the ByRef ParamArray parameter or the ByRef Structure parameter.

When the .NET method has the ByRef ParamArray parameter, you receive the following error message:
Compile error:

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic


When the .NET method has the ByRef Structure parameter, you receive the following error message:
Compile error:

User-defined type may not be passed ByVal

CAUSE

This problem occurs because Visual Basic version 6.0 does not let the ParamArray parameter and the Structure parameter be passed to the BYVAL value. This problem occurs with a .NET property because a .NET property does not let property parameters be defined by the BYREF value.

RESOLUTION

To work around this problem, you can define the ParamArray parameter and the Structure parameter as BYREF. For a property, you can add a method with the ByRef parameter that assigns the property to the private member.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the problem

  1. Create a new Visual Basic .NET class library project.

    By default, Class1.vb is added.
  2. Put the following code in Class1.vb. Overwrite the existing code.
    <ComClass()> Public Class Class1
        Dim _res(4) As Byte
        Public Property res() As Byte()
            Get
                Return _res
            End Get
            Set(ByVal Value As Byte())
                _res = Value
            End Set
        End Property
    
        Public Structure s1
            Public i As Integer
            dim j as integer
        End Structure
    
        Dim _mes As s1
        Public Property mes() As s1
            Get
                Return _mes
            End Get
            Set(ByVal Value As s1)
                _mes = Value
            End Set
        End Property
    
        Public Sub setArrayProp(ByRef Value As Byte())
            _res = Value
        End Sub
    
        Public Sub setStructProp(ByRef Value As s1)
            _mes = Value
        End Sub
    
    End Class
    						
  3. On the Project Properties menu, click Configuration Properties.

    This starts Solution Property Pages.

    Click the Build tab, and then click Register for COM Interop.
  4. Compile the project.

    The Classlibrary1.tlb is generated after the project is compiled.
  5. Start Visual Basic version 6.0.
  6. Create a standard .exe project.

    By default, Form1 is added.
  7. Add a button to Form1.

    Use the default name for the button. The default name is Command1.
  8. Put the following code in the code window of Form1.
    Private Sub Command1_Click()
        Dim cls As New Class1
        Dim arr() As Byte
        Dim s As s1
        
        arr = cls.res
        'cls.res = arr
        
        s = cls.mes
        'cls.mes = s
       
        Call cls.setArrayProp(arr)
        Call cls.setStructProp(s)
        
    End Sub
    						
  9. Add a reference to the Classlibrary1.tlb file that is generated in step 4.
  10. Press F5.

    The code runs correctly.
  11. Uncomment the following two lines, and then run the code again.
    • cls.res = arr
    • cls.mes = s
    You receive the error message that mentioned in the "Symptoms" section.

Modification Type:MinorLast Reviewed:9/13/2005
Keywords:kbvs2002sp1sweep kbtshoot kbbug kbCOMInterop kbpending KB327084 kbAudDeveloper