Microsoft KB Archive/323737

= 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: &quot;System.InvalidCastException&quot; =

Article ID: 323737

Article Last Modified on 2/8/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q323737



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 .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 .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 .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
 Create a Visual Basic 6.0 Microsoft ActiveX DLL project. By default, the Class1 class is created. Rename the project CollectionFactory, and then rename the class clsVBACollection.  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  On the File menu, click Make CollectionFactory.dll. 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. 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.</li> In the Add Reference dialog box, click the COM tab.</li> Click Browse, and then locate CollectionFactory.dll. Click OK.

Note In Visual Studio 2005, click CollectionFactory.dll, and then click OK.</li>  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(&quot;Microsoft&quot;) End Sub </li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.

<div class="moreinformation_section">

Steps to reproduce the problem
<ol> Create a Visual Basic 6.0 Microsoft ActiveX DLL project. By default, the Class1 class is created.</li>  Add the following code to the Class1 class. Public Function GetCollection(col As Collection) As Variant GetCollection = col(1) End Function </li> On the Project menu, click Properties. Rename the project TestCollection.</li> On the File menu, click Make TestCollection.dll.</li> 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.</li> 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.</li> In the Add Reference dialog box, click the COM tab.</li> Click Browse, and then locate TestCollection.dll. Click OK.

Note In Visual Studio 2005, click TestCollection.dll, and then click OK.</li>  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(&quot;Hello World&quot;)

' Pass the collection as a parameter. objRetVal = objTestCol.GetCollection(col) End Sub </li> On the Debug menu, click Start to run the application. You receive the first error message that is mentioned in the &quot;Symptoms&quot; section.</li> Click Continue.</li>  Locate the following line of code. Dim col As New Collection </li>  Replace the line of code that you located in step 12 with the following line of code. Dim col As New VBA.CollectionClass </li> <li>On the Debug menu, click Start to run the application. You receive the second error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">