C#•Validate on the Range

Listing 2. This class implements the range validator. Its Validate method compares the new value for the data to the minimum and maximum defined in the rule. In addition, this concrete class is responsible for serializing the minimum and maximum values.

[Serializable ()]
public class RangeValidator : Validator
{
   private object _minValue;
   private object _maxValue;

   public RangeValidator (string t, string c, 
      string m,
      object min,
      object max) :
      base (t, c, m)
   {
      _minValue = min;
      _maxValue = max;
   }

   public override void Validate (DataSet ds, 
      DataRow r, DataColumn c, object newVal)
   {
      IComparable curr = newVal as IComparable;
      // Too Small,
      if ((curr.CompareTo (_minValue) < 0)
         // too large
         || (curr.CompareTo (_maxValue) > 0))      {
         r.SetColumnError (c, _errMessage);
         r.RowError = _errMessage;
      } 
      else
      {
         r.SetColumnError (c, "");
         r.RowError = "";
      }
   }
}