Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 20th, 2010  |  syntax: C#  |  size: 0.69 KB  |  views: 358  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.                 /// <summary>
  2.                 /// Converts a string with the length of 1 to the supplied enum type
  3.                 /// </summary>
  4.                 /// <typeparam name="T">The enum type that you want to convert the string to</typeparam>
  5.                 /// <param name="string">A string, with the length of 1, that maps to a enum item on the enum type you're supplying</param>
  6.                 /// <returns>An enum of the supplied type</returns>
  7.                 public static T ToEnum<T>(string @string)
  8.                 {
  9.                         if (string.IsNullOrEmpty(@string))
  10.                         {
  11.                                 throw new ArgumentException("Argument null or empty");
  12.                         }
  13.                         if (@string.Length > 1)
  14.                         {
  15.                                 throw new ArgumentException("Argument length greater than one");
  16.                         }
  17.                         return (T)Enum.ToObject(typeof(T), @string[0]);
  18.                 }
clone this paste RAW Paste Data