Re: Casting to double in C#
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Fri, 8 Jun 2007 11:42:44 -0500
"Aneesh Pulukkul[MCSD.Net]" <aneesh.p@xxxxxxxxx> wrote in message
news:1181317092.656190.3940@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jun 8, 8:30 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@xxxxxxxxx>
wrote:
On Jun 8, 8:18 pm, "Johnny Jörgensen" <j...@xxxxxxxxx> wrote:
What happens if you try
double x = Convert.ToDouble(myReader["TimeStamp"]);
???
Cheers,
Johnny J.
<kanepa...@xxxxxxxxxxx> wrote in message
news:1181314362.001548.67670@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.
.
- References:
- Casting to double in C#
- From: kanepart2
- Re: Casting to double in C#
- From: Johnny Jörgensen
- Re: Casting to double in C#
- From: Aneesh Pulukkul[MCSD.Net]
- Re: Casting to double in C#
- From: Aneesh Pulukkul[MCSD.Net]
- Casting to double in C#
- Prev by Date: Re: from asp.net to COM object
- Next by Date: Re: interfaces
- Previous by thread: Re: Casting to double in C#
- Next by thread: How does .NET property databinding work?
- Index(es):
Relevant Pages
|