Wimdows.NET

Wim's .NET blog

Generic Parse method on Enum - a solution

David Findley writes about how he wishes we had a generic Parse method on the Enum class in .NET 2.0.

Though I agree in principle, it's actually quite trivial to create a generic static class with a Parse method, which alleviates some of the pain.

Here's my stab at it:

    public static class EnumUtil<T>
    {
        public static T Parse(string s)
        {
            return (T)Enum.Parse(typeof(T), s);
        }
    }


Say we have the following enum:

    public enum Color
    {
        Black,
        White,
        Blue,
        Red,
        Green
    }

We can now simply use the generic EnumUtil class as follows:

Color c = EnumUtil<Color>.Parse("Black");

I feel that this helper method doesn't actually warrant a class in its own right, so you may want to add it to one of your Util classes (if you happen to have a generic one!) instead.

Posted: Jul 06 2006, 09:59 AM by Wim | with 18 comment(s)
Filed under: ,

Comments

Jens said:

Am I wrong or is it a mistake?:

Color c = EnumUtil<Number>.Parse("Black");

should be

Color c = EnumUtil<Color >.Parse("Black");

or?

# July 6, 2006 5:38 AM

Wim said:

Ofcourse Jens - that's because I started out with an enum called Number, but renamed it in-situ to an enum called Color (as that seemed a better example for an enum) while I was editing my post. Fixed now though.

# July 6, 2006 6:28 AM

DotNetKicks.com said:

Trackback from DotNetKicks.com

# July 6, 2006 7:47 AM

Jouni Heikniemi said:

It would probably make more sense to use the type parameter at the method level (i.e. public static T Parse<T>(string s)) instead of the class level.

Having a type-parameterized utility class may be useful in some particular context, but unless it's instantiated for some purposes (such as to wrap or manage the instances of the original class/enum), I'd vote for keeping the type parameterization at the static method level.

# July 6, 2006 7:58 AM

David Truxall said:

I posted a TryParse style function for enums on my blog at http://dotnetjunkies.com/WebLog/davetrux/archive/2005/12/07/134190.aspx

# July 6, 2006 9:36 AM

findleyd said:

I'm actually doing this at the moment. Thats how I came up with the idea of Hey, why don't they just do this on the Enum class so millions of developers won't have to go write util code. :)

Personally I think MS was very conservative with the use of generics in the 2.0 fwk. Once we get to WinFX errr... .NET FWK 3.0 I think the use of gerics will explode. I just hope they remember to give the enum class some love. :P

# July 6, 2006 9:38 AM

Wim said:

Jouni - you're right, unless you got a good reason for having a generic util class, it makes more sense just having a static generic method.

David - nice one, your generic TryParse() function. Not sure whether it'd be better to raise an exception or return a boolean if teh value cannot be parsed but hey...

Dave - yep, it would make a lot of sense, and in many cases they have not really looked at existing classes in the framework and said:"Now, where would it be useful to add generics into the mix...", even if they'd limited that to the System namespace...

# July 6, 2006 10:55 AM

Generic Parse method on Enum - a solution said:

Pingback from  Generic Parse method on Enum - a solution

# November 26, 2007 2:24 PM

greyson said:

public static class GenericEnum<T>

   where T : struct, IComparable, IFormattable, IConvertible

 {

   public static bool TryParse(String value)

   {

     bool parsable = false;

     if (Enum.IsDefined(typeof(T), value))

     {

       parsable = true;

     }

     return parsable;

   }

   public static T Parse(String value, bool ignoreCase)

   {

     return (T)Enum.Parse(typeof(T), value, ignoreCase);

   }

   public static T Parse(String value)

   {

     return Parse(value, false);

   }

 }

# November 15, 2008 10:50 AM

Developer Tales n°6 - Generic Enum parsing « Xmlguy Weblog said:

Pingback from  Developer Tales n&deg;6 - Generic Enum parsing &laquo; Xmlguy Weblog

