469,138 Members | 1,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,138 developers. It's quick & easy.

DirectCast vs CType

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

Nov 20 '05 #1
6 10453
Hi,

Directcast is faster. See the Conversion Functions, CType,
DirectCast, and System.Convert section for more info.

http://msdn.microsoft.com/library/de...tinternals.asp

Ken
-----------------
"Ot" <ur***@tds.invalid (use net)> wrote in message
news:uI**************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #2
OT,

Here's a quick outline of the differences and recommendations of where you
should ctype and directcast (IHMO anyway)

1) CType is capable of a *cast* or a *conversion*. DirectCast can only
*cast*

By "conversion" I mean converting one datatype to another (e.g. string to
integer, decimal to integer, object to string etc).

By "cast" I mean changing one type of object into another type that is
related to it by one of the following rules:

a) The type you're converting the object to must be the same type
e.g.
--------------
Dim a as String = "hello"
Dim b as Object = a
Dim c as string = directcast(b, String)
--------------

Variable b is an object that holds a string, so you can cast it to a string.

b) If converting to an interface, the type you're converting must implement
the interface
e.g.
---------------------
Dim a as New MyInterfaceObejct
Dim b as IInterface = directcast(a, IInterface)
----------------------

c) If converting to a derived type, the runtime type of the object must be
the derived type or one of it's own derived types :S
e.g.
----------------------
Dim a as Base = New Derived
Dim b as Derived = directcast(a, Derived)
----------------------

2) Use directcast whenever a "type relationship" exists - it is slightly
faster (in some cases anyway), but forces you to be more aware of
conversions that are going on.

3) Use Ctype when a type relationship doesn't exist, but a value
relationship does (e.g. converting the string "123" to the integer 123)

The difference has been discussed a lot latley in this group. Have a look at
the following for more reference:

http://tinyurl.com/3e8rg
http://tinyurl.com/3ensw
HTH,

Trev.
Nov 20 '05 #3
Cor
Hi Ot,

I thought why am I almost never using CType, and when I saw the answer from
Trev I knew.

I use in that kind of situations mostly
Cdate,
Cint
Cdouble
..tostring

In all other situations it is with me
Directcast (there should be a shortcut on the keyboard for that I think)

Cor
Nov 20 '05 #4
Ot,
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. In a nut shell that is it!

DirectCast will only allow the cast if the parameter is already of the
requested type. Where as CType will find a routine to do a conversion if
available.

For example:
Dim o1 As Object
Dim o2 As Object
Dim i As Integer
o1 = 1 ' o1 contains a boxed Integer
o2 = "1" ' o2 contains a string

' This works as o1 contains an integer
i = DirectCast(o1, Integer)

' This fails as o2 really contains a string
i = DirectCast(o2, Integer)

' this works as there is a conversion from String to Integer
i = CType(o2, Integer)

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. Casting deals with inheritance and what types a variable looks like, it
looks at both Inherits & Implements keywords. When you use Cast you are
changing what type the variable looks like, not actually changing what type
it is. For example System.IO.Stream: System.IO.MemoryStream inherits from
Stream so a Memory stream is of type MemoryStream and it is also of type
Stream. MemoryStream is "down the hierarchy" from Stream, so if you have a
Stream variable that contains a MemoryStream object, you can use DirectCast
to look at that variable in its true form a MemoryStream.

You do understand that when Class1 Inherits from Class2 that Class1 is both
the Class1 type & the Class2 type? And that when Class1 Implements
Interface1, that Class1 is both the Class1 type & the Interface1 type? As
understanding how Inherits & Implements work is needed to understand how
casting works...

However in my example above, String does not inherit (or implement) from
Integer, nor Integer from String, hence the DirectCast does not work, where
as CType knows there is a routine that will convert (change the type) of
String into Integer, hence it does work.

Hope this helps
Jay
"Ot" <ur***@tds.invalid (use net)> wrote in message
news:uI**************@TK2MSFTNGP11.phx.gbl... 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

Nov 20 '05 #5
Ot
Thank you all, your responses are appreciated.

Jay,

I think I have it....

Class A

Class B
inherits A

Class C
inherits B

Class D
inherits C

In that case if I have an object that is an instance of D I could cast it
to any of A,B,C,D.

However an object of class C could be cast as A,B or C, but not D.

If I use, say, a hashtable to store some integers and later get them (they
are objects when they come back). I could CDbl(o) but not
DirectCast(o,Long) because the object is really an Integer which is not
also a Long or Double even though a conversion exists.

