Posts
2
Comments
6
Trackbacks
0
Friday, October 31, 2008
Enum TryParse

 Well this would be my first post on geekswithblogs, and I'm going to start by introducing a couple of tips and tricks in terms of parsing and string manipulation.

 

The following is my first version of an Enum TryParse, which uses Generics to convert a string into an Enum using an out parameter, and returning a boolean verifying whether the string passed can be converted into the Enum or not.

public static bool EnumTryParse<theEnum>(string input, out theEnum returnValue)

{

      if (Enum.IsDefined(typeof(theEnum), input))

      {

            returnValue = (theEnum)Enum.Parse(typeof(theEnum), input, true);

            return true;

      }

 

      returnValue = default(theEnum);

      return false;

}

This is particularly useful for being used in if conditions. The way to use it is as follows:

 

MyEnum m;

 

if (EnumTryParse<MyEnum>("value", out m))
{
     
//do something
}

However, this particular version of my EnumTryParse does not allow for case sensitivity. What if "value" had been written as "Value" within the declaration of the Enum? The solution is to check whether the string equals to a value within the Enum defined, and converting the string input into the case which is required.

 

For this, we need two different string manipulation methods. Case Insensitive String Equals and Case Insensitive String Replacement, which are as follows:

 

public static bool CaseInsensitiveEquals(string input, string pattern)
{
      return input.Equals(pattern, StringComparison.CurrentCultureIgnoreCase);
}

public static string CaseInsensitiveReplace(string input, string pattern, string replacement)
{
      return Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase);
}


These two methods allow for some interesting string manipulation which can be used throughout the program, rather than just for the Enum Parser, however that is out of scope for this article. In future articles I shall go into greater depth regarding some string manipulation methods which I have developed.

With these two methods, we can iterate through the values with our enum and when we find a match using the CaseInsensitiveEquals, we can replace the string we inputted with the string which is within the Enum, thus formatting the input into the case which we want.

As such, the final version of the EnumTryParse is as follows:

public static bool EnumTryParse<theEnum>(string input, out theEnum returnValue)
{
     foreach (string s in Enum.GetNames(typeof(theEnum)))
     {
          if (CaseInsensitiveEquals(input, s))
          {
                input = CaseInsensitiveReplace(input, s, s);
                break;
          }
     }

     if (Enum.IsDefined(typeof(theEnum), input))

     {

           returnValue = (theEnum)Enum.Parse(typeof(theEnum), input, true);

           return true;

     }

 

     returnValue = default(theEnum);

     return false;

}

 

posted @ Friday, October 31, 2008 8:42 PM | Feedback (2)
News