Click here to Skip to main content
13,353,124 members (64,822 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 20 Jan 2008

A Class Based Enumeration Implementation

, 28 Jan 2013
Rate this:
Please Sign up or sign in to vote.
A classical implementation of the enumeration using class together with NHibernate user types


The ClassEnum library is a class-based implementation of enumeration for scenarios where language enum cannot do the job.

Why to Use ClassEnum

C# enum is very convenient, but it can only have one string name without any space and special character in it.

One simple example to show why we need a class implemented could be a credit card type. There are 4 common credit card types: Visa, Master, American Express and Discover Card. If you use C# enum to represent these types, you will have to use AmericanExpress and DiscoverCard as the enum name, which could be cumbersome when you try to display them.
A class based enumeration implementation is simple. It uses static readonly fields of a class as enumeration items, and declares the constructor to be private to make it safe. Here is an example of a class based enumeration for credit card types:

public sealed class CreditCardType {
    public static readonly CreditCardType AmericanExpress =
            new CreditCardType("American Express");
    public static readonly CreditCardType DiscoverCard =
            new CreditCardType("Discover Card");
    public static readonly CreditCardType Master = new CreditCardType("Master");
    public static readonly CreditCardType Visa = new CreditCardType("Visa");
    private readonly string name;
    private CreditCardType(string name) { = name;
    public override string ToString() {

        return name;

The power of this class based implementation is that you can add properties or even functions if needed. Something missing here is the Enum.Parse function which allows you to parse an enum type from a string. Also this class based enumeration needs to be able to be mapped in NHibernate. ClassEnum library offers a base class for creating such class based enumeration with some basic functions such as Parse and a NHiberante User type.

ClassEnum is easy to use. You create your own class based enumeration by inheriting the ClassEnumGeneric<T> abstract class. T here is the type of your class based enumeration.

public class CreditCardType : ClassEnumGeneric<CreditCardType>
    public readonly  static CreditCardType Visa = new CreditCardType("Visa");
    public readonly  static CreditCardType AmericanExpress = new CreditCardType("American Express");
    private CreditCardType(string name):base(name) {}


Now this CreditCardType automatically inherits the useful functions and properties from ClassEnumGeneric:

// static parse function
CreditCardType.Parse("Visa"); //returns CreditCardType.Visa
// static Items property that returns a collection that contains
// CredictCardType.Visa and CredictCardType.AmericanExpress
// and other CredictCardTypesif declared.
CreditCardType.Items; //returns a collection that contains

ClassEnum implementation can be more powerful as you can use inheritance and polymorphisms in the class-based implementation. Here is an example:

public class CreditCardType : ClassEnumGeneric<CreditCardType> {
      public readonly static CreditCardType Visa = new Visa();
      public readonly static CreditCardType AmericanExpress = new AmericanExpress();
      protected CreditCardType(string name) : base(name) {}

      public abstract bool ValidateCCNumber(string ccNumber);

      private class Visa : CreditCardType {

          public Visa() : base("Visa") {}
          public override bool ValidateCCNumber(string ccNumber){

            // some Visa validation

      private class AmericanExpress : CreditCardType {

          public AmericanExpress() : base("American Express") {}
          public override bool ValidateCCNumber(string ccNumber){

            // some AmericanExpress validation

Now comes another major reason why you want to use the ClassEnum base class. The ClassEnum assembly also offers a NHibernate usertype so that you can map your ClassEnum property in NHibernate just as you map the enum property. All you need to do is just to declare a NHibernate usertype by inheriting the generic ClassEnumUserType base class in the ClassEnum assembly.

public class CreditCardTypeUserType : ClassEnumUserType<CreditCardType>{}

Now you can map your property using the following Hbm mapping XML sample:

<property name="CCType" type="YourNameSpace.CreditCardTypeUserType,
   YourAssembly" column="CCTypeEnum"/>

How To Use

ClassEnum assembly is a part of the MindLib project. You can download the MindLib source package within which you will find the ClassEnum.sln in the /src folder. Also in the solution, you can find the unit test ClassEnum.Test project, where more sample code is available to show how to use this ClassEnum library.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Kailuo Wang
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
johannesnestler11-Dec-13 23:21
memberjohannesnestler11-Dec-13 23:21 
SuggestionIf your issue is only that it's hard to display proper strings for enums... Pin
Aron Weiler28-Jan-13 15:03
memberAron Weiler28-Jan-13 15:03 
GeneralRe: If your issue is only that it's hard to display proper strings for enums... Pin
PIEBALDconsult28-Jan-13 15:46
memberPIEBALDconsult28-Jan-13 15:46 
GeneralSomething else to do (Ennis Ray Lynch is right) Pin
SAKryukov28-Jan-08 10:48
memberSAKryukov28-Jan-08 10:48 
Did you think of the ridiculous deficiency of C/C++/C#-style approach to enum -- it does not enumerate!?
So, I totally agree with Ennis Ray Lynch, Jr.: all we need is enumerable equivalent of enum.

As to the Ui names of the enum fields (with blank spaces or not), the problem is that the DisplayPropertyAttrinute is not applicable to them -- another annoying Microsoft mistake. All you need to do is to create your own attribute to introduce more human-readable names and use it appropriately (not so easy if you want them in your PropertyGrid, yet possible, too). Please see other CodeProject contributions -- you will also find a good advice on how to localize them. Perhaps, it is not worth doing the enum class just for naming.

Thank you.

Sergey A Kryukov

GeneralRe: Something else to do (Ennis Ray Lynch is right) Pin
Kailuo Wang28-Jan-08 11:35
memberKailuo Wang28-Jan-08 11:35 
GeneralRe: Something else to do (Ennis Ray Lynch is right) Pin
PIEBALDconsult28-Jan-13 15:48
memberPIEBALDconsult28-Jan-13 15:48 
GeneralYou probably should use readonly Pin
Ennis Ray Lynch, Jr.23-Jan-08 15:02
memberEnnis Ray Lynch, Jr.23-Jan-08 15:02 
QuestionRe: You probably should use readonly ??? Pin
jwhurst28-Jan-08 11:36
memberjwhurst28-Jan-08 11:36 

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 | 2.8.180111.1 | Last Updated 28 Jan 2013
Article Copyright 2008 by Kailuo Wang
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid