vb.net 将对象转换为整数的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16582146/
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
Best way to convert object to integer
提问by George Filippakos
I wish to safely convert an objectfrom an external cache to an Integer type.
我希望安全地将对象从外部缓存转换为 Integer 类型。
The only way I can seem to do this is inside a try catch block like so:
我似乎可以做到这一点的唯一方法是在 try catch 块中,如下所示:
Try
Return Convert.ToInt32(obj)
Catch
'do nothing
End Try
I hate writing catch statements like this.
我讨厌写这样的 catch 语句。
Is there a better way?
有没有更好的办法?
I have tried:
我试过了:
TryCast(Object, Int32)
Doesn't work (must be reference type)
不起作用(必须是引用类型)
Int32.TryParse(Object, result)
Doesn't work (must be a string type)
不起作用(必须是字符串类型)
UPDATE
更新
I like the comment posted by Jodrell - this would make my code look like this:
我喜欢 Jodrell 发表的评论——这将使我的代码看起来像这样:
Dim cacheObject As Object = GlobalCache.Item(key)
If Not IsNothing(cacheObject) Then
If TypeOf cacheObject Is Int32 Then
Return Convert.ToInt32(cacheObject)
End If
End If
'Otherwise get fresh data from DB:
Return GetDataFromDB
采纳答案by Jodrell
Unesscessary conversion to Stringshould be avoided.
String应该避免不必要的转换。
You could use Isto check the type beforehand
您可以使用Is预先检查类型
Dim value As Integer
If TypeOf obj Is Integer Then
value = DirectCast(obj, Integer)
Else
' You have a problem
End If
or,
或者,
You could implement a variation on TryCastlike this,
你可以TryCast像这样实现一个变体,
Function BetterTryCast(Of T)(ByVal o As Object, ByRef result As T) As Boolean
Try
result = DirectCast(o, T)
Return True
Catch
result = Nothing
Return False
End Try
End Function
Which you could use like this
你可以像这样使用
Dim value As Integer
If BetterTryCast(obj, value) Then
// It worked, the value is in value.
End If
回答by Marc Gravell
Clarification: the question was originally tagged c#vb.net; the following applies to C# only(although may be translated into VB.NET):
澄清:该问题最初被标记为c# vb.net;以下内容仅适用于 C#(虽然可能会被翻译成 VB.NET):
If it is a boxed int, then:
如果是 boxed int,则:
object o = 1, s = "not an int";
int? i = o as int?; // 1, as a Nullable<int>
int? j = s as int?; // null
so generalising:
所以概括:
object o = ...
int? i = o as int?;
if(i == null) {
// logic for not-an-int
} else {
// logic for is-an-int, via i.Value
}
回答by Steve
The simplest one is
最简单的一种是
Int32.TryParse(anObject.ToString, result)
Every Object has a ToString method and calling Int32.TryParse will avoid a costly (in terms of perfomance) exception if you Object is not a numeric integer. Also the value for result, if the object is not a string will be zero.
每个 Object 都有一个 ToString 方法,如果 Object 不是数字整数,则调用 Int32.TryParse 将避免代价高昂的(就性能而言)异常。如果对象不是字符串,则结果的值也将为零。
EDIT. The answer from Marc Gravell raised my curiosity. Its answer seems complex for a simple conversion, but it is better? So I have tried to look at the IL code produced by its answer
编辑。Marc Gravell 的回答引起了我的好奇。对于简单的转换,它的答案似乎很复杂,但它更好吗?所以我试图查看其答案产生的 IL 代码
object o = 1, s = "not an int";
int? i = o as int?; // 1, as a Nullable<int>
int? j = s as int?; // null
IL CODE
代码
IL_0000: ldc.i4.1
IL_0001: box System.Int32
IL_0006: stloc.0 // o
IL_0007: ldstr "not an int"
IL_000C: stloc.1 // s
while the IL CODE produced by my answer is the following
而我的回答产生的 IL CODE 如下
IL_0000: ldc.i4.1
IL_0001: box System.Int32
IL_0006: stloc.0 // anObject
IL_0007: ldloc.0 // anObject
IL_0008: callvirt System.Object.ToString
IL_000D: ldloca.s 01 // result
IL_000F: call System.Int32.TryParse
Definitively the answer from Marc is the best approach. Thanks Marc to let me discover something new.
毫无疑问,Marc 的答案是最好的方法。感谢 Marc 让我发现新的东西。
回答by Andrew
this works:
这有效:
Int32.TryParse(a.ToString(), out b);

