string 如何在 VB.NET 中从对象转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13151196/
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
How to cast from Object to Integer in VB.NET?
提问by CJ7
How should I cast from an Object
to an Integer
in VB.NET?
我应该如何在 VB.NET 中从 an 转换Object
为 an Integer
?
When I do:
当我做:
Dim intMyInteger as Integer = TryCast(MyObject, Integer)
it says:
它说:
TryCast operand must be reference type, but Integer is a value type.
TryCast 操作数必须是引用类型,而 Integer 是值类型。
回答by Jonathon Reinhart
TryCast
is the equivalent of C#'s as
operator. It is a "safe cast" operator that doesn't throw an exception if the cast fails. Instead, it returns Nothing
(null
in C#). The problem is, you can't assign Nothing
(null
) (a reference type) to an Integer
(a value type). There is no such thing as an Integer
null
/Nothing
.
TryCast
相当于 C# 的as
运算符。它是一个“安全转换”运算符,如果转换失败,它不会抛出异常。相反,它返回Nothing
(null
在 C# 中)。问题是,您不能将Nothing
( null
) (引用类型)分配给(Integer
值类型)。没有像Integer
null
/这样的东西Nothing
。
Instead, you can use TypeOf
and Is
:
相反,您可以使用TypeOf
和Is
:
If TypeOf MyObject Is Integer Then
intMyInteger = DirectCast(MyObject, Integer)
Else
intMyInteger = 0
End If
This tests to see if the runtime typeof MyObject
is Integer
. See the MSDN documentation on the TypeOf
operatorfor more details.
这将测试以查看 的运行时类型是否MyObject
为Integer
。有关更多详细信息,请参阅有关TypeOf
运算符的MSDN 文档。
You could also write it like this:
你也可以这样写:
Dim myInt As Integer = If(TypeOf myObj Is Integer, DirectCast(myObj,Integer), 0)
Furthermore, if an integer with a default value (like 0) is not suitable, you could consider a Nullable(Of Integer)
type.
此外,如果具有默认值(如 0)的整数不合适,您可以考虑一个Nullable(Of Integer)
类型。
回答by CoOl
You can use this:
你可以使用这个:
Dim intMyInteger as Integer
Integer.TryParse(MyObject, intMyInteger)
回答by Swordblaster
Use Directcast and catch InvalidCastException
使用 Directcast 并捕获 InvalidCastException
回答by Abacus
The equivalent for TryCastis CType. Both will do a type conversion if it is possible. By contrast, DirectCastwill only convert the type if it isthat type already.
TryCast的等效项是CType。如果可能,两者都会进行类型转换。相比之下,DirectCast只会转换已经是该类型的类型。
To illustrate, you can use CTypeto convert a String, or Short, or Double, to an Integer. DirectCastwill generally give you a syntax/compile error if you do that; but if you try to go around the error by using type Object (this is called "boxing" and "unboxing"), it will throw an exception at run-time.
举例来说,您可以使用CType将 String、Short 或 Double 转换为 Integer。如果你这样做,DirectCast通常会给你一个语法/编译错误;但是如果您尝试使用 Object 类型(这称为“装箱”和“拆箱”)来解决错误,它将在运行时抛出异常。
Dim OnePointTwo As Object = "1.2"
Try
Dim temp = CType(OnePointTwo, Integer)
Console.WriteLine("CType converted to: " & temp.ToString & " (type: " & temp.GetType.ToString & ")")
Catch ex As Exception
Console.WriteLine("CType threw exception")
End Try
Try
Dim temp = DirectCast(OnePointTwo, Integer)
Console.WriteLine("DirectCast converted to: " & temp.ToString & " (type: " & temp.GetType.ToString & ")")
Catch ex As Exception
Console.WriteLine("DirectCast threw exception")
End Try
This will output:
这将输出:
CType converted to: 1 (type: System.Int32)
DirectCast threw exception
So to most closely follow the TryCastsemantics, I suggest using a function like this:
因此,为了最密切地遵循TryCast语义,我建议使用这样的函数:
Shared Function TryCastInteger(value As Object) As Integer?
Try
If IsNumeric(value) Then
Return CType(value, Integer)
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
And to illustrate its effect:
并说明其效果:
Shared Sub TestTryCastInteger()
Dim temp As Integer?
Dim OnePointTwo As Object = "1.2"
temp = TryCastInteger(OnePointTwo)
If temp Is Nothing Then
Console.WriteLine("Could not convert to Integer")
Else
Console.WriteLine("TryCastInteger converted to: " & temp.ToString & " (type: " & temp.GetType.ToString & ")")
End If
Dim NotANumber As Object = "bob's your uncle"
temp = TryCastInteger(NotANumber)
If temp Is Nothing Then
Console.WriteLine("Could not convert to Integer")
Else
Console.WriteLine("TryCastInteger converted to: " & temp.ToString & " (type: " & temp.GetType.ToString & ")")
End If
End Sub
Running TestTryCastInteger() will output:
运行 TestTryCastInteger() 将输出:
TryCastInteger converted to: 1 (type: System.Int32)
Could not convert to Integer
There also issuch a thing as a null/Nothing Integer, or any other static type, called a "nullable" type, see Variable declaration question markfor some more information. But that does not really make it a "reference" type either.
还有就是这样的事,作为一个空/没有整型,或任何其他静态类型,称为“可空”类型,请参阅变量声明问号一些更多的信息。但这并没有真正使它成为“参考”类型。