Integer.TryParse - a better way?

后端 未结 7 1580
礼貌的吻别
礼貌的吻别 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:27

    No need to declare the integer.

    If Integer.TryParse(intToCheck, 0) Then
    

    or

    If Integer.TryParse(intToCheck, Nothing) Then
    

    If you have .Net 3.5 ability you can create an extension method for strings.

    Public Module MyExtensions
    
        <System.Runtime.CompilerServices.Extension()> _
        Public Function IsInteger(ByVal value As String) As Boolean
            If String.IsNullOrEmpty(value) Then
                Return False
            Else
                Return Integer.TryParse(value, Nothing)
            End If
        End Function
    
    End Module
    

    And then call like:

    If value.IsInteger() Then
    

    Sorry, getting carried away I know, but also you can add this to the MyExtensions class above in .Net 3.5 and not worry unless you need validations.

    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToInteger(ByVal value As String) As Integer
        If value.IsInteger() Then
            Return Integer.Parse(value)
        Else
            Return 0
        End If
    End Function
    

    Then simply use

    value.ToInteger()
    

    This will return 0 if it isn't a valid Integer.

    0 讨论(0)
  • 2020-12-08 07:32
    public static class Util {
    
        public static Int32? ParseInt32(this string text) {
            Int32 result;
            if(!Int32.TryParse(text, out result))
                return null;
            return result;
        }
    
        public static bool IsParseInt32(this string text) {
            return text.ParseInt32() != null;
        }
    
    }
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-08 07:44

    J Ambrose Little performed timing tests for IsNumeric checks back in 2003. You may wish to retry the mentioned tests with v2 of the CLR.

    0 讨论(0)
  • 2020-12-08 07:48

    Since you are using VB.net you can use the IsNumeric Function

    If IsNumeric(myInt) Then
        'Do Suff here
    End If
    
    0 讨论(0)
  • 2020-12-08 07:52

    Why not write an extension method to clean up your code? I haven't written VB.Net for a while, but here is an example in c#:

    public static class MyIntExtensionClass
    {
      public static bool IsInteger(this string value)
      {
        if(string.IsNullOrEmpty(value))
          return false;
    
        int dummy;
        return int.TryParse(value, dummy);
      }
    }
    
    0 讨论(0)
提交回复
热议问题