Tuesday, January 11, 2011

Input Parameters: One Better than Strings and Enums

The ASP.Net/C# project I'm working on has several methods that resemble this general pattern:
  public void UpdateDataTable(DataRow dataRow, object objFldName, object newValue)
    string fieldName = objFldName.ToString();
In times past the 2nd parameter would always be string fieldname but to increase data typing and reduce errors I started introducing Enums wherever possible.  In this particular example UpdateDataTable processes DataRows from several different DataTables.  As I have a different Enum for each DataTable's fieldnames and not one Enum for all DataTable fieldnames, a solitary Enum parameter type isn't possible.

I eventually realized that I could use the little trick of specifying a generic object data type and then convert each parameter value over to its string equivalent.  A simple trick, yes, but also an effective one!

No comments:

Post a Comment