Click here to Skip to main content
11,412,516 members (74,770 online)
Click here to Skip to main content

Tagged as

Enums on steroids

, 1 Feb 2013 CPOL
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 (Junior)
United States United States
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 1 Feb 2013
Article Copyright 2013 by jlopez788
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid