Microsoft KB Archive/834059

= BUG: You receive a &quot;System.InvalidCastException&quot; error message when you upgrade a Visual Basic 6.0 application that holds a user control to Visual Basic .NET or Visual Basic 2005 =

Article ID: 834059

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition

-





SYMPTOMS
You upgrade a Microsoft Visual Basic 6.0 application that holds a user control to Microsoft Visual Basic .NET or Visual Basic 2005. The user control that the Visual Basic 6.0 application holds calls a custom event. When you run the Visual Basic .NET or Visual Basic 2005 application, you may receive the following error message:

An unhandled exception of type 'System.InvalidCastException' occurred in Project1.exe.

Additional information: Specified cast is not valid.



CAUSE
This bug occurs when the index parameter determines that the code that the Visual Basic Upgrade Wizard generates does not pass the correct instance of the control array.



WORKAROUND
To work around this bug, locate the following code in the form that is named Form1 of the Command1_Click event in a Visual Basic .NET application: Call UserControl11_MyCustomEvent(UserControl11, New UserControl1.MyCustomEventEventArgs(5)) Replace this code with the following: Call UserControl11_MyCustomEvent(UserControl11(0), New UserControl1.MyCustomEventEventArgs(5))



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Create a Visual Basic 6.0 application
 Start Visual Basic 6.0. Create a new Standard EXE project.

By default, the Form1.frm file is created. On the Project menu, click Add User Control.

By default, the UserControl1.ctl file is created. Right-click UserControl1, and then click View Code.  Add the following code to the UserControl1.ctl file: Public Event MyCustomEvent(MyParam As Integer) </li> On the File menu, click Save UserControl1.</li> On the Save File As dialog box, click Save.

The UserControl1 control is added to the toolbox.</li> Add the UserControl1 control to the Form1 form.</li> Click UserControl1, and then press CTRL + C.</li> Click Form1, and then press CTRL + V.

You receive the following message:

You already have a control named 'UserControl11'. Do you want to create a control array?

Click Yes.</li> Add a CommandButton control to the Form1 form.</li>  Add the following code in the Click event of the CommandButton control: Call UserControl11_MyCustomEvent(0, 5) </li>  Add the following code to the Form1.frm file: Private Sub UserControl11_MyCustomEvent(Index As Integer, MyParam As Integer) If Index = 0 Then MsgBox MyParam Else MsgBox &quot;Hello&quot; End If End Sub </li> Save the project.</li></ol>

Update the Visual Basic 6.0 application to Visual Basic .NET or Visual Basic 2005

 * 1) Click Start, point to Programs, and then click Microsoft Visual Studio .NET.

Note In Visual Studio 2005, click Microsoft Visual Studio 2005.
 * 1) On the Start page, click Open Project. Locate the folder that contains the Visual Basic 6.0 project file that you created.
 * 2) Click Project1.vdp, and then click Open.

Visual Studio .NET or Visual Studio 2005 detects that your project is an earlier Visual Basic project. Visual Studio .NET or Visual Studio 2005 starts the Visual Basic Upgrade Wizard.
 * 1) Create a new project to upgrade your existing project to.
 * 2) Copy the form files and the class files to the new project.
 * 3) Provide an upgrade report that lists the components of the project that upgraded successfully and that lists the components of the project that did not upgrade successfully.

Test the application

 * 1) Open the Visual Basic .NET or Visual Basic 2005 project.
 * 2) On the Debug menu, click Start to run the application.
 * 3) In the Form1 window, click the Command1 button.

You receive the error message that is mentioned in the &quot;Symptoms&quot; section of this article.

<div class="references_section">