0
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

(I shortened the code quite a bit: I didn't want to have 12 different cases that are unnecessary.)

All I get is a runtime error. The error reads:

System.InvalidCastException was unhandled Message=Conversion from string "Canoe (Not In Service) 1MP" to type 'Long' is not valid.
Source=Microsoft.VisualBasic StackTrace: at Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) at Money_Money.ChapterTwo.MakeArmyShipButton_Click(Object sender, EventArgs e) in C:\Documents and Settings\Matthew's\My Documents\Visual Studio 2010\Projects\Money Money\Money Money\ChapterTwo.vb:line 166 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at Money_Money.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.FormatException Message=Input string was not in a correct format. Source=Microsoft.VisualBasic StackTrace: at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) InnerException:

Why is this happening???

4

1 回答 1

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
于 2013-09-18T23:11:55.810 回答