Casting to double in C#

K

kanepart2

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.
 
J

Jon Skeet [C# MVP]

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
 
K

kanepart2

Well , I tried what you said and it turns out that the type is string
and it doesnt allow conversions from string to double.


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 -
 
J

Johnny Jörgensen

What happens if you try

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

???

Cheers,
Johnny J.
 
K

kanepart2

yea that worked. thanks for the help guys

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:-
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 -
 
A

Aneesh Pulukkul[MCSD.Net]

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

Johnny Jörgensen

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

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:-
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 -
 
A

Aneesh Pulukkul[MCSD.Net]

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:-
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.
 
A

Aneesh Pulukkul[MCSD.Net]

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:-
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.
 
J

Jon Skeet [C# MVP]

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
 
B

Ben Voigt [C++ MVP]

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:-
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.
 
P

Peter Duniho

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.
 
A

Aneesh Pulukkul[MCSD.Net]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top