I've resolved it the following decimal way:
[RangeValidator(typeof(decimal), "0.00", RangeBoundaryType.Inclusive, "1.00", RangeBoundaryType.Ignore)]
Question
Hello Stackoverflow fellas!
I'm working with Enterprise Library 5.0 and I'm trying to validate if a decimal value is from the specified range, but there's no obvious way to do it
e.g
[RangeValidator(0, RangeBoundaryType.Exclusive, 1, RangeBoundaryType.Ignore)]
works only for int, double & float.
I'm exactly looking for the equivalent of the Visual Basic's line of code:
<RangeValidator(GetType(Decimal), "0.00", RangeBoundaryType.Inclusive, "0.00", RangeBoundaryType.Ignore, MessageTemplate := "Value must be greater than 0.")>
I don't want to cast, because of the precision. Has anyone overcame this?
Solution
I've resolved it the following decimal way:
[RangeValidator(typeof(decimal), "0.00", RangeBoundaryType.Inclusive, "1.00", RangeBoundaryType.Ignore)]
OTHER TIPS
I'm not sure if this will work but try making your range values decimal:
[RangeValidator(0.0, RangeBoundaryType.Exclusive, 1.0, RangeBoundaryType.Ignore)]
If that doesn't work, try:
[RangeValidator(0.0m, RangeBoundaryType.Exclusive, 1.0m, RangeBoundaryType.Ignore)]
The RangeValudatorAttribute
class doesn't have a constructor that supports decimals directly - VB may indirectly cast the
You can use the constructor that takes double
values instead:
[RangeValidator(0.0, RangeBoundaryType.Exclusive, 1.0, RangeBoundaryType.Ignore)]
Since 0.0
and 1.0
can be represented exactly in binary you shouldn't have a precision issue when comparing to decimal
values.