C#•Validate Your Base Class

Listing 1. This class provides the basis for all validators. It is responsible for serializing the table and column names, as well as the error message. It contains the event handler for the ColumnChanged event, and it provides the logic to wire the event handlers to the DataSet at run time.

[Serializable ()]
public abstract class Validator
{
   protected string _tableName;
   protected string _columnName;

   protected string _errMessage;

   protected Validator (string t, string c, 
      string m)
   {
      _tableName = t;
      _columnName = c;
      _errMessage = m;
   }

   // Responsible for wiring events.
   public void ConnectTo (DataSet ds)
   {
      ds.Tables[_tableName].ColumnChanged +=new 
      DataColumnChangeEventHandler
         (Validator_ColumnChanged);
   }

   abstract public void Validate (DataSet ds, 
      DataRow r, DataColumn c, object newVal);

   private void Validator_ColumnChanged(object 
      sender, DataColumnChangeEventArgs e)
   {
      if (e.Column.ColumnName == _columnName)
         Validate (e.Row.Table.DataSet,
            e.Row, e.Column, 
            e.ProposedValue);
   }
}