无法创建函数来检查字符串的特定格式

时间:2014-10-04 03:41:40

标签: vb.net string tryparse

我试图创建一个函数来检查字符串是否采用坐标格式(例如:362,352),但我似乎无法正确判断它。这是我尝试过的:

Private Function CheckPointIsValid(ByVal CoordsText As String) As Boolean
    If CoordsText.Contains(",") AndAlso Integer.TryParse(CoordsText = CoordsText.Replace(",", String.Empty), Nothing) Then Return True
    Return False
End Function

无论传递给函数的是什么,总是返回false。

2 个答案:

答案 0 :(得分:1)

检查你的TryParse ......你把它设置为空,你忘记了其他的返回假...请参阅下面的答案。

 Dim intNumber As Integer = 0
 Dim result As Boolean = Int32.TryParse(CoordsText.Replace(",",String.Empty), intNumber)

所以这是你的功能......

 Private Function CheckPointIsValid(ByVal CoordsText As String) As Boolean

 Dim intNumber As Integer = 0
 Dim result As Boolean = Int32.TryParse(CoordsText.Replace(",",String.Empty), intNumber)    

  Return CBool(CoordsText.Contains(",") AndAlso result)

 End Function

答案 1 :(得分:0)

这就是我现在正在使用的(它工作正常):

Private Function CheckPointIsValid(ByVal CoordsText As String) As Boolean
    If CoordsText.Contains(",") AndAlso Integer.TryParse(CoordsText.Replace(",", String.Empty), Nothing) = True Then Return True
    Return False
End Function