努力想出一个比较不同类型的数字对象的通用C#方法 [英] Struggling to come up with a generic C# method that compares different types of numeric objects
问题描述
如果我将.Value字段设置为小于.MinValue的值或其他值超过.MaxValue,然后我得到一个System.Exception与下面的消息抛出:
'Value'属性不能被设置为一个值超出'MinValue'和'MaxValue'属性的范围
UltraNumericEditor相关字段的签名如下:
公共对象MinValue {get;组; }
公共对象MaxValue {get;组; }
public object Value {get;组; }
这有可能通过我们的代码库发生数百次,所以不是检查MinValue和MaxValue对比每次我们试图设置的值 ,我想我会控制子类并将检查放在那里:
public class OurNumericEditor:Infragistics.Win.UltraWinEditors.UltraNumericEditor
{
public object Value
{
get
{
return base.Value;
}
set
{
//确保我们设置的不在最小或最大
之外//如果是,则将值设置为min或max,而不是
double min =(double)base.MinValue;
double max =(double)base.MaxValue;
double attempt =(double)value;
如果(尝试>最大)
base.Value = max;
else if(尝试< min)
base.Value = min;
else
base.Value = value;
}
}
}
显然,值的类型MinValue和MaxValue可以转换为双精度,但如果不可能,我会期待InvalidCastException。
现在我可能只是在这里有一个金发的时刻,但我认为应该有可能编写一种方法来利用泛型进行比较,但我正在努力想象可能的样子。
任何想法或输入?
感谢
Tom
您不能很好地使用泛型,因为在编译时您不知道数据类型。
只需使用转换
类以将任何类型的数值数据转换为 double
值:
double min = Convert.ToDouble(base.MinValue);
double max = Convert.ToDouble(base.MaxValue);
double attempted = Convert.ToDouble(value);
这也有利于它处理混合数据类型的情况,例如 MinValue
是一个 int
和值
是一个 double
。
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 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
.
这篇关于努力想出一个比较不同类型的数字对象的通用C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!