Casting to double in C#

  • Thread starter Thread starter kanepart2
  • Start date Start date
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.
 
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
 
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 -
 
What happens if you try

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

???

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