Casting variables in VB.NET
-
What is the difference between Directcast and CType? Also is it better to use CType(MyObect, Integer) than CInt(MyObject) - does it get compiled to the same MSIL or is a helper function called in the Microsoft.VisualBasic.dll? Jim
-
What is the difference between Directcast and CType? Also is it better to use CType(MyObect, Integer) than CInt(MyObject) - does it get compiled to the same MSIL or is a helper function called in the Microsoft.VisualBasic.dll? Jim
While
DirectCast
andCType
are both used for casting,DirecCast
can only case object to object they have a direct relationship (ie. inheritance ) with. For example, if B inherits A, and C inherits A, you can useDirectCast
to cast a C object to A. OTOH,CType
can be used to cast from any object to any object. The advantage ofDirectCast
is that it's much faster thanCType
, so use it whenever appropriate. Looking through the IL forCType
andCInt
, they're actually doing the same thing, so I assume there's no difference in using either.
"if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler. Support Bone
-
What is the difference between Directcast and CType? Also is it better to use CType(MyObect, Integer) than CInt(MyObject) - does it get compiled to the same MSIL or is a helper function called in the Microsoft.VisualBasic.dll? Jim
CType is more flexible because it's not limited to converting between standard types. It can be used to convert between composite types as well as converting an object to any one of its interfaces. CType is also compiled in-line with the expression whereas the CInt is not. CInt calls a library function to convert an already evaluated expression result whereas CType code is actually compiled as part of the expression. Now, DirectCast... The difference between the two is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType Consider the following code:
Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.The run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer. Clear as mud? ;) RageInTheMachine9532
-
CType is more flexible because it's not limited to converting between standard types. It can be used to convert between composite types as well as converting an object to any one of its interfaces. CType is also compiled in-line with the expression whereas the CInt is not. CInt calls a library function to convert an already evaluated expression result whereas CType code is actually compiled as part of the expression. Now, DirectCast... The difference between the two is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType Consider the following code:
Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.The run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer. Clear as mud? ;) RageInTheMachine9532
Thanks, that is a lot clearer. Jim