BUG: You receive a "System.InvalidCastException" error message when you upgrade a Visual Basic 6.0 application that holds a user control to Visual Basic .NET or Visual Basic 2005 (834059)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)

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.

MORE INFORMATION

Steps to reproduce the behavior

Create a Visual Basic 6.0 application

  1. Start Visual Basic 6.0.
  2. Create a new Standard EXE project.

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

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

    The UserControl1 control is added to the toolbox.
  8. Add the UserControl1 control to the Form1 form.
  9. Click UserControl1, and then press CTRL + C.
  10. 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.
  11. Add a CommandButton control to the Form1 form.
  12. Add the following code in the Click event of the CommandButton control:
    Call UserControl11_MyCustomEvent(0, 5)
  13. 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 "Hello"
        End If
    End Sub
  14. Save the project.

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.
  2. On the Start page, click Open Project. Locate the folder that contains the Visual Basic 6.0 project file that you created.
  3. 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.
  4. Create a new project to upgrade your existing project to.
  5. Copy the form files and the class files to the new project.
  6. 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 "Symptoms" section of this article.

REFERENCES


For more information, visit the following Microsoft Web site:
For additional information about how to upgrade the Microsoft Visual Basic 6.0 project to Microsoft Visual Basic .NET, click the following article number to view the article in the Microsoft Knowledge Base:

317885 HOW TO: Use the Visual Basic Upgrade Wizard


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbBug kberrmsg kbEvent kbCodeGen kbExceptHandling KB834059 kbAudDeveloper