When I set the "type" of a numeric editor to decimal, start the application, dont change anything (ie accept the default 0 ("zero"))
and then call:
(decimal) numericEditor.Value
I get an exception. Why is that? When I manually type in 0, the Value parameter is castable to decimal. The default, unchanged zero seems to be an integer....
Am I doing something wrong?
What's the error?
My best guess is that the Value is null at that point, and the control simply displays a 0 for null. The solution would be to simply check for null before trying to cast the Value to a decimal.
It looks like a bug to me. When I set the value to 0 in the property editor on the form designer, the type of value seems to be a boxed (int), when I set it to 0 programmatically or via user input it is a boxed decimal (as expected).
The exception is an InvalidCast.
Hi,
I tried this out and I get the same results at design-time and run-time. If I set the Value to 0 at design-time and try this line of code, I get an error:
decimal d = (decimal)this.ultraNumericEditor1.Value;
If I try this at run-time, I get the same error:
this.ultraNumericEditor1.Value = 0;decimal d = (decimal)this.ultraNumericEditor1.Value;
This works fine:
this.ultraNumericEditor1.Value = 0M;decimal d = (decimal)this.ultraNumericEditor1.Value;
If I set the Value at design-time to anything other than 0, like 1, it works fine, also. So it looks like the default value of the Value property is a boxed 0 integer and that's why it's failing. I suppose the control might be able to vary the default Value based on the NumericType, so perhaps you are right and this is a bug.
I'll forward this over to Infragistics Developer Support so they can check it out. In the mean time, it's very easy to work around, either by setting the Value at run-time in the Form_Load or by using decimal.Parse instead of a direct cast:
decimal d = decimal.Parse(this.ultraNumericEditor1.Value.ToString());