473,662 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting to double in C#

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

with this there are no compilation errors but I get a runtime
exception saying :-

System.InvalidC astException: specified cast is not valid.

Any clues on how to fix this will be much appreciated.

Jun 8 '07 #1
14 31496
On Jun 8, 3:52 pm, kanepa...@hotma il.com wrote:
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);

with this there are no compilation errors but I get a runtime
exception saying :-

System.InvalidC astException: 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)(deciml a)(myReader["TimeStamp"]);

Jon

Jun 8 '07 #2
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.com wrote:
On Jun 8, 3:52 pm, kanepa...@hotma il.com wrote:


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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);
with this there are no compilation errors but I get a runtime
exception saying :-
System.InvalidC astException: 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)(deciml a)(myReader["TimeStamp"]);

Jon- Hide quoted text -

- Show quoted text -

Jun 8 '07 #3
What happens if you try

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

???

Cheers,
Johnny J.


<ka*******@hotm ail.comwrote in message
news:11******** *************@k 79g2000hse.goog legroups.com...
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);

with this there are no compilation errors but I get a runtime
exception saying :-

System.InvalidC astException: specified cast is not valid.

Any clues on how to fix this will be much appreciated.

Jun 8 '07 #4
yea that worked. thanks for the help guys

On Jun 8, 11:18 am, "Johnny Jörgensen" <j...@altcom.se wrote:
What happens if you try

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

???

Cheers,
Johnny J.

<kanepa...@hotm ail.comwrote in message

news:11******** *************@k 79g2000hse.goog legroups.com...
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);
with this there are no compilation errors but I get a runtime
exception saying :-
System.InvalidC astException: specified cast is not valid.
Any clues on how to fix this will be much appreciated.- Hide quoted text -

- Show quoted text -

Jun 8 '07 #5
Double.TryParse can be more useful...

Jun 8 '07 #6
On Jun 8, 8:18 pm, "Johnny Jörgensen" <j...@altcom.se wrote:
What happens if you try

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

???

Cheers,
Johnny J.

<kanepa...@hotm ail.comwrote in message

news:11******** *************@k 79g2000hse.goog legroups.com...
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);
with this there are no compilation errors but I get a runtime
exception saying :-
System.InvalidC astException: 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(my Reader["TimeStamp"]);
}
catch(....)/Handle exception here. If the string contains data that
cannot be parsed to double it would throw exception.)
{
...........
...........
}

Jun 8 '07 #7
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.


<ka*******@hotm ail.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
yea that worked. thanks for the help guys

On Jun 8, 11:18 am, "Johnny Jörgensen" <j...@altcom.se wrote:
What happens if you try

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

???

Cheers,
Johnny J.

<kanepa...@hotm ail.comwrote in message

news:11******** *************@k 79g2000hse.goog legroups.com...
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);
with this there are no compilation errors but I get a runtime
exception saying :-
System.InvalidC astException: specified cast is not valid.
Any clues on how to fix this will be much appreciated.- Hide quoted
text -

- Show quoted text -


Jun 8 '07 #8
On Jun 8, 8:30 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail .com>
wrote:
On Jun 8, 8:18 pm, "Johnny Jörgensen" <j...@altcom.se wrote:


What happens if you try
double x = Convert.ToDoubl e(myReader["TimeStamp"]);
???
Cheers,
Johnny J.
<kanepa...@hotm ail.comwrote in message
news:11******** *************@k 79g2000hse.goog legroups.com...
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);
with this there are no compilation errors but I get a runtime
exception saying :-
System.InvalidC astException: 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(my Reader["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.ToDoubl e is the apt one.

Jun 8 '07 #9
On Jun 8, 8:30 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail .com>
wrote:
On Jun 8, 8:18 pm, "Johnny Jörgensen" <j...@altcom.se wrote:


What happens if you try
double x = Convert.ToDoubl e(myReader["TimeStamp"]);
???
Cheers,
Johnny J.
<kanepa...@hotm ail.comwrote in message
news:11******** *************@k 79g2000hse.goog legroups.com...
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)(myRead er["TimeStamp"]);
double y = (double)(myRead er["YAxis"]);
with this there are no compilation errors but I get a runtime
exception saying :-
System.InvalidC astException: 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(my Reader["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.

Jun 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13615
by: Jonathan Fielder | last post by:
Hi, My program (below) casts a double (which is in range for a float) to a float. As far as I know this should give me the nearest representable float, which will loose some precision. I then test for a NAN by comparing my float to itself (is this correct?), and I get different behaviour with different compilers. Have I done something that is undefined, or is this a compiler bug? Thanks,
231
23108
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
6
7264
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math methods deal with doubles and I'm having problems with casting them back to floats. For example: (double)0.1f = 0.10000000149011612 I don't need the accuracy of doubles, but the speed of floats would be benificial, but it seems I'm either...
17
2215
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
32
2373
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION" variable, which of the following lines are correct:
22
2761
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
4
7564
by: Sagaert Johan | last post by:
Hi I am struggling with pinvoke I have dll a function that is declared as : sf_command (IntPtr sndfile,int command, IntPtr data, int datasize); i need to pass a pointer to a double in the data parameter, i now did : IntPtr mem = Marshal.AllocHGlobal(sizeof(double));
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.