Struggling to come up with a generic C# method that compares different types of numeric objects

Deadly 提交于 2019-12-11 12:52:03

问题


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


回答1:


You can't make any good use of generics, as you don't know the data type at compile time.

Just use the Convert class to convert any kind numeric data to double values:

double min = Convert.ToDouble(base.MinValue);
double max = Convert.ToDouble(base.MaxValue);
double attempted = Convert.ToDouble(value);

This has the benefit that it also handles cases with mixed data types, like when MinValue is an int and value is a double.




回答2:


You could simply cast everything as IComparable and use that interface to do the comparison.




回答3:


Generics will not help you here, as the controls Value property is not generic either. But you could go with the IConvertible interface to convert the number to what you need for the comparison (double for instance).




回答4:


This is probably overkill here, and IComparable would probably do the trick in your situation.

But if you really want to strongly type the values accepted by your control, you should probably write something like this :

public OurNumericEditor<T> : Infragistics.Win.UltraWinEditors.UltraNumericEditor  

 public T Value  
    {
        get
        {
            return (T) 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)MinValue;
            double max = (double)MaxValue;
            double attempted = // explicit conversion code goes here

            if (attempted > max)
                base.Value = max;
            else if (attempted < min)
                base.Value = min;
            else
                base.Value = value;
        }
    }
}


来源:https://stackoverflow.com/questions/802024/struggling-to-come-up-with-a-generic-c-sharp-method-that-compares-different-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!