您收到此错误的原因实际上是因为 CF 不包含TryParse
方法。另一种解决方案是使用正则表达式:
Public Function CheckIsNumeric(ByVal inputString As String) As Boolean
Return Regex.IsMatch(inputString, "^[0-9 ]+$")
End Function
编辑
这是一个更全面的正则表达式,应该匹配任何类型的数字:
Public Function IsNumeric(value As String) As Object
'bool variable to hold the return value
Dim match As Boolean
'regula expression to match numeric values
Dim pattern As String = "(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)"
'generate new Regulsr Exoression eith the pattern and a couple RegExOptions
Dim regEx As New Regex(pattern, RegexOptions.Compiled Or RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
'tereny expresson to see if we have a match or not
match = If(regEx.Match(value).Success, True, False)
'return the match value (true or false)
Return match
End Function
有关详细信息,请参阅本文:http ://www.dreamincode.net/code/snippet2770.htm