I have an Infragistics UltraNumericEditor (version 5.3, quite old) control on a form.
If I set the .Value field to something less than .MinValue or something more than .MaxValue then I get a System.Exception thrown with the following message:
The ‘Value’ property cannot be set to a value that is outside the range determined by the ‘MinValue’ and ‘MaxValue’ properties
The signatures of the relevant fields on UltraNumericEditor are as follows:
public object MinValue { get; set; }
public object MaxValue { get; set; }
public object Value { get; set; }
This has potential to occur many hundreds of times through our codebase, so rather than check MinValue and MaxValue vs the value we’re trying to set every time, I thought I’d subclass the control and put the check there:
public class OurNumericEditor : Infragistics.Win.UltraWinEditors.UltraNumericEditor
{
public object Value
{
get
{
return base.Value;
}
set
{
// make sure what we're setting isn't outside the min or max
// if it is, set value to the min or max instead
double min = (double)base.MinValue;
double max = (double)base.MaxValue;
double attempted = (double)value;
if (attempted > max)
base.Value = max;
else if (attempted < min)
base.Value = min;
else
base.Value = value;
}
}
}
Clearly this works fine when the type of value, MinValue and MaxValue can be casted to doubles, but I would expect an InvalidCastException when that’s not possible.
Now I may just be having a blonde moment here but I think it should be possible to write a method that makes use of generics to do the comparison, but I’m struggling to visualise what that might look like.
Any ideas or input at all?
Thanks
Tom
You can’t make any good use of generics, as you don’t know the data type at compile time.
Just use the
Convertclass to convert any kind numeric data todoublevalues:This has the benefit that it also handles cases with mixed data types, like when
MinValueis anintandvalueis adouble.