Re: Casting to double in C#

Tech-Archive recommends: Fix windows errors by optimizing your registry




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


.



Relevant Pages

  • Data source controls and parameter type conversion
    ... Exception I got: ... Since String and Int32 are incompatible types, ... perform conversion from String to Int32 in the first place? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Conversion from string "0" to type Integer is not valid
    ... exception is thrown regardless of the context of the conversion, ... Explicitly converting the string as you ... indicated below will still throw the exception on my machine. ... Dim i as Integer ...
    (microsoft.public.dotnet.framework)
  • Re: about casting
    ... while I see X as string, the same as DirectCast ... cast for: cast but exclude any indirect conversion. ... what we've been talking about as a "direct cast" just to distinguish ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Intermittent "Specified cast is not valid" Exception
    ... > In the page I evaluate a Query String and then I get data form a database. ... > Every now and again somthing goes odd and I get this Exception: ... would get a "specified cast is not valid" if it was DbNull.Value! ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How do I convert from string to Type?
    ... > I want to run a conversion on a string to convert the string ... > there some way to do a generic cast or conversion on the type? ... > object MyFunc(Type T, String Str) ... returns only is of the type "object", you'll probably still need to cast it ...
    (microsoft.public.dotnet.languages.csharp)