473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Casting to double in C#

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.

Jun 8 '07 #1
14 31473
On Jun 8, 3:52 pm, kanepa...@hotmail.com wrote:
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.
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

Jun 8 '07 #2
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, 10:56 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Jun 8, 3:52 pm, kanepa...@hotmail.com wrote:


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.

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- Hide quoted text -

- Show quoted text -

Jun 8 '07 #3
What happens if you try

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

???

Cheers,
Johnny J.


<ka*******@hotmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
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.

Jun 8 '07 #4
yea that worked. thanks for the help guys

On Jun 8, 11:18 am, "Johnny Jörgensen" <j...@altcom.sewrote:
What happens if you try

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

???

Cheers,
Johnny J.

<kanepa...@hotmail.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
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.- Hide quoted text -

- Show quoted text -

Jun 8 '07 #5
Double.TryParse can be more useful...

Jun 8 '07 #6
On Jun 8, 8:18 pm, "Johnny Jörgensen" <j...@altcom.sewrote:
What happens if you try

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

???

Cheers,
Johnny J.

<kanepa...@hotmail.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
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.- Hide quoted text -

- 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.)
{
...........
...........
}

Jun 8 '07 #7
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.


<ka*******@hotmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
yea that worked. thanks for the help guys

On Jun 8, 11:18 am, "Johnny Jörgensen" <j...@altcom.sewrote:
What happens if you try

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

???

Cheers,
Johnny J.

<kanepa...@hotmail.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
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.- Hide quoted
text -

- Show quoted text -


Jun 8 '07 #8
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.sewrote:


What happens if you try
double x = Convert.ToDouble(myReader["TimeStamp"]);
???
Cheers,
Johnny J.
<kanepa...@hotmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
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.- Hide quoted text -
- 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.)
{
..........
..........

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
It seems Double.Parse accepts string parameters only.
Since this is an object, Convert.ToDouble is the apt one.

Jun 8 '07 #9
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.sewrote:


What happens if you try
double x = Convert.ToDouble(myReader["TimeStamp"]);
???
Cheers,
Johnny J.
<kanepa...@hotmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
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.- Hide quoted text -
- 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.)
{
..........
..........

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Or you can convert the value to string and then parse - better avoid
two conversions.

Jun 8 '07 #10
On Jun 8, 4:31 pm, "Johnny Jörgensen" <j...@altcom.sewrote:
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

Jun 8 '07 #11

"Aneesh Pulukkul[MCSD.Net]" <an******@gmail.comwrote in message
news:11********************@h2g2000hsg.googlegroup s.com...
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.sewrote:


What happens if you try
double x = Convert.ToDouble(myReader["TimeStamp"]);
???
Cheers,
Johnny J.
<kanepa...@hotmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
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.- Hide quoted
text -
- 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.)
{
..........
..........

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

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.
Jun 8 '07 #12
On Fri, 08 Jun 2007 08:13:17 -0700, <ka*******@hotmail.comwrote:
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.
Jun 8 '07 #13
On Jun 8, 10:31 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 08 Jun 2007 08:13:17 -0700, <kanepa...@hotmail.comwrote:
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.

Jun 8 '07 #14
double y = double.Parse((string)myReader["YAxis"]);

Jun 9 '07 #15

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

Similar topics

4
by: Jonathan Fielder | last post by:
Hi, My program (below) casts a double (which is in range for a float) to a float. As far as I know this should give me the nearest representable float, which will loose some precision. I...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
4
by: Sagaert Johan | last post by:
Hi I am struggling with pinvoke I have dll a function that is declared as : sf_command (IntPtr sndfile,int command, IntPtr data, int datasize); i need to pass a pointer to a double in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.