Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C#

Enum Generitized

Rate me:
Please Sign up or sign in to vote.
3.28/5 (10 votes)
12 May 2007CPOL7 min read 35.2K   164   26  
Using Generics to make a different kind of enumeration: easy to comment, and supports many types.
using System;
using ThreeOaks.EnumGeneritized;

namespace Enums
{
	/// <summary>
	/// Generic Enumeration with date values A,B,C
	/// </summary>
	[Serializable()]
	public class TestGenericEnumClassABC : GenericEnumClass<DateTime>
	{
       
        /// <summary>
        /// Constructor
        /// </summary>
        public TestGenericEnumClassABC() : base(){}

        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericEnumClassABC(DateTime setValue) : base(setValue) { }

		/// <summary>
		/// A is January 22nd, 2005
		/// </summary>
		public static readonly DateTime A = new DateTime(2005,01,22);
		/// <summary>
		/// B is January 23nd, 2005
		/// </summary>
		public static readonly DateTime B = new DateTime(2005,01,23);
		/// <summary>
		/// C is January 24nd, 2005
		/// </summary>
		public static readonly DateTime C = new DateTime(2005,01,24);
	}

	/// <summary>
	/// Enumeration with values X,Y,Z
	/// </summary>
	[Serializable()]
    public class TestGenericEnumClassXYZ : GenericEnumClass<DateTime>
	{
		/// <summary>
		/// X is March 15th, 2005
		/// </summary>
		public static readonly DateTime X = new DateTime(1968,03,15);
		/// <summary>
		/// Y is April 16th, 2005
		/// </summary>
		public static readonly DateTime Y = new DateTime(1968,04,16);
		/// <summary>
		/// Z is May 17th, 2005
		/// </summary>
		public static readonly DateTime Z = new DateTime(1968,05,17);
	}

	/// <summary>
	/// Enumeration with a bad value, not a string.
	/// </summary>
	[Serializable()]
    public class TestGenericEnumClassBadType : GenericEnumClass<DateTime>
	{
		/// <summary>
		/// X is January 21nd, 2005
		/// </summary>
		public static readonly DateTime X = new DateTime(1970,01,21);
		/// <summary>
		/// Y is January 22nd, 2005
		/// </summary>
		public static readonly DateTime Y = new DateTime(1970,01,22);
		/// <summary>
		/// i is 1
		/// </summary>
		public static readonly int i = 1;
	}

	/// <summary>
	/// Enumeration with duplicate values
	/// </summary>
	[Serializable()]
    public class TestGenericEnumClassDuplicate : GenericEnumClass<DateTime>
	{
		/// <summary>
		/// X is March 15th, 2005
		/// </summary>
		public static readonly DateTime X = new DateTime(2002,03,15);
		/// <summary>
		/// Y is April 16th, 2005
		/// </summary>
		public static readonly DateTime Y = new DateTime(2002,04,16);
		/// <summary>
		/// Duplicate is April 16th, 2005
		/// </summary>
		public static readonly DateTime Duplicate = new DateTime(2002,04,16);
	}

    /// <summary>
    /// Generic Enumeration with string values A,B,C
    /// </summary>
    [Serializable()]
    public class TestGenericStringEnumClassABC : GenericEnumClass<System.String>
    {
        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericStringEnumClassABC() : base() { }
        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericStringEnumClassABC(string setValue) : base(setValue) { }

        /// <summary>
        /// A for A
        /// </summary>
        public static readonly string A = "A";
        /// <summary>
        /// B for B
        /// </summary>
        public static readonly string B = "B";
        /// <summary>
        /// C for C
        /// </summary>
        public static readonly string C = "C";
    }

    /// <summary>
    /// Generic Enumeration with string with a value
    /// </summary>
    [Serializable()]
    public class TestGenericStringEnumClassWithNull : GenericEnumClass<System.String>
    {
        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericStringEnumClassWithNull() : base() { }
        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericStringEnumClassWithNull(string setValue) : base(setValue) { }

        /// <summary>
        /// A for A
        /// </summary>
        public static readonly string A = "A";
        /// <summary>
        /// B for B
        /// </summary>
        public static readonly string B = "B";
        /// <summary>
        /// C for C
        /// </summary>
        public static readonly string C = "C";
        /// <summary>
        /// An declared string with no value
        /// </summary>
        public static readonly string DNull;
    }

    /// <summary>
    /// Generic Enumeration with string values A,B,C
    /// </summary>
    [Serializable()]
    public class TestGenericStringEnumClassInit : GenericEnumClass<System.String>
    {
        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericStringEnumClassInit() : base(){}

        /// <summary>
        /// Constructor that sets the value upon instantiation
        /// </summary>
        public TestGenericStringEnumClassInit(string setValue) : base(setValue)
        {
        }

        /// <summary>
        /// Static constructor to initialize read-only fields
        /// </summary>
        static TestGenericStringEnumClassInit()
        {
            A = "A";
            B = "B";
            C = "C";
        }
        
        /// <summary>
        /// A for A
        /// </summary>
        public static readonly string A;
        /// <summary>
        /// B for B
        /// </summary>
        public static readonly string B;
        /// <summary>
        /// C for C
        /// </summary>
        public static readonly string C;
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Team Leader
United States United States
A biography in this little spot...sure.
I've worked at GTE HawaiianTel. I've worked at Nuclear Plants. I've worked at Abbott Labs. I've consulted to Ameritech Cellular. I've consulted to Zurich North America. I've consulted to International Truck and Engine. Right now, I've consulted to Wachovia Securities to help with various projects. I've been to SHCDirect and now at Cision.

During this time, I've used all kinds of tools of the trade. Keeping it to the more familier tools, I've used VB3 to VB.NET, ASP to ASP/JAVASCRIPT/XML to ASP.NET. Currently, I'm developing with C# and ASP.NET. I built reports in Access, Excel, Crystal Reports, and Business Objects (including the Universes and ETLS). Been a DBA on SQL Server 4.2 to 2000 and a DBA for Oracle. I've built OLTP databases and DataMarts. Heck, I've even done Documentum. I've been lucky to have created software for the single user to thousands of concurrent users.

I consider myself fortunate to have met many different people and worked in many environments. It's through these experiences I've learned the most.

Comments and Discussions