在C#中转换为double [英] Casting to double in C#

查看:502
本文介绍了在C#中转换为double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我遇到以下代码片段的问题: -


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

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


导致以下编译错误:


无法隐式转换类型' '对象''到''加倍''。一个明确的

转换存在(你错过了演员吗?)

为了解决这个问题,我将代码修改为: -


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

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


用这个没有编译错误,但是我得到一个运行时

异常说: -


System.InvalidCastException:指定的强制转换是无效的。 />

非常感谢任何有关如何解决这个问题的线索。

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.

推荐答案

6月8日,3:52 pm,kanepa ... @ hotmail.com写道:
On Jun 8, 3:52 pm, kanepa...@hotmail.com wrote:

大家好,我遇到以下代码片段的问题: -


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

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

导致以下编译错误:


无法将类型''object'隐式转换为''double' 。一个明确的

转换存在(你错过了一个演员?)


为了解决这个问题,我将代码修改为: -


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

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


这个没有编译错误但我得到一个运行时

异常说: -


System.InvalidCastException:指定的强制转换不是有效。


任何有关如何解决此问题的线索将非常感激。
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.



是 - 演员试图取消装箱。它可能是一个不同的

格式,例如十进制。


打印myReader [" TimeStamp"]。GetType()并放一个合适的在双倍之前投入

,例如:

double x =(double)(decimla)(myReader [" TimeStamp"]);

Jon

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


好吧,我试过你所说的,结果证明类型是字符串

和它不允许从字符串转换为双倍。

6月8日上午10点56分,Jon Skeet [C#MVP] < s ... @ pobox.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.
On Jun 8, 10:56 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:

6月8日下午3:52,kanepa ... @ hotmail.com写道:


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"]);
double x = (myReader["TimeStamp"]);
double y = (myReader["YAxis"]);


导致以下编译错误:
Resulting in the follwing compilation error:


无法隐式转换输入''object''到''double''。存在明确的

转换(您是否错过了演员表?)
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"]);
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:指定的强制转换无效。
System.InvalidCastException: specified cast is not valid.


任何有关如何修复此问题的线索将非常感激。
Any clues on how to fix this will be much appreciated.



是 - 演员试图取消装箱。它可能是一个不同的

格式,例如十进制。


打印myReader [" TimeStamp"]。GetType()并放一个合适的在双倍之前投入

,例如:

double x =(double)(decimla)(myReader [" TimeStamp"]);

Jon-隐藏引用文字 -


- 显示引用文字 -


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 -



如果你尝试会发生什么


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


???


干杯,

Johnny J.


< ka ******* @ hotmail.comwrote in message

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

大家好,我遇到以下代码片段的问题: -


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

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


导致后续编译错误:


无法将类型''object'隐式转换为''double''。存在明确的

转换(您是否错过了演员?)


为了解决这个问题,我将代码修改为: -


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

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


这个没有编译错误但我得到一个运行时

异常说: -


System.InvalidCastException:指定的强制转换不是有效。


非常感谢任何有关如何解决这个问题的线索。
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.



这篇关于在C#中转换为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