Integer.TryParse - a better way?

后端 未结 7 1623
礼貌的吻别
礼貌的吻别 2020-12-08 06:51

I find myself often needing to use Integer.TryParse to test if a value is an integer. However, when you use TryParse, you have to pass a reference variable to the function,

7条回答
  •  心在旅途
    2020-12-08 07:37

    Try this code.

    Module IntegerHelpers
    
      Function IsInteger(ByVal p1 as String) as Boolean
        Dim unused as Integer = 0
        return Integer.TryParse(p1,unused)
      End Function
    End Module
    

    The nice part is that since it's declared as a Module level function it can be used without a qualifier. Example Usage

    return IsInteger(mInt)
    

提交回复
热议问题