Integer.TryParse - a better way?

后端 未结 7 1626
礼貌的吻别
礼貌的吻别 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: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);
      }
    }
    

提交回复
热议问题