Saturday, September 15, 2007

Some useful functions

In several my projects I am collecting my most useful methods in some class named "Functions.cs"

Yesterday I have added two functions which I think will be interesting to you.
The code is like this:
   1:          public static T GetQueryStringParam(string parameterName, T defaultValue)
   2:          {
   3:              HttpContext context = HttpContext.Current;
   4:              T result;
   5:              if (context.Request.QueryString[parameterName] != null)
   6:              {
   7:                  try
   8:                  {
   9:                      result = (T)typeof(T).GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { context.Request.QueryString[parameterName] });
  10:                  }
  11:                  catch
  12:                  {
  13:                      result = defaultValue;
  14:                  }
  15:              }
  16:              else
  17:              {
  18:                  result = defaultValue;
  19:              }
  20:              return result;
  21:          }
  22:          public static Guid GetQueryStringParamGuid(string parameterName, Guid defaultValue)
  23:          {
  24:              HttpContext context = HttpContext.Current;
  25:              Guid result;
  26:              if (context.Request.QueryString[parameterName] != null)
  27:              {
  28:                  try
  29:                  {
  30:                      result = new Guid(context.Request.QueryString[parameterName]);
  31:                  }
  32:                  catch
  33:                  {
  34:                      result = defaultValue;
  35:                  }
  36:              }
  37:              else
  38:              {
  39:                  result = defaultValue;
  40:              }
  41:              return result;
  42:          }
the first is generic function, which returns strongly typed querystring object if it exists, or default value if not, or casting can not be done. As you see casting is done using "Parse" method in T type through reflection. so if the type required doesnot contains the Parse method, default value will be returned.

The second method does same for Guid type.

I want to share some more useful functions from my Functions.cs class, these are:

   1:          public static string RenderControl(Control ctrl)
   2:          {
   3:              StringBuilder sb = new StringBuilder();
   4:              StringWriter tw = new StringWriter(sb);
   5:              HtmlTextWriter hw = new HtmlTextWriter(tw);
   6:              ctrl.RenderControl(hw);
   7:              return sb.ToString();
   8:          }
Renders control and returns string HTML representation of that control

   1:          public static T IntToEnum(int value)
   2:          {
   3:              T result = (T)Enum.ToObject(typeof(T), value);
   4:              return result;
   5:          }
returns enumeration value of type T, from integer value of that value

   1:          public static T StringToEnum(string value)
   2:          {
   3:              T result = (T)Enum.Parse(typeof(T), value);
   4:              return result;
   5:          }
returns enumeration value of type T, from string representation of that value

   1:          public static void SetDropDownListSelection(ListControl ddl, T value)
   2:          {
   3:              if (ddl.Items.Count > 0)
   4:              {
   5:                  ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value.ToString()));
   6:              }
   7:          }
sets dropdownlist ddl-s selected item to specified value. (This function actually should not be placed in BLL area of application, because it controls interface related task. Place it in UI namespace instead)

   1:          public static string RandomString
   2:          {
   3:              get
   4:              {
   5:                  return GetRandomString(8);
   6:              }
   7:          }
   8:          public static string GetRandomString(int length)
   9:          {
  10:              Guid g = Guid.NewGuid();
  11:              string res = g.ToString().Replace("-", "");
  12:              return res.Substring(0, length);
  13:          }
these two methods return short random strings (for test purposes for example, or passwords)