# December 5, 2008 10:37 AM

nick_rodome said:

# May 16, 2009 8:54 AM

poori said:

I hear that term "jail broken" all the time when talking to friends but i never understood what it meant. I want to say that if you buy a jail broken iphone you don't have to pay for the data usage plan. Is that true? What exactly does it mean jail broken and what is its functions? Thanks.

________________

<a href="www.youtube.com/watch iphone 3gs</a>

# October 14, 2009 5:28 AM

poori said:

I just got an iphone 3g and I got so excited that i filled it with loads of applications and songs (about $30 worth). The other day when i plugged my <a href="unlockiphone22.com/.../a>  into the computer on itunes it said to reset the iphone to factory settings. I dont want to loose all my apps so what can i do? PLEASE REPLY A.S.A.P.

# November 15, 2009 7:10 AM

Developer Tales n??5- Generic Enum parsing said:

Pingback from  Developer Tales n??5- Generic Enum parsing

# March 7, 2011 6:07 AM

icons archive said:

<a href=fredflatfour.skynetblogs.be/.../index.php Let's talk on this theme.</a>

# November 2, 2012 8:19 AM

icon pack said:

<a href=newsgenocide.com/.../viewtopic.php You have hit the mark. It seems to me it is very good thought. Completely with you I will agree.</a>

# November 4, 2012 10:22 AM

lvoicons.com said:

By WebOsPublisher

404 Not Found

Not Found

The requested URL /search/ was not found on this server.

Apache Server at iconfinder.comiconfinder.com Port 80

# December 8, 2012 11:27 PM

download icons said:

P.S. Please review our <a href="http://icons.wp8design.org">design portfolio</a> for Doors2012.

Website Branding Through Graphic Design

A great web design is not just about using attractive colours and putting together the navigation menus. Rather, it is about branding the website in a way that it attracts the targeted audience. While graphic design is all about visual appeal, it should also put across a message to the visitors. By incorporating the principles of graphic design in your web design, you have to form a unique identity that your visitors and customers can relate to and recognise easily.Many web designers often make the mistake of overusing or misusing graphic images on the wbesite. Too much of graphic design makes the website look cluttered and too little or dull graphic images can make the site look bland. The trick is to use graphic design throughout the website in measured proportions. Some of the areas where graphic images and design can be used on a website include corporate logos, navigation buttons, mastheads and footers, image maps, bullet points, background images, photos and image maps. All these areas on the website have to be designed in a way that it leaves a lasting impression on the minds of the visitors.Some of the graphic design elements that must be added to a web design are discussed below:Website colours: The purpose of investing in graphic design services while building a website is to gain a competitive edge and attract more visitors. Colours play a huge role in catching the attention, and during web design, it should be given high priority. The graphic designer should choose colours that complement the industry that you are targeting. For example, if your website is about women's apparel, the colours should be selected from the palettes of pinks, reds and purples that appeal to the female sensibility. Thus, it is important to choose right colours and blend them with the theme of the website.Typography: The fonts used throughout the web design have to be addressed by a graphic designer. The different fonts used in the website should not ohly complement each other but also the content on each of the web pages. The typography has to make the content appealing and readable. It is advisable that you choose familiar fonts that add a unifying element to the web design.Social media integration: One needs to blend the links to the social media profiles of a company with its website. Just by placing social media icons on your website does not bring effective results. To give your site a distinct edge, you can blend the social media icons with the colours and theme of the website by customising them.When you incorporate these elements of graphic design to your web design, the site is sure to rise above the rest and get a distinctive identity on the Internet. Through website branding, you stand a chance of getting higher visitors, enhancing the conversion rate, leads, sales and revednue. In Sydney, you can find several web designers and graphic designers who have an expertise in both the domains and can help in building a website that is not only visually appealing but also enhances the user experience.

# December 14, 2012 12:46 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)