Ramblings of an Id10t C# Programmer

Random Posts about .net

VSX Developer Conference

clock September 15, 2008 10:43 by author Shawn C

So my first visit to the Seattle area is to the attend the VSX Dev Conference and thus far it has been a great time. Although the conference just started I really enjoyed the keynote as it had a lot of details about the upcoming versions of Visual Studio 10, 11, and 12. Though nothing is written in stone yet but the basic goals are impressive and will hopefully fix most of the annoyances I have with VS. To read more about please visit DiveDeeper recap posting VSX Keynote and VSX Futures.

Currently rated 1.5 by 67 people

  • Currently 1.492537/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


EnumParse Extension Method

clock July 21, 2008 10:13 by author Shawn C

 

Not the first one and most likely not the last EnumParse Methods

/// <summary>
/// Attempts to parse a string in to an enum value
/// </summary>
public static T EnumParse<T>(this string stringEnum)
{
    return stringEnum.EnumParse<T>(false);
} 

 

/// <summary>
/// Attempts to parse a string in to an enum value 
/// </summary>
/// <typeparam name="T">Enum to convert to</typeparam>
/// <param name="stringEnum">string to Parse</param>
/// <param name="ignoreCase">if the string is exect enum value true should be used else false</param>
/// <returns>enum of type T</returns>
public static T EnumParse<T>(this string stringEnum, bool ignoreCase)
{
    if (string.IsNullOrEmpty(stringEnum) )
    {
        throw new ArgumentNullException("stringEnum");
    }
 
    stringEnum = stringEnum.Trim();
    if (stringEnum.Length == 0)
    {
        throw new ArgumentException("Must specify valid information for parsing in the string.", "stringEnum");
    }
 
    Type t = typeof (T);
 
    if (!t.IsEnum)
    {
        throw new ArgumentException("Type provided must be an Enum.", "T");
    }
 
    T enumType = (T) Enum.Parse(t, stringEnum, ignoreCase);
    return enumType;
}

Currently rated 1.5 by 2 people

  • Currently 1.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


EnumToList Extension Method

clock July 21, 2008 09:57 by author Shawn C

 

While not the best solution I believe this is the only solution at this time due to the fact that enums are not instances of objects. Allows you to take an Enum and convert it to a Typed List which also gives you LINQabilty on it.

Usage:

   1: var list = StringComparison.CurrentCulture.EnumToList();
   2: foreach (var enumValue in list)
   3: {
   4: ...
   5: }

And the method:

   1: /// <summary>
   2: ///     Converts a enum to a list
   3: /// </summary>
   4: /// <typeparam name="T">string name of type of Enum</typeparam>
   5: /// <returns>An enumerable list of Enum Type "T"</returns>
   6: /// <remarks>
   7: ///     Usage:
   8: ///     var list = StringComparison.CurrentCulture.EnumToList();
   9: /// </remarks>
  10: public static IEnumerable<T> EnumToList<T>(this T enumName) where T : struct
  11: {
  12:     Type enumType = enumName.GetType();
  13:  
  14:     // Can’t use generic type constraints on value types,
  15:     // so have to do check like this
  16:     if (enumType.BaseType != typeof (Enum))
  17:         throw new ArgumentException(enumType + " is not an Enum.", "T", null);
  18:  
  19:     var enumValArray = Enum.GetValues(enumType);
  20:     var enumValList = new List<T>(enumValArray.Length);
  21:  
  22:     foreach (int val in enumValArray)
  23:     {
  24:         enumValList.Add((T) Enum.Parse(enumType, val.ToString()));
  25:     }
  26:  
  27:     return enumValList;
  28: }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Good Luck Hammett

clock July 21, 2008 08:49 by author Shawn C

So last week Hammett announced that he was joining Microsoft as a "Program Manager on the MEF team". All I got to say is good luck and thanks for being a great proponent of great technology. Though I don't comment much on this blog it is always one of the first rss I check in the morning. Considering the feedback and congrats on the news I believe that I am not the only one. Please continue to post your great posts and good luck in the new adventure.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Blog Re-start

clock July 16, 2008 05:41 by author Shawn C
So as i haven't posted in a while and i wanted to start up again i decided to clean out everything and start over. Not that it is a big deal as i have maybe 2 readers (1 being me Laughing). So i am going to try again.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


About the author

I like to believe that I am a halfway decent programmer that earns a living programming in .net. Graduated from R.I.T., and I have been programming for 10 years for various sized companies in the Rochester, NY area. To this day I still live in Rochester, NY but I have a great wife and 3 fat cats to make my days enjoyable.

View Shawn Carr's profile on LinkedIn

Tag cloud

Recent posts

Recent comments

None

Other Information

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Month List

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Most comments

Sign in