Solved

Casting woes from System.Single directly to System.Double

Posted on 2004-10-14
2
229 Views
Last Modified: 2010-04-15
I am reading from a SQL table whith a row "open" of type "float" (which C# / .NET reads as a System.Single value).

Here is the code:
1. Console.WriteLine("Value: {0}, data type={1}", myReader["open"], myReader["open"].GetType());
2. float f = (float)(myReader["open"]);
3. d = (double)f;
4. d = (double)(myReader["open"]);

1. prints: Value: 24.02, data type=System.Single
2. No problem (f=24.02)
3. No problem (d=24.02)
4. Exception - invalid cast ...

Why? How is statement 4 different than 2 & 3 combined? Does C# have trouble doing the conversion directly, and if so, what would be an elegant and robust way to do it?

Thanks,
Patrick
0
Comment
Question by:patrickq
  • 2
2 Comments
 
LVL 3

Expert Comment

by:aacool
Comment Utility
Please see my code below - this works fine - no exceptions are thrown.
   try
   {
         System.Single single = 5.3F;
         float f= (float)single;
         double d1 = (double)f;
         double d2 = (double)single; // no exception thrown.
   }
   catch(Exception ex)
   {
      MessageBox.Show(ex.Message+ex.StackTrace);
   }



Not sure what exactly is the issue without looking at the definition of myReader. Could you post the exception thrown?
0
 
LVL 3

Accepted Solution

by:
aacool earned 50 total points
Comment Utility
Did this work out for you?
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now