Dim b As Integer = Integer.Parse("1") 导致 "System.FormatException: 'Input string was not in a correct format.'"

Dim b As Integer = Integer.Parse("1") resulting in "System.FormatException: 'Input string was not in a correct format.'"

我确定我一定是在做一些愚蠢的事情,但是有什么想法为什么这不起作用吗?

        Dim b As Integer = -1
        b = Integer.Parse("‭1")

如果有人想知道我为什么要这样做......这是调试的一部分,最初我想从表单上的文本框中获取数字,但是这不是用 tryParse 转换并抛出Parse/Convert 异常,因此我精简了代码以对其进行调试....

我得到的异常是:System.FormatException:'Input string was not in a correct format.'

更多详情:

System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool)
System.Number.ParseInt32(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
USBreceiver.Form1.Btn_confirm_details_Click(Object, System.EventArgs) in Form1.vb
System.Windows.Forms.Control.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message)
System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message)
System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message)
... [Call Stack Truncated]

我已经尝试了各种替代转换函数(tryparse、convert.toint32 等),但我得到了相同的结果...例如。如果 tryparsse 或只是抛出异常,则结果 =0 并捕获异常。无论哪种方式,转换都失败了。

我不禁想到区域设置/全球化发生了一些奇怪的事情(我在英国使用 VS2019 和 .net 4.7.2,它是一个 VB.net Windows 表单应用程序.异常的'source'是mscorlib StringToNumber..

      Source  "mscorlib"  String
      StackTrace  "   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)" & vbCrLf & "   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)" & vbCrLf & "   at USBreceiver.Form1.Btn_confirm_details_Click(Object sender, EventArgs e) in C:\Users\Z830\source\repos\USBreceiver\USBreceiver\Form1.vb:line 505" & vbCrLf & "   at System.Windows.Forms.Control.OnClick(EventArgs e)" & vbCrLf & "   at System.Windows.Forms.Button.OnClick(EventArgs e)" & vbCrLf & "   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)" & vbCrLf & "   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)" & vbCrLf & "   at System.Windows.Forms.Control.WndProc(Message& m)" & vbCrLf & "   at System.Windows.Forms.ButtonBase.WndProc(Message& m)" & vbCrLf & "   at System.Windows.Forms.Button.WndProc(Message& m)" & vbCrLf & "   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)" & vbCrLf & "   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)" & vbCrLf & "   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)" & vbCrLf & "   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)" & vbCrLf & "   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)" & vbCrLf & "   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()" & vbCrLf & "   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()" & vbCrLf & "   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)" & vbCrLf & "   at USBreceiver.My.MyApplication.Main(String[] Args) in :line 81"   String
      TargetSite  {Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

最奇怪的是,我确定它之前一直有效,然后我更改了数字(一个很大的数字,但仍然小于 max int32 = 2*10^9)并且它永远坏了,现在赢了连“1”都用不了……这真的很奇怪……

另一个重要信息是我认为加载 VS2019(或可能是其他软件)正在将我的键盘布局更改为美式...所以我从 windows 中卸载了美式键盘布局 - 但它没有修好了任何东西。

有什么想法吗? 谢谢,

@JohnL 你是我的救星!!!

我在这上面浪费了一整天的时间,真的很感激...

我仍然不完全确定该选项位于何处,但在 VS2019 顶部的搜索栏中输入 'unicode',然后出现了复选框 -> 现在可以使用了! ''' 当数据无法保存为代码页时将文档保存为 Unicode '''

谢谢!!!