O
Ot
I apparently have a bit to learn about Casting and Conversion. I have been
thinking of them as the same but a discussion in another thread leads me to
believe that this is wrong thinking.
I found this in the VB Language reference:
<quote>
The DirectCast keyword introduces a type conversion operation. You use it
the same way you use the CType keyword....
Both keywords take an expression to be converted as the first argument, and
the type to convert it to as the second argument. Both conversions fail if
there is no conversion defined between the data type of the expression and
the data type specified as the second argument.
The difference between the two keywords 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.
DirectCast is special in that conversions from type Object to any other
type are performed as a direct cast down the hierarchy - all of the special
conversion behaviors from Object are ignored.
</quote>
This last sentence is a bit unclear to me. I am not sure what "special
conversion behaviors" might be in an Object.
Further, it says that DirectCast conversions are "performed as a direct
cast down the hierarchy..." which may be true but is decidedly unhelpful if
one doesn't know what a "direct cast" is in the first place.
----------------------
"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."
My interpretation: An object to be DirectCast must already be of the
desired type. If you know this in advance, DirectCast is faster than
CType.
Thanks in advance,
Ot
thinking of them as the same but a discussion in another thread leads me to
believe that this is wrong thinking.
I found this in the VB Language reference:
<quote>
The DirectCast keyword introduces a type conversion operation. You use it
the same way you use the CType keyword....
Both keywords take an expression to be converted as the first argument, and
the type to convert it to as the second argument. Both conversions fail if
there is no conversion defined between the data type of the expression and
the data type specified as the second argument.
The difference between the two keywords 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.
DirectCast is special in that conversions from type Object to any other
type are performed as a direct cast down the hierarchy - all of the special
conversion behaviors from Object are ignored.
</quote>
This last sentence is a bit unclear to me. I am not sure what "special
conversion behaviors" might be in an Object.
Further, it says that DirectCast conversions are "performed as a direct
cast down the hierarchy..." which may be true but is decidedly unhelpful if
one doesn't know what a "direct cast" is in the first place.
----------------------
"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."
My interpretation: An object to be DirectCast must already be of the
desired type. If you know this in advance, DirectCast is faster than
CType.
Thanks in advance,
Ot