Click here to Skip to main content
13,455,909 members (46,957 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 1 Feb 2013

Enums on steroids

, 1 Feb 2013
Rate this:
Please Sign up or sign in to vote.
If enums' could be used OOP style.


This snippet of code allows me to use classes and treat them like enums (to a certain extent). Since it is based on a class type paradigm, this allows me to introduce other properties as well. 

Also, I'd like to mention that this is my first article written and published on the web, not much of a blogger, but I do like helping out other fellow coders.


A few months ago I came across this CP article a few months ago and found it quite interesting. So I basically took this code and ran with it.... Or so the expression goes.

Like to thank   for giving me a head start on this enum snippet idea; hope you find it rather useful. 

Using the code  

Think that using this code is pretty simple and straight  forward to use.  Since enum style is taking an OOP approach, we must first inherit from Enum<T>.

Like so:  

public class UserRole : Enum<UserRole>
    public static readonly UserRole
        Guest = new UserRole(0x02, "Guest"),
        User = new UserRole(0x04, "User"),
        Admin = new UserRole(0x0c, "Admin"),
        Super = new UserRole(0x1c, "Super");

    public UserRole(int key, string value) : base(key, value) { }

Then we can use something like so 

EnumCollection<UserRole> roles = UserRole.GetBaseOrKey(0x1c); // Retrieves a collection of User, Admin, Super
bool isguest = roles == UserRole.Guest;                       // Compare if roles is just Guest
bool isnotguest = roles > UserRole.Guest;                     // Compare if is an authenticated user 
bool isadmin = (roles & UserRole.Admin) == UserRole.Admin;    // Compare if user is admin    
roles = UserRole.Admin;                                       // Role is just admin
string str = roles.ToString(); 
str =  UserRole.Admin.Value; 
str = UserRole.Admin;  
int int32 = UserRole.Admin;

Pretty simple to use. 

Points of Interest

Not gonna lie. It was fun coding this. One thing I did not test for was performance, not really sure how well this is going to test out when this is added on a larger project. But for small ones, seems kind of fun. 

I have attached the source code and not the solution. Figure you can always just drop it into your project and run with it. 


2/1 - Original version released.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer tw telecom holdings
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionGreat Code.....Another alternative to implement an enum Pin
charles henington4-Feb-13 3:09
membercharles henington4-Feb-13 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180322.1 | Last Updated 1 Feb 2013
Article Copyright 2013 by onelopez
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid