Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, April 28, 2009 9:20 AM
How do you cast a DataSet object to an FLOAT?
float F1;
F1 = ds.Tables[0].Rows[i]["Forecast"];
-smc
All replies (16)
Tuesday, April 28, 2009 11:13 AM
F1 = (float)ds.Tables[0].Rows[i]["Forecast"];
Tuesday, April 28, 2009 2:21 PM
No, I have tried that and I receive this error: InvalidCastException was unhandled by user code.
-smc
Tuesday, April 28, 2009 5:40 PM
What is the underlying datatype?
Tuesday, April 28, 2009 5:53 PM
It is likely that the underlying datatype is some string like varchar(50) or similar.
In that case, try with float.Parse(ds.Tables[0].Rows[i]["Forecast"].ToString());
This will however require the value in the data source to use the same character as decimal point as the locale that is running the web application.
Another possibility is that the data source is in fact of sql data type DECIMAL or similar, but can contain null values.
In that case, I'd recommend:
F1 = (ds.Tables[0].Rows[i]["Forecast"] == DBNull.Value) ? float.MinValue : (float)ds.Tables[0].Rows[i]["Forecast"];
and then deal with the special case of F1 being MinValue later then displaying it.
Wednesday, April 29, 2009 10:13 AM
F1 = (float)ds.Tables[0].Rows[i]["Forecast"];
Mr. Wellens, the data starts out it SQL Sever with a type of 'FLOAT'. However, it must be typed as an object once bound to the dataset because I get this error without the cast when trying to move a dataset item into a float variable:
Cannot implicitly convert type 'object' to 'float'. An explicit conversion exists (are you missing a cast?)
And it will finally let me cast to a float, if I first cast it to a double. Why?
-smc
Wednesday, April 29, 2009 11:00 AM
And it will finally let me cast to a float, if I first cast it to a double. Why?
I'm not sure what you mean by that. Are you doing something like this?
double F1;
float F2;
F1 = (double)ds.Tables[0].Rows[i]["Forecast"];
F2 = (float)F1;
Thursday, April 30, 2009 11:11 AM
Mr. Wellens,
Yes, casting in two steps as you describe above, first double, then float worked.
This one-step, nested cast also worked.
float F2;
F2 = (float)Convert.ToDouble(ds.Tables[0].Rows[i]["Forecast"]);.
Why is it first requiring a CAST to a Double before it will CAST to a Float?
-smc
Thursday, April 30, 2009 11:36 AM
I don't know. What you discovered doesn't make sense. However, it is repeatable:
float MyFloat = 1.23f;
double MyDouble;
Object MyObject;
MyObject = MyFloat;
MyFloat = (float)MyObject; // two-step cast works
MyDouble = (double)MyFloat;
MyDouble = (double)MyObject; // throws exception
I'm not sure if this is a bug or not, I entered it here:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=436416
I am also moving this thread into the C# language forum.
Thursday, April 30, 2009 11:48 AM
Thanks for entering the bug for me. I am on VS2005.
-smc
Thursday, May 7, 2009 3:48 PM
This is not a bug - you're confusing the cast operator with the unboxing operator. They look the same, but they work very differently:
http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx
Thursday, May 7, 2009 4:26 PM
I got this email from the Visual Studio product team:
Greetings from Microsoft Connect!
This notification was generated for feedback item: Double cast is required to go from float (in an object) to double which you submitted at the Microsoft Connect site.
Thanks for reporting this issue. We are escalating this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.
Thank you,
Visual Studio Product Team
I'll tag your email onto the report and they can decide what to do.
Thanks for posting.
Thursday, May 7, 2009 9:09 PM
I wouldn't expect that this is something that canbe 'fixed'. The first cast (from Object to float) is casting something that actually is a float (it has the bit pattern of a legal float). The cast from Object is just unboxing.
The second cast (from float to double) would need to change the underlying set of bits from those which represent a float to those which represent a double.
The third cast (from Object to double) is casting something that is actually a float. When the unboxing is attempted the bit pattern is not that of a double and hence the exception.
The c# standard says (4.3.2)
For an unboxing conversion to a given non-nullable-value-type to succeed at run-time, the value of the source operand must be a reference to a boxed value of that non-nullable-value-type. If the source operand is null, a System.NullReferenceException is thrown. If the source operand is a reference to an incompatible object, a System.InvalidCastException is thrown.
<edit>
Just noticed that the bug report has been closed with 'as by design'
</edit>
Friday, May 8, 2009 9:17 AM
It's not surprising it's a bit confusing, everything else in C# works auto-magically well. Expecting a simple cast to work isn't unreasonable.
The C# standard isn't quite right:
If the source operand is a reference to an incompatible object, a System.InvalidCastException is thrown.
A float is a compatible object to cast to a double.
Maybe the error message could be corrected to say "Unbox to incompatible type".
Wednesday, August 5, 2009 4:54 PM
Why did they close the MS Connect issue?
-smc
Wednesday, August 5, 2009 5:42 PM
Why did they close the MS Connect issue?
Because it's not a bug:
http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx
Thursday, August 6, 2009 2:09 AM
I beleive this is not a bug. as if 1st time if you box it in float, next you can not unbox that to double or vice versa.
Soumen