为什么我收到“InvaladCastExeption未处理”错误?

时间:2013-09-18 23:04:37

标签: .net vb.net forms runtime-error vb.net-2010

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
    Dim CaseCompareItem As String = ListBox.SelectedValue
    Select Case CaseCompareItem

        Case "MyText" Or "NotText"
            MsgBox("YAY!!! IT WORKED!!!!!")
    End Select
End Sub

(我把代码缩短了一点:我不想有12个不必要的案例。)

我得到的只是运行时错误。错误如下:

  

System.InvalidCastException未处理Message = Conversion from   字符串“Canoe(Not In Service)1MP”键入'Long'无效   Source = Microsoft.VisualBasic StackTrace:          在Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String   值)          at Money_Money.ChapterTwo.MakeArmyShipButton_Click(Object sender,EventArgs e)在C:\ Documents and Settings \ Matthew's \ My   Documents \ Visual Studio 2010 \ Projects \ Money Money \ Money   Money \ ChapterTwo.vb:第166行          在System.Windows.Forms.Control.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息& m)          在System.Windows.Forms.ButtonBase.WndProc(消息& m)          在System.Windows.Forms.Button.WndProc(消息& m)          在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr)   dwComponentID,Int32原因,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32)   原因,ApplicationContext上下文)          在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()          在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()          在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String []   命令行)          at Money_Money.My.MyApplication.Main(String [] Args)in 17d14f5c-a337-4978-8281-53493378c1071.vb:第81行          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean   ignoreSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          at System.Threading.ThreadHelper.ThreadStart()InnerException:System.FormatException          消息=输入字符串格式不正确。          来源= Microsoft.VisualBasic程序          堆栈跟踪:               在Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String   Value,NumberFormatInfo NumberFormat)               在Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String   值)          的InnerException:

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

Or语句中使用Case意味着一个整数类型,它将导致转换为Long抛出异常。将其替换为','。

这些错误在那些拒绝改变Option Strict On的人身上很常见,这些错误会在他们写完时标记出来。

您可能会使用SelectedItem属性而不是SelectedValue属性找到更多成功,并且您还应该在访问它的值之前检查对象是否为空:

    If Not ListBox1.SelectedItem Is Nothing Then
        Dim CaseCompareItem As String = ListBox1.SelectedItem.ToString
        Select Case CaseCompareItem

            Case "MyText", "NotText"
                MsgBox("YAY!!! IT WORKED!!!!!")
        End Select
    End If