PC Review


Reply
Thread Tools Rate Thread

about ConvertTo and cast

 
 
tony
Guest
Posts: n/a
 
      31st Aug 2006
Hello!

What is the difference if I use double as cast as in (double)value

compare to using Convert.ToDouble(value)



//Tony


 
Reply With Quote
 
 
 
 
Sanjib Biswas
Guest
Posts: n/a
 
      31st Aug 2006
You should be using Convert class to do the type conversion as it uses
implicit conversions. Where as type casting a value, called explicit or
force conversion, there may be a data loss.

"tony" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello!
>
> What is the difference if I use double as cast as in (double)value
>
> compare to using Convert.ToDouble(value)
>
>
>
> //Tony
>
>



 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      31st Aug 2006
> Hello!
>
> What is the difference if I use double as cast as in (double)value
>
> compare to using Convert.ToDouble(value)
>
> //Tony


if "value" is a double, float, int (or other numerical type), I don't
think there is a difference.

if "value" is a boxed numerical type, but not a boxed decimal, then a
cast will result in a runtime error, while Convert gives the correct
result.
Example:
float f1 = 1.23f;
object o1 = f1;
double d1 = Convert.ToDouble(o1); // succeeds
double d2 = (double)f1; // succeeds, same result
double d3 = (double)o1; // CRASH

Hans Kesting


 
Reply With Quote
 
TheSteph
Guest
Posts: n/a
 
      31st Aug 2006
Example :

int tmpIntBad = (int) '1';
// the value of tmpIntBad is 49

int tmpIntGood = Convert.ToInt32('1');
//the value of tmpIntGood is 1



"tony" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello!
>
> What is the difference if I use double as cast as in (double)value
>
> compare to using Convert.ToDouble(value)
>
>
>
> //Tony
>
>



 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      1st Sep 2006
TheSteph wrote:
> Example :
>
> int tmpIntBad = (int) '1';
> // the value of tmpIntBad is 49
>
> int tmpIntGood = Convert.ToInt32('1');
> //the value of tmpIntGood is 1


No.

Convert.ToInt32('1') also returns 49.

Convert.ToInt32("1") returns 1.

Arne
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      4th Sep 2006
Sanjib Biswas <(E-Mail Removed)> wrote:
> You should be using Convert class to do the type conversion as it uses
> implicit conversions. Where as type casting a value, called explicit or
> force conversion, there may be a data loss.


That's a massive overgeneralisation. If the type of value is already
numeric (or a boxed double) then a cast will do just as well as a call
to Convert (which can lead to exactly the same kind of data loss).
Convert is useful for parsing, or for using custom conversions.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit operator cast --> Specified cast is not valid Peter Microsoft C# .NET 1 24th Aug 2011 09:06 AM
cast in VB.Net =?Utf-8?B?QXJuZQ==?= Microsoft ASP .NET 2 2nd Aug 2007 07:14 AM
checking for valid cast before cast is called? =?Utf-8?B?d2dpbGxpbg==?= Microsoft Dot NET Framework 1 27th Jun 2005 10:29 PM
string-int-string generates exception with ConvertTo... js Microsoft C# .NET 1 4th Oct 2003 06:30 PM
Re: Cast Brian Camire Microsoft Access Queries 0 30th Sep 2003 05:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:49 AM.