473,583 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CType() versus Convert.ToXXXX( )

could someone please discuss the pros and cons of

CType(MyDouble, Decimal)

versus

Convert.ToDecim al(MyDouble)

.... and other such conversions ...
Nov 20 '05 #1
8 7346
John A Grandy wrote:
could someone please discuss the pros and cons of

CType(MyDouble, Decimal)

versus

Convert.ToDecim al(MyDouble)

... and other such conversions ...


They're fairly equivilent, but I would use CType, because you can be more
consistant; instead of casting between built-in types with Convert and other
types with CType, you can use CType for everything.
Convert is still needed for some things, though, such as
Convert.ToBase6 4String and other similar specialized methods.

- Pete
Nov 20 '05 #2
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
could someone please discuss the pros and cons of

CType(MyDouble, Decimal)

versus

Convert.ToDecim al(MyDouble)


Convert is a function that accepts one data type, and returns another. CType
actually casts the binary data into a new type. So I assume that Convert has
more overhead, but this is just a very loose assumption. Did you check
msdn.microsoft. com, and the .NET documentation yet?
Jeremy

Nov 20 '05 #3
> CType actually casts the binary data into a new type

I don't think this is true. In most cases, CType and Convert.Toxxx do pretty
much the same thing. For example if you look at the IL for the following:

---------------

Dim a as Integer = 5
Dim b as string

b = cstr(a) ' Method 1
b = Convert.ToStrin g(a) ' Method 2

---------------

You'll see that both end up calling System.Int32::T oString() if you trace
through the call stack.

If CType casted the binary data, then you wouldn't get a string containing
"5", you would get a string contining the character with numeric value 5
which isn't the same thing.

In some cases, CType behaves like directcast, but it is better to use
directcast when you can (when the run time types are the same). See other
posts in this group involving "CType vs DirectCast" etc.

IMO, Ctype behaves like a shortcut to Convert.Toxxx with extra added
features such as casting (like directcast).

HTH,

Trev.

"Jeremy Cowles" <jeremy.cowle s[nosp@m]asifl.com> wrote in message
news:Ds******** ************@tw ister.tampabay. rr.com...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
could someone please discuss the pros and cons of

CType(MyDouble, Decimal)

versus

Convert.ToDecim al(MyDouble)
Convert is a function that accepts one data type, and returns another.

CType actually casts the binary data into a new type. So I assume that Convert has more overhead, but this is just a very loose assumption. Did you check
msdn.microsoft. com, and the .NET documentation yet?
Jeremy

Nov 20 '05 #4
"Trev Hunter" <hu*********@ho tmail.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
CType actually casts the binary data into a new type
I don't think this is true. In most cases, CType and Convert.Toxxx do

pretty much the same thing. For example if you look at the IL for the following:


So what is the case when you cast to something other than a string?
Nov 20 '05 #5
So what is the case when you cast to something other than a string?
It depends on the conversion, sometimes an optimized cast is allowed (e.g.
integer to long), sometimes overflow checking has to be done. AFAIK, ctype
allows the compiler to preform optimizations when it can, but an explicit
call to Convert.Toxxx is always predefined - so no optimizations can be done
by the compiler.

HTH,

Trev.

"Jeremy Cowles" <jeremy.cowle s[nosp@m]asifl.com> wrote in message
news:yE******** **********@twis ter.tampabay.rr .com... "Trev Hunter" <hu*********@ho tmail.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
CType actually casts the binary data into a new type
I don't think this is true. In most cases, CType and Convert.Toxxx do

pretty
much the same thing. For example if you look at the IL for the

following:
So what is the case when you cast to something other than a string?

Nov 20 '05 #6
"Trev Hunter" <hu*********@ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
So what is the case when you cast to something other than a string?
It depends on the conversion, sometimes an optimized cast is allowed (e.g.
integer to long), sometimes overflow checking has to be done. AFAIK, ctype
allows the compiler to preform optimizations when it can, but an explicit
call to Convert.Toxxx is always predefined - so no optimizations can be

done by the compiler.


Interesting, still seems to me that Convert is the weaker of the two. But
again, my opinion is just based on assumption.

Jeremy

Nov 20 '05 #7
As a side note, unless you're doing really heavy looping or intensive math,
there won't be any noticable difference between any of the methods.

If you are doing heavy looing or intensive math then you sholuld try and
limit your castings / conversions rather than rely on compiler
optimizations.

Just my 2c

Trev.
Nov 20 '05 #8
Well, the only really weakness is that it involves extra function calls
rather than doing inline processing. Apart from that it's essentially doing
the exact same thing. I'm not sure, but I assume that the JIT compiler is
also capable of inlining the Convert.Toxxx functions to further optimize
(which would make it exactly the same as ctype).

I guess another plus point of ctype is that you can shorten the amount you
have to type by using one of the other "c" functions (cint, cstr etc.) which
in most cases are the came as their ctype equivalent.

As mentioned in my other post, performance at this level is really not all
that relevant to a typical application IME.

HTH,

Trev.
"Jeremy Cowles" <jeremy.cowle s[nosp@m]asifl.com> wrote in message
news:Ma******** **********@twis ter.tampabay.rr .com...
"Trev Hunter" <hu*********@ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
So what is the case when you cast to something other than a string?


It depends on the conversion, sometimes an optimized cast is allowed (e.g. integer to long), sometimes overflow checking has to be done. AFAIK, ctype allows the compiler to preform optimizations when it can, but an explicit call to Convert.Toxxx is always predefined - so no optimizations can be

done
by the compiler.


Interesting, still seems to me that Convert is the weaker of the two. But
again, my opinion is just based on assumption.

Jeremy

Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6560
by: Joeri | last post by:
Hi, I'm struggling with this issue, maybe someone has already solved ik before. Here's the problem I Have : clsA witch is a base class clsB inherits from clsA clsC inherits from clsA clsD inherits from clsA
1
1787
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance would be identical. I was 100% incorrect. The code below produces the results: CType Took: 0.2187528 seconds. Convert Took: 12.187656 seconds.
3
319
by: TS | last post by:
Is there a way to convert a variable from 1 type to another type that you determine at runtime? I'm trying to pull a value from a ListDictionary's key. The datatype of that key must be identical to pull that value. I have a property to pull this value based on the key supplied, but I am using an object type on my parameter. I want to do a...
3
21408
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
5
5054
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
2
4960
by: Jarod_24 | last post by:
Structure myXMLtype Dim Filename As String Dim sql As String End Structure .... Dim obj as New ArrayList ...... return CType(obj.ToArray, myXMLtype) 'This wont work
7
2711
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
3
7706
by: PeterK | last post by:
I am trying to set Public overridable CreditlimitS() as System.Data.SqlTypes.SqlMoney to Creditlimit as Double like CreditLimitS=creditlimit and get this error "Value of type double cannot be converted to System.Data.SqlTypes.SqlMoney " How do I get creditlimit into creditlimitS? There seems to be no conversion function. TIA
8
5197
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello Everything in .Net is an object and CTYPE converts one datatype to another datatype so why use Cstr, Cint or any other method to convert from one datatype to another?
0
7888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.