Moving Beyond Enumerations
page 5 of 5
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35328/ 99

Summary

In many cases, enumerations are a good fit for the needs of a system.  However, if and when the concept being modeled by the enum gains any kind of behavior beyond its numeric constant and developer-friendly label, it may be time to consider moving from an enum to a class.  In this article, you've seen how you can easily make this transition with minimal changes in your code that already makes use of an enum, provided that you keep the same naming convention for your class and its named static instances.

Learn more about software development best practices in my Principles of Object Oriented Design course available from PluralSight.

You can follow me on my blog at SteveSmithBlog.com and on Twitter at twitter.com/ardalis.  You can download the (very small) sample code for this article.


View Entire Article

User Comments

Title: Good Catch   
Name: Steve Smith
Date: 2011-08-30 7:56:15 PM
Comment:
@Ian,
Good catch. Yes, those fields should be readonly, of course.

Thanks!
Steve
Title: public static roles should be readonly   
Name: Ian Mercer
Date: 2011-08-30 5:01:05 PM
Comment:
Totally agree that enums should be replaced with classes as soon as they stop being enums. One improvement to your class would be to make all the values readonly, e.g.

public static readonly Author ...






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-15 6:35:54 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search