Reputation: 2137
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???
Upvotes: 0
Views: 436
Reputation: 6948
Using Or
in a Case
statement implies an integral type which will cause the conversion to Long
to throw an exception. Replace it with a ','.
These kinds of mistakes are common with those that refuse to turn Option Strict On, which will flag them when they're written.
You will probably find more success using the SelectedItem
property, instead of the SelectedValue
property, and also you should check if the object is nothing before accessing it's value:
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
Upvotes: 2