【问题标题】:Dim b As Integer = Integer.Parse("1") resulting in "System.FormatException: 'Input string was not in a correct format.'"Dim b As Integer = Integer.Parse("1") 导致“System.FormatException:'输入字符串格式不正确。'”
【发布时间】:2020-05-23 08:38:36
【问题描述】:

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

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

如果有人想知道我为什么要这样做......它是调试的一部分,最初我想从表单上的文本框中获取数字,但是没有使用 tryParse 进行转换并抛出异常Parse/Convert 因此我将代码修剪下来进行调试....

我得到的异常是:System.FormatException: '输入字符串的格式不正确。'

更多详情:

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)
Form1.vb 中的 USBreceiver.Form1.Btn_confirm_details_Click(Object, System.EventArgs)
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(参考 System.Windows.Forms.Message)
System.Windows.Forms.ButtonBase.WndProc(参考 System.Windows.Forms.Message)
System.Windows.Forms.Button.WndProc(参考 System.Windows.Forms.Message)
... [调用堆栈被截断]

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

我不禁想到语言环境/全球化发生了一些奇怪的事情(我在英国使用 VS2019 和 .net 4.7.2,它是一个 VB.net Windows 窗体应用程序。异常的“来源”是 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 卸载了美式键盘布局 - 但它没有修复任何问题。

有什么想法吗? 谢谢,

【问题讨论】:

  • 您的代码文件编码有问题。将您的代码文件另存为 Unicode。
  • 代码文件是源码吗?还是编译...?选项在哪里,是项目属性吗?
  • 将此代码保存为 UTF-8 编码的文件。
  • "‭1" 不是 "1" :引号内有一个不可打印的字符。如果您Console.WriteLine("‭1"),您将看到?1 而不是1。这是复制/粘贴问题。
  • 谢谢 Jimi,也非常有用,输入了“1”,所以这绝对是一个编码问题,但我按照你所说的对文本框中的文本做了什么,它有 '?'由于复制和粘贴...谢谢

标签: windows vb.net winforms globalization mscorlib


【解决方案1】:

@JohnL 你是我的救星!!!!

我在这件事上浪费了一整天的时间,真的很棒......

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

谢谢!!!

【讨论】:

  • 感谢 JohnyL -> 我在 SourceFile 的“另存为”中看到了选项 - 使用编码保存的下拉箭头!谢谢!
  • 如果您有问题的答案,即使您自己发布,也请接受该答案,以便我们可以看到不需要任何帮助,无需打开问题并阅读全文.
  • 还要确保使用 Jimi 的方法“Console.WriteLine()”来测试任何行为不端的字符串!
  • 显然我必须等待 2 天才能关闭答案,但我会的,谢谢 jmcilhinney
  • 我没有意识到这一点。很高兴知道。
猜你喜欢
  • 2023-03-14
  • 2011-11-23
  • 2022-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多