468,168 Members | 1,521 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 31101
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jonathan Fielder | last post: by
231 posts views Thread by Brian Blais | last post: by
6 posts views Thread by James Thurley | last post: by
17 posts views Thread by sophia.agnes | last post: by
32 posts views Thread by alex.j.k2 | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.