Regards,
Ot

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eX**************@TK2MSFTNGP11.phx.gbl...
Ot,
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. In a nut shell that is it!

DirectCast will only allow the cast if the parameter is already of the
requested type. Where as CType will find a routine to do a conversion if
available.

For example:
Dim o1 As Object
Dim o2 As Object
Dim i As Integer
o1 = 1 ' o1 contains a boxed Integer
o2 = "1" ' o2 contains a string

' This works as o1 contains an integer
i = DirectCast(o1, Integer)

' This fails as o2 really contains a string
i = DirectCast(o2, Integer)

' this works as there is a conversion from String to Integer
i = CType(o2, Integer)

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. Casting deals with inheritance and what types a variable looks like, it
looks at both Inherits & Implements keywords. When you use Cast you are
changing what type the variable looks like, not actually changing what

type it is. For example System.IO.Stream: System.IO.MemoryStream inherits from
Stream so a Memory stream is of type MemoryStream and it is also of type
Stream. MemoryStream is "down the hierarchy" from Stream, so if you have a Stream variable that contains a MemoryStream object, you can use DirectCast to look at that variable in its true form a MemoryStream.

You do understand that when Class1 Inherits from Class2 that Class1 is both the Class1 type & the Class2 type? And that when Class1 Implements
Interface1, that Class1 is both the Class1 type & the Interface1 type? As
understanding how Inherits & Implements work is needed to understand how
casting works...

However in my example above, String does not inherit (or implement) from
Integer, nor Integer from String, hence the DirectCast does not work, where as CType knows there is a routine that will convert (change the type) of
String into Integer, hence it does work.

Hope this helps
Jay
"Ot" <ur***@tds.invalid (use net)> wrote in message
news:uI**************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #6
Ot,
Looks like you got it.

Jay

"Ot" <ur***@tds.invalid (use net)> wrote in message
news:O2****************@TK2MSFTNGP10.phx.gbl...
Thank you all, your responses are appreciated.

Jay,

I think I have it....

Class A

Class B
inherits A

Class C
inherits B

Class D
inherits C

In that case if I have an object that is an instance of D I could cast it
to any of A,B,C,D.

However an object of class C could be cast as A,B or C, but not D.

If I use, say, a hashtable to store some integers and later get them (they
are objects when they come back). I could CDbl(o) but not
DirectCast(o,Long) because the object is really an Integer which is not
also a Long or Double even though a conversion exists.

Regards,
Ot

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eX**************@TK2MSFTNGP11.phx.gbl...
Ot,
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. In a nut shell that is it!

DirectCast will only allow the cast if the parameter is already of the
requested type. Where as CType will find a routine to do a conversion if
available.

For example:
Dim o1 As Object
Dim o2 As Object
Dim i As Integer
o1 = 1 ' o1 contains a boxed Integer
o2 = "1" ' o2 contains a string

' This works as o1 contains an integer
i = DirectCast(o1, Integer)

' This fails as o2 really contains a string
i = DirectCast(o2, Integer)

' this works as there is a conversion from String to Integer
i = CType(o2, Integer)

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.

Casting deals with inheritance and what types a variable looks like, it
looks at both Inherits & Implements keywords. When you use Cast you are
changing what type the variable looks like, not actually changing what

type
it is. For example System.IO.Stream: System.IO.MemoryStream inherits from Stream so a Memory stream is of type MemoryStream and it is also of type
Stream. MemoryStream is "down the hierarchy" from Stream, so if you have

a
Stream variable that contains a MemoryStream object, you can use

DirectCast
to look at that variable in its true form a MemoryStream.

You do understand that when Class1 Inherits from Class2 that Class1 is

both
the Class1 type & the Class2 type? And that when Class1 Implements
Interface1, that Class1 is both the Class1 type & the Interface1 type? As understanding how Inherits & Implements work is needed to understand how
casting works...

However in my example above, String does not inherit (or implement) from
Integer, nor Integer from String, hence the DirectCast does not work,

where
as CType knows there is a routine that will convert (change the type) of
String into Integer, hence it does work.

Hope this helps
Jay
"Ot" <ur***@tds.invalid (use net)> wrote in message
news:uI**************@TK2MSFTNGP11.phx.gbl...
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



Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Tubs | last post: by
13 posts views Thread by Crirus | last post: by
6 posts views Thread by Mark Nethercott | last post: by
7 posts views Thread by Brian Henry | last post: by
1 post views Thread by iwdu15 | last post: by
4 posts views Thread by Vagabond Software | last post: by
3 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.