Welcome to Manicprogrammer Sign in | Join | Help

Enums + Extension Methods = Sweet

Intro

Have you ever wanted to get an enum's description instead of it's ToString() representation? I know I did.

Just override ToString()? Enum's don't allow overrides. Just use some method that switches based on the enum value? Switches are evil! 

Enter extension methods. Do you know that you can extend an enum?

The following is perfectly legal code:

public static string DoSomethingWith(SomeEnum some){ //... }

From that I got the idea of extending Enum itself to add the following to the given enum:

public enum SomeEnum{

    [Description("Some Value")] 

    SomeValue = 1

  1. GetValue() - Returns the enum value as integer - i.e.: SomeEnum.SomeValue.GetValue(); - Returns 1
  2. GetName() - Returns the description for a given enum value (use DescriptionAttribute to assign a description to each value) - i.e.: SomeEnum.SomeValue.GetName(); - Returns "Some Value"
  3. GetValueAsString() - Returns GetValue().ToString() - i.e.: SomeEnum.SomeValue.GetValueAsString() - Returns "1"

Conclusion

I included the project that uses that as an attachment! I'll definitely be using that in the future. Happy coding!

Published Monday, November 17, 2008 10:06 AM by heynemann
Attachment(s): EnumDemo.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Enums + Extension Methods = Sweet

Monday, November 17, 2008 9:05 AM by Maethorin

Very Cool and usefull.

But, GetName to return Description?

Should not be GetDescription???

Cheers!

# re: Enums + Extension Methods = Sweet

Monday, November 17, 2008 9:12 AM by heynemann

Cool! Just use your resharper, F2, GetDescription and you're good :)

Cheers

Bernardo Heynemann

# re: Enums + Extension Methods = Sweet

Monday, November 17, 2008 1:02 PM by Jim

The project I am on right now uses this a lot, and IMHO it is NOT a good idea!  It would be much better to make the enum a proper class and give it a description property or whatever.

# re: Enums + Extension Methods = Sweet

Monday, November 17, 2008 3:25 PM by heynemann

Yeah the problem with that is that it does not create a very good domain model.

Enums are the supported way of the language of creating multi-valued properties. Not to mention persistence.

What are the problems you ran into exactly?

# re: Enums + Extension Methods = Sweet

Monday, November 17, 2008 6:34 PM by Jim

Chris Stevenson has written some good posts about the pitfalls of enums.  The main problems I run into are: performance (which is ironic given the supposedly lightweight nature of enums), lack of type safety and lack of encapsulation.  I will attempt to blog the worst offenses sometime.

I'm curious that you say [about my suggestion] "it does not create a very good domain model".  What do you mean by this?  What do you have against objects? :-)

# re: Enums + Extension Methods = Sweet

Tuesday, November 18, 2008 6:21 AM by heynemann

Nothing against objects at all.

Yes against objects that replace enums.

usually they are in the form of:

public class SomeClassThatShouldBeAnEnum{

 public const int SomeValue = 1;

 public string GetValueFor(int value){

   return "Some Value";

 }

}

Ok that is a very simplistic view of it. My problem with it is that you just added code that needs to be tested, in order to do what an enum would to just fine (it can be reduced with a base class, but then you introduced inheritance of enums :( ).

I still don't see the pitfalls you went through.


Enter the text you see in the image:

Leave a Comment

(required) 
required 
(required)