Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Casting to double in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kanepart2@hotmail.com - 08 Jun 2007 15:52 GMT
Hi guys, I am having a problem with the following code snippet:-

double x = (myReader["TimeStamp"]);
double y = (myReader["YAxis"]);

Resulting in the follwing compilation error:

Cannot implicitly convert type 'object' to 'double'. An explicit
conversion exists (are you missing a cast?)

To fix this I modified the code to :-

double x = (double)(myReader["TimeStamp"]);
double y = (double)(myReader["YAxis"]);

with this there are no compilation errors but I get a runtime
exception saying :-

System.InvalidCastException: specified cast is not valid.

Any clues on how to fix this will be much appreciated.
Jon Skeet [C# MVP] - 08 Jun 2007 15:56 GMT
On Jun 8, 3:52 pm, kanepa...@hotmail.com wrote:
> Hi guys, I am having a problem with the following code snippet:-
>
[quoted text clipped - 17 lines]
>
> Any clues on how to fix this will be much appreciated.

Yes - the cast is trying to unbox. It's probably in a different
format, eg decimal.

Print out myReader["TimeStamp"].GetType() and put an appropriate cast
in before the double, eg:
double x = (double)(decimla)(myReader["TimeStamp"]);

Jon
kanepart2@hotmail.com - 08 Jun 2007 16:13 GMT
Well , I tried what you said and it turns out that the type is string
and it doesnt allow conversions from string to double.

> On Jun 8, 3:52 pm, kanepa...@hotmail.com wrote:
>
[quoted text clipped - 30 lines]
>
> - Show quoted text -
Peter Duniho - 08 Jun 2007 18:31 GMT
> Well , I tried what you said and it turns out that the type is string
> and it doesnt allow conversions from string to double.

If you know it's a string, then you can use double.Parse() to convert to  
double.
Aneesh Pulukkul[MCSD.Net] - 08 Jun 2007 19:06 GMT
On Jun 8, 10:31 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > Well , I tried what you said and it turns out that the type is string
> > and it doesnt allow conversions from string to double.
>
> If you know it's a string, then you can use double.Parse() to convert to  
> double.

Yeah now it's clear - difference between casting and converting.
That's why we don't have InvalidCastException for Convert.ToXXXX()
method. Thank you Jon for the clarification.
Karras - 09 Jun 2007 21:33 GMT
double y = double.Parse((string)myReader["YAxis"]);
Johnny Jörgensen - 08 Jun 2007 16:18 GMT
What happens if you try

double x = Convert.ToDouble(myReader["TimeStamp"]);

???

Cheers,
Johnny J.

> Hi guys, I am having a problem with the following code snippet:-
>
[quoted text clipped - 17 lines]
>
> Any clues on how to fix this will be much appreciated.
kanepart2@hotmail.com - 08 Jun 2007 16:21 GMT
yea that worked. thanks for the help guys

On Jun 8, 11:18 am, "Johnny J?rgensen" <j...@altcom.se> wrote:
> What happens if you try
>
[quoted text clipped - 32 lines]
>
> - Show quoted text -
cjard - 08 Jun 2007 16:30 GMT
Double.TryParse can be more useful...
Johnny Jörgensen - 08 Jun 2007 16:31 GMT
Great.

MS is recommending using direct casting (double) instead of Convert, but
often Convert will do the job where the direct casting fails (for some
reason I don't know)

Cheers,
Johnny J.

yea that worked. thanks for the help guys

On Jun 8, 11:18 am, "Johnny Jörgensen" <j...@altcom.se> wrote:
> What happens if you try
>
[quoted text clipped - 33 lines]
>
> - Show quoted text -
Jon Skeet [C# MVP] - 08 Jun 2007 16:50 GMT
On Jun 8, 4:31 pm, "Johnny J?rgensen" <j...@altcom.se> wrote:
> MS is recommending using direct casting (double) instead of Convert, but
> often Convert will do the job where the direct casting fails (for some
> reason I don't know)

The reason in this case is simple - you can't cast from a string to a
double, but you can convert from a string to a double.

Jon
Aneesh Pulukkul[MCSD.Net] - 08 Jun 2007 16:30 GMT
On Jun 8, 8:18 pm, "Johnny J?rgensen" <j...@altcom.se> wrote:
> What happens if you try
>
[quoted text clipped - 32 lines]
>
> - Show quoted text -

try
{
double x  = Double.Parse(myReader["TimeStamp"]);
}
catch(....)/Handle exception here. If the string contains data that
cannot be parsed to double it would throw exception.)
{
..........
..........
}
Aneesh Pulukkul[MCSD.Net] - 08 Jun 2007 16:37 GMT
On Jun 8, 8:30 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
> On Jun 8, 8:18 pm, "Johnny J?rgensen" <j...@altcom.se> wrote:
>
[quoted text clipped - 50 lines]
>
> - Show quoted text -

It seems Double.Parse accepts string parameters only.
Since this is an object, Convert.ToDouble is the apt one.
Aneesh Pulukkul[MCSD.Net] - 08 Jun 2007 16:38 GMT
On Jun 8, 8:30 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
> On Jun 8, 8:18 pm, "Johnny J?rgensen" <j...@altcom.se> wrote:
>
[quoted text clipped - 50 lines]
>
> - Show quoted text -

Or you can convert the value to string and then parse - better avoid
two conversions.
Ben Voigt [C++ MVP] - 08 Jun 2007 17:42 GMT
On Jun 8, 8:30 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
> On Jun 8, 8:18 pm, "Johnny Jörgensen" <j...@altcom.se> wrote:
>
[quoted text clipped - 54 lines]
>Or you can convert the value to string and then parse - better avoid
>two conversions.

Cast, not convert, the object to string.  There would be only one
conversion, and an explicit cast should be far more efficient than calling
Convert.ToDouble(object) and having a dynamic type check.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.