Handle enums gracefully...

by Mikael Henriksson 27. January 2009 18:59

As of lately I have been trying to use Enums wherever I can. In the project I just finished I had a bunch of buggy code and wanted more detailed descriptions so I began writing a grand class to handle all the exceptions. I decided to use and Enum for describing all the different logging routines but when I needed to actually do something based on more than one LogType I ran in to patrol. I wanted to iterate through the declared enum type for the class and log to all types in a sweep. First I got kind of confused looking at my code and since I wanted the others in the team to be able to use it easily I had to rethink. First thing was to refactor the class to something really simple, then begin adding my "normalized" methods but I got stuck at not being able to gracefully looping through my enum.

Let's say I have an enum like this:

[Flags()]   
public enum LogType   
{
	/// <summary>Exception will be logged to a text file</summary>  
	TextFile = 1,
	/// <summary>Exception will be logged to the application event log</summary>
	EventLog = 2,
	/// <summary>Exception will be logged via email</summary>
	Email = 4,
	/// <summary>Log to a website</summary>
	WebSite = 8,
	/// <summary>Log to a database of your choice</summary>
	Database = 16,
	/// <summary>A summary of all LogSources</summary
	All = TextFile | EventLog | Email | WebSite
}

The above code should be a no-brainer, if you don't understand it you should probably buy a book! :)

I found a great tool for making my work extremely readable for my college by the way, it's called Ghost Doc and if you have not tried it yet I urge you to! The project can be found over at http://www.roland-weigelt.de/ghostdoc/
If you are stronger in the code than my granny was I believe you should have no problems reading the following code either, and making good use of it!

public static Collection<T> ConvertEnumValuesToCollection<T>(T enumInputValue) 
where T : struct { Type enumType = typeof(T); if (!enumType.IsEnum) { throw new ArgumentException(enumType.ToString()
+ " Is not an Enum.", "T", null); } Array enumValues = Enum.GetValues(enumType); long inputValueLong = Convert.ToInt64(enumInputValue); Collection<T> enumOutputCollection = new Collection<T>(); foreach (T enumValue in enumValues) { long enumValueLong = Convert.ToInt64(enumValue); if ((enumValueLong&inputValueLong)==enumValueLong&&enumValueLong!=0) { enumOutputCollection.Add(enumValue); } } if (enumOutputCollection.Count == 0 && enumValues.GetLength(0) > 1) { T enumNoneValue = (T)enumValues.GetValue(0); if (Convert.ToInt64(enumNoneValue) == 0) { enumOutputCollection.Add(enumNoneValue); } } return enumOutputCollection; }

No credits at all to me for this one though, I stumbled upon it by mistake but since having computer crashes etc I cannot give my credits to the right webby unfortunately. Hugs and kisses for this though and definately worth sharing. If you have any questions what so ever about enums, flags or .net the myth the legend and the framework I recommend contacting Krzystof Cwalina. One of the chief architects over at Microsoft. If you just want to learn some more about enums I recommend you checking his extremely extensive do's and don'ts out over at http://blogs.msdn.com/kcwalina/archive/2004/05/18/134208.aspx

Tags:

C#

blog comments powered by Disqus