vb.net Integer.TryParse - 更好的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/419027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:00:40  来源:igfitidea点击:

Integer.TryParse - a better way?

vb.nettryparse

提问by Ryan Smith

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, so I find myself always needing to create a blank integer to pass in. Usually it looks something like:

我发现自己经常需要使用 Integer.TryParse 来测试一个值是否为整数。然而,当你使用 TryParse 时,你必须向函数传递一个引用变量,所以我发现自己总是需要创建一个空白整数来传入。通常它看起来像:

Dim tempInt as Integer
If Integer.TryParse(myInt, tempInt) Then

I find this to be quite cumbersome considering that all I want is a simple True / False response. Is there a better way to approach this? Why isn't there an overloaded function where I can just pass the value I want to test and get a true / false response?

考虑到我想要的只是一个简单的 True/False 响应,我觉得这很麻烦。有没有更好的方法来解决这个问题?为什么没有重载函数,我可以在其中传递我想要测试的值并获得真/假响应?

回答by Tom Anderson

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.

如果你有 .Net 3.5 能力,你可以为字符串创建一个扩展方法。

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.

抱歉,我知道得意忘形了,但您也可以将其添加到 .Net 3.5 中的 MyExtensions 类中,除非您需要验证,否则不用担心。

<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。

回答by TonyB

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

由于您使用的是 VB.net,因此您可以使用 IsNumeric 函数

If IsNumeric(myInt) Then
    'Do Suff here
End If

回答by yfeldblum

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;
    }

}

回答by JaredPar

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)

回答by Jason Hymanson

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

为什么不写一个扩展方法来清理你的代码?我有一段时间没有写 VB.Net,但这里有一个 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);
  }
}

回答by icelava

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

J Ambrose Little早在 2003 年就对 IsNumeric 检查进行了计时测试。您可能希望使用 CLR 的 v2 重试上述测试。

回答by Cerveser

A variation would be:

一个变化是:

Int32.TryParse(input_string, Globalization.NumberStyles.Integer)