在评估对象时,IsNumeric抛出FormatException

Ric*_*eri 2 .net vb.net compact-framework windows-ce visual-studio

我正在使用visual studio 2008开发用于Windows CE 6.0,紧凑框架的软件.

我有这个"奇怪的?" 使用isNumeric方法时出现问题.还有另一种更好的方法来完成这项工作吗?为什么让我成为例外?(事实上​​两个......都是FormatException类型)

谢谢

dim tmpStr as object = "Hello"
if isNumeric(tmpStr) then    // EXCEPTIONs on this line
    // It's a number
else
    // it's a string
end if
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 5

尽管FormatException未在文档中列出IsNumeric它确实是可以抛出的异常之一.抛出它的情况是

  • 传递了一个字符串值
  • 该字符串没有0x&H前缀

我找不到这种行为的任何理由.我甚至能够辨别它的唯一方法是挖掘反射器中的实现.

解决它的最佳方法似乎是定义一个包装器方法

Module Utils
  Public Function IsNumericSafe(ByVal o As Object) As Boolean
    Try
      Return IsNumeric(o)
    Catch e As FormatException
      Return False
    End Try
  End Function
End Module
Run Code Online (Sandbox Code Playgroud)