Export (0) Print
Expand All
Expand Minimize
29 out of 167 rated this helpful - Rate this topic

Troubleshooting Exceptions: System.InvalidCastException

An InvalidCastException exception is thrown when a failure occurs during an explicit reference conversion. Reference conversions are conversions from one reference type to another. While they may change the type of the reference, they never change the type or value of the conversion's target. Casting objects from one type to another is a frequent cause for this exception.

Check source types against destination types to make sure the conversion is valid.

For information on conversions supported by the system, see Convert.

For an explicit reference conversion to be successful, the source value must be Null (Nothing in Visual Basic), or the object type referenced by the source argument must be convertible to the destination type by an implicit reference conversion.

When a Visual Basic 6.0 application with a call to a custom event in a user control is upgraded to a newer version of Visual Basic and run, this exception may occur with the additional information, "Specified cast is not valid." To address this error, find the following line of code in Form1:

Call UserControl11_MyCustomEvent(UserControl11, New UserControl1.MyCustomEventEventArgs(5))

And replace it with:

Call UserControl11_MyCustomEvent(UserControl11(0), New UserControl1.MyCustomEventEventArgs(5))

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.