Enum Parse Extension Methods
04 Nov 2008When I write code I don't like to depend on catching exceptions to control my programming logic, so as much as I can I try to check for nulls, empty, etc...
When parsing one type into another there are usually TryParse() methods available so that an exception does not occur. I've always been a little confused why there wasn't such a method off the Enum class. So, I decided to make my own.
However, after I started to extend the TryParse method off of the Enum, the answer started to become clear why that was not a feature :) I could extend the actual specific enum, but that didn't really help me any. I wanted a Generic solution for all enums.
So, instead of making an Enum.TryParse(), I decided to extend the string class to add string.ToEnum() and string.TryToEnum(). The string is usually the class that I want to parse into a specific Enum after all.
The following is the Extension methods that I created...
using System;
namespace Web.Helpers {
public static class EnumHelper {
public static bool TryToEnum(this string obj, out T parsed) {
bool isParsed = false;
if (Enum.IsDefined(typeof(T), obj)) {
parsed = (T)Enum.Parse(typeof(T), obj);
isParsed = true;
} else {
parsed = (T)Enum.Parse(typeof(T), Enum.GetNames(typeof(T))[0]);
}
return isParsed;
}
public static T ToEnum(this string obj) {
return (T)Enum.Parse(typeof(T), obj);
}
}
}
</pre>I created a set of 4 MS Unit tests to exercise different scenarios that might exist. Let me know if you see any other tests that I should test.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Web.Helpers;
namespace WebTests.Generic {
[TestClass]
public class EnumTest {
public enum Color { Red, Orange, Yellow, Green, Blue, Indigo, Violet };
[TestMethod]
public void TryToEnumWorksWithValidEnum() {
Color parsedColor = Color.Blue;
string realColor = "Yellow";
bool canParse = realColor.TryToEnum(out parsedColor);
Assert.AreEqual(true, canParse);
}
[TestMethod]
public void TryToEnumWorksWithInvalidEnum() {
Color parsedColor = Color.Blue;
string fakeColor = "Elijahish";
bool canParse = fakeColor.TryToEnum(out parsedColor);
Assert.AreEqual(false, canParse);
}
[TestMethod]
public void ToEnumWorksWithValidEnum() {
Color parsedColor = Color.Blue;
string realColor = "Yellow";
parsedColor = realColor.ToEnum();
Assert.AreEqual(Color.Yellow, parsedColor);
}
[TestMethod, ExpectedException(typeof(ArgumentException), "Unable to parse enum")]
public void ToEnumThrowsExceptionWithInalidEnum() {
Color parsedColor = Color.Blue;
string fakeColor = "Elijahish";
parsedColor = fakeColor.ToEnum();
}
}
}
</pre>Have you found yourself doing some sort of the same thing? If so, how?