vb.net 检查整数的最安全方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/580429/
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
Safest way to check for integer
提问by CodeKiwi
This is probally more of an elegance question than functionality. I am looking for the absolutely safest way to check for an integer from a string and an object,
这可能更像是一个优雅的问题而不是功能。我正在寻找从字符串和对象中检查整数的绝对最安全的方法,
Using most of the built in functions for this in .net seem to generate a first chance exception, displayed in the Immediate window, and over time they just build up. what are the implications of these exceptions as they don't seem to affect the operation of the system.
在 .net 中使用大多数内置函数似乎会产生第一次机会异常,显示在立即窗口中,并且随着时间的推移它们只是建立起来。这些异常的含义是什么,因为它们似乎不会影响系统的运行。
Here are my two attempts, both feel clunky and I know there has to be a better way than using VB.IsDBNull and Integer.TryParse... Or am I just being anal.
这是我的两次尝试,都感觉笨拙,我知道必须有比使用 VB.IsDBNull 和 Integer.TryParse 更好的方法......或者我只是在肛门。
(to integer from object)
(从对象到整数)
Dim nInteger As Integer = 0
If oData Is Nothing OrElse VB.IsDBNull(oData) Then
Else
If bThrowErrorIfInvalid Then
Else
On Error Resume Next
End If
nInteger = CType(oData, Integer)
End If
Return nInteger
(to integer from string)
(从字符串到整数)
Dim nInteger As Integer = 0
If sText Is Nothing Then
Else
If bThrowErrorIfInvalid Then
Else
On Error Resume Next
End If
Integer.TryParse(sText, nInteger)
End If
Return nInteger
回答by Rosstified
Whats wrong with using the Integer.TryParse? Thats what it was made for...
使用 Integer.TryParse 有什么问题?这就是它为...
int i = 0;
string toTest = "not number";
if(int.TryParse(toTest, out i))
{
// it worked
}
How is that clunky? (C# not VB i know, but same diff)
怎么这么笨?(我知道 C# 不是 VB,但相同的差异)
EDIT: Added if you want to check from an object as well (since TryParse relies on a string) and im not too sure on how you actually plan to use it. Does that cover your concerns a little since this one method will check for both of your cases?
编辑:如果您还想从对象中检查(因为 TryParse 依赖于字符串)并且我不太确定您实际计划如何使用它,则添加。这是否涵盖了您的担忧,因为这种方法将检查您的两种情况?
static bool TryParseInt(object o, out int i)
{
i = 0;
if (o.GetType() == typeof(int))
{
i = (int)o;
return true;
}
else if (o.GetType() == typeof(string))
{
return int.TryParse(o as string, out i);
}
return false;
}
回答by Fritz H
You can try this:
你可以试试这个:
Dim i as Integer
Try
i = Convert.ToInt32(obj)
Catch
' This ain't an int
End Try
Convert
is in the System
namespace.
Convert
位于System
命名空间中。
EDIT: Note: If you are going to put any other code in the Try
block, make sure to specify that the only exception the Catch
should catch is the exception thrown by Convert.ToInt32
if/when it fails - otherwise you could end up with a nasty problem if something else in that try/catch should fail.
编辑:注意:如果您打算在Try
块中放置任何其他代码,请确保指定Catch
应该捕获的唯一异常是Convert.ToInt32
if/when 失败时抛出的异常- 否则如果出现某些问题,您最终可能会遇到令人讨厌的问题否则在 try/catch 中应该失败。
回答by Jeromy Irvine
Integer.TryParse
is designed to be the safe way to do this: that's why it was put into the framework in the first place. For an Object, you could always just call ToString()
before using TryParse
.
Integer.TryParse
旨在成为执行此操作的安全方式:这就是为什么它首先被放入框架中。对于一个对象,你总是可以ToString()
在使用之前调用TryParse
。
I'd also avoid using On Error Resume Next
in favor of a Try-Catch
block if you need to swallow an error for some reason, as it's much less likely to cause an unwanted side effect.
我也想避免使用On Error Resume Next
赞成的Try-Catch
块,如果你需要吞下一个错误出于某种原因,因为它不太可能造成不必要的副作用。
回答by Jacob Adams
Since it's VB you can also use the IsNumeric function
因为它是 VB 你也可以使用 IsNumeric 函数
回答by Jimmy
it seems in your second example, you have a unnecessary check for bThrowErrorIfInvalid because Integer.TryParse never throws an error. Something Like
在您的第二个示例中,您似乎对 bThrowErrorIfInvalid 进行了不必要的检查,因为 Integer.TryParse 从不引发错误。就像是
If bThrowErrorIfInvalid And Not Integer.TryParse(sText, nInteger) Then
Throw New ArgumentException()
EndIf
回答by Geoffrey Lee
Dim d As Double = 2.0
Dim i As Integer = CInt(d)
If d - i = 0 Then
Debug.WriteLine("is integer")
Else
Debug.WriteLine("not a integer")
End If