BUG: Error message when you try to pass a Collection object from Visual Basic 6.0 components to Visual Basic 2005 or to Visual Basic .NET: "System.InvalidCastException"

Article translations Article translations
Article ID: 323737 - View products that this article applies to.
Expand all | Collapse all

On This Page

Symptoms

When you try to pass a Collection object from Microsoft Visual Basic 6.0 components to Microsoft Visual Basic 2005 or to Microsoft Visual Basic .NET, you may receive an error message. In Microsoft Visual Studio 2005, you receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in ApplicationName.exe
Additional information: Unable to cast object of type 'Microsoft.VisualBasic.Collection' to type 'VBA.Collection'.
In Microsoft Visual Studio .NET, you receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in ApplicationName.exe
Additional information: Specified cast is not valid.
If you examine the type of the collection object that Visual Basic 2005 or Visual Basic .NET expects, you find that Visual Basic 2005 or Visual Basic .NET expects the VBA.Collection type instead of the Microsoft.VisualBasic.Collection type. If you change your code to pass a collection object of the VBA.Collection type, you receive the following error message on the line of code where you try to create a new instance of the VBA.Collection class:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ApplicationName.exe
Additional information: COM object with CLSID {A4C4671C-499F-101B-BB78-00AA00383CBB} is either not valid or not registered.
This problem also occurs in other Microsoft .NET Framework-supported languages such as Microsoft Visual C# 2005 and earlier versions of .NET Framework-supported Microsoft Visual C#.

Cause

The InvalidCastException error occurs because the Microsoft.VisualBasic.Collection type is incompatible with the VBA.Collection type. The COMException error occurs because only a Visual Basic 6.0 application can create an instance of the VBA.Collection class. You cannot create an instance of the VBA.Collection class outside a Visual Basic 6.0 application.

Workaround

To work around this problem, create a VBA.Collection object in a Visual Basic 6.0 application, and then return the object to the Visual Basic 2005 or Visual Basic .NET application. To do this, you can create a new Visual Basic 6.0 DLL or add a new method in the existing DLL.

Note The index of the Collection object in Visual Basic 2005 or in Visual Basic .NET is base 1. However, the index of Visual Basic 6.0 is base 0. Therefore, you may have to modify the Visual Basic 6.0 DLL to use base 1 as the index for your collection instead of base 0.

Create a Visual Basic 6.0 DLL that returns the collection

  1. Create a Visual Basic 6.0 Microsoft ActiveX DLL project. By default, the Class1 class is created.
  2. Rename the project CollectionFactory, and then rename the class clsVBACollection.
  3. Add the following code to the clsVBACollection class.
    ' This function creates a new object of the VBA collection.
    Public Function CreateVBACollection() As Collection
    
       ' Define a variable of type Collection.
       Dim col As Collection
    
       ' Create a Collection object.
       Set col = New Collection
    
       ' Return the Collection object.
       Set CreateVBACollection = col
    End Function
  4. On the File menu, click Make CollectionFactory.dll.
  5. Start Visual Studio 2005 or Visual Studio .NET. Create a Console Application project by using Visual Basic 2005 or Visual Basic .NET. By default, the Module1.vb file is created.
  6. In Solution Explorer, right-click References, and then click Add Reference.

    Note In Visual Studio 2005, click the project name, and then click Add Reference.
  7. In the Add Reference dialog box, click the COM tab.
  8. Click Browse, and then locate CollectionFactory.dll. Click OK.

    Note In Visual Studio 2005, click CollectionFactory.dll, and then click OK.
  9. Replace the Sub Main method with the following code.
       Sub Main()
          ' Create a new instance of the clsVBSCollection class.
          Dim objVBACollection As New CollectionFactory.clsVBACollectionClass()
    
          ' The following variable stores the collection object that the CollectionFactory DLL returns.
          Dim col As VBA.CollectionClass
    
          ' Get the VBA.Collection object.
          col = objVBACollection.CreateVBACollection()
    
          ' Use the collection as you typically would do so.
          col.Add("Microsoft")
       End Sub

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 Visual Basic 6.0 Microsoft ActiveX DLL project. By default, the Class1 class is created.
  2. Add the following code to the Class1 class.
    Public Function GetCollection(col As Collection) As Variant
       GetCollection = col(1)
    End Function
  3. On the Project menu, click Properties. Rename the project TestCollection.
  4. On the File menu, click Make TestCollection.dll.
  5. In Visual Studio 2005 or in Visual Studio .NET, create a Console Application project by using Visual Basic 2005 or Visual Basic .NET. By default, the Module1.vb file is created.
  6. In Solution Explorer, right-click References, and then click Add Reference.

    Note In Visual Studio 2005, click the project name, and then click Add Reference.
  7. In the Add Reference dialog box, click the COM tab.
  8. Click Browse, and then locate TestCollection.dll. Click OK.

    Note In Visual Studio 2005, click TestCollection.dll, and then click OK.
  9. Replace the Sub Main method with the following code.
       Sub Main()
          ' Create a Microsoft.VisualBasic.Collection object.
          Dim col As New Collection()
          Dim objTestCol As New TestCollection.Class1Class()
          Dim objRetVal As Object
    
          ' Add an item to the collection.
          col.Add("Hello World")
    
          ' Pass the collection as a parameter.
          objRetVal = objTestCol.GetCollection(col)
       End Sub
    
  10. On the Debug menu, click Start to run the application. You receive the first error message that is mentioned in the "Symptoms" section.
  11. Click Continue.
  12. Locate the following line of code.
       Dim col As New Collection()
  13. Replace the line of code that you located in step 12 with the following line of code.
       Dim col As New VBA.CollectionClass()
  14. On the Debug menu, click Start to run the application. You receive the second error message that is mentioned in the "Symptoms" section.

References

For more information, click the following article number to view the article in the Microsoft Knowledge Base:
316163 Error messages when you attempt to build a Class Library project in Visual Basic .NET or in Visual Basic 2005

Properties

Article ID: 323737 - Last Review: June 19, 2014 - Revision: 4.0
Applies to
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbvs2002sp1sweep kberrmsg kbmsg kbdll kbcollections kbcollectionclass kbinterop kbautomation kbappcompatibility kbbug KB323737

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com