Reputation: 23275
How should I cast from an Object
to an Integer
in VB.NET?
When I do:
Dim intMyInteger as Integer = TryCast(MyObject, Integer)
it says:
TryCast operand must be reference type, but Integer is a value type.
Upvotes: 19
Views: 49681
Reputation: 2109
The equivalent for TryCast is CType. Both will do a type conversion if it is possible. By contrast, DirectCast will only convert the type if it is that type already.
To illustrate, you can use CType to convert a String, or Short, or Double, to an Integer. DirectCast will 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.
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 TryCast semantics, I suggest using a function like this:
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:
TryCastInteger converted to: 1 (type: System.Int32)
Could not convert to Integer
There also is such a thing as a null/Nothing Integer, or any other static type, called a "nullable" type, see Variable declaration question mark for some more information. But that does not really make it a "reference" type either.
Upvotes: 2
Reputation: 2797
You can use this:
Dim intMyInteger as Integer
Integer.TryParse(MyObject, intMyInteger)
Upvotes: 5
Reputation: 137398
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
.
Instead, you can use TypeOf
and Is
:
If TypeOf MyObject Is Integer Then
intMyInteger = DirectCast(MyObject, Integer)
Else
intMyInteger = 0
End If
This tests to see if the runtime type of MyObject
is Integer
. See the MSDN documentation on the TypeOf
operator for more details.
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.
Upvotes: 34