Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Enumerations » Downloads
 
Add your own
alternative version

Enum Generitized

, 12 May 2007
Using Generics to make a different kind of enumeration: easy to comment, and supports many types.
using System;
using System.Data;
using Adapdev.UnitTest;
using Enums;

namespace TestEnum
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[TestFixture]
	public class TestDateTimeEnumClass
	{

		/// <summary>
		/// Test multiple assignmesnt to same ABC
		/// </summary>
		[Test(Category="Assign value",Description="Test multiple assignments to same ABC.")]
		public void AssignMultipleValuesToABC()
		{
			TestDateTimeEnumClassABC a1 = new TestDateTimeEnumClassABC();
			
			a1.Value = TestDateTimeEnumClassABC.A;
			
			Console.WriteLine("a1.value = " + a1.Value.ToString("G"));
			a1.Value = TestDateTimeEnumClassABC.B;
			Console.WriteLine("a1.value = " + a1.Value.ToString("G"));
			Assert.IsTrue(a1.Value == TestDateTimeEnumClassABC.B,"Values should be B's" );
			
			
		}
		/// <summary>
		/// Test multiple ABC
		/// </summary>
		[Test(Category="Assign value",Description="Create two ABC's, assign different values.")]
		public void AssignMultipleABC()
		{
			TestDateTimeEnumClassABC a1 = new TestDateTimeEnumClassABC();
			TestDateTimeEnumClassABC a2 = new TestDateTimeEnumClassABC();
			a1.Value = TestDateTimeEnumClassABC.A;
			a2.Value = TestDateTimeEnumClassABC.B;
			Console.WriteLine("a1.value = " + a1.Value.ToString("G"));
			Console.WriteLine("a2.value = " + a2.Value.ToString("G"));
			Assert.IsTrue(a1.Value != a2.Value,"a1 values should differ from a2's" );
			
			
		}

		/// <summary>
		/// Declare abc and xyz TestDateTimeEnums.
		/// Makes sure can do different enums from abstrct DateTimeEnum.
		/// Confirms return correct value.
		/// </summary>
		[Test(Category="Assign value",Description="Sets ABC and XYZ, makes sure values are different")]
		public void SetABCAndXYZValues()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = TestDateTimeEnumClassABC.A;
			Assert.IsTrue(abc.Value == TestDateTimeEnumClassABC.A,"The value should equal A");
			TestDateTimeEnumClassXYZ xyz = new TestDateTimeEnumClassXYZ();
			xyz.Value = TestDateTimeEnumClassXYZ.X;
			Assert.IsTrue(abc.Value == TestDateTimeEnumClassABC.A,"The value should equal A");
			Assert.IsTrue(xyz.Value == TestDateTimeEnumClassXYZ.X,"The value should equal X");
			Assert.IsTrue(abc.Value!=xyz.Value,"The values should be different");
		}

		
		
		/// <summary>
		/// Makes sure receive exception when set to bad value.
		/// </summary>
		[Test(Category="Assign value",Description="Assigning a vad value to ABC, expect out of range exception")]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void AssignABCaBadValue()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = DateTime.Now ;
			Assert.IsTrue(abc.Value == DateTime.Now,"The value should equal now");
			
		}


		/// <summary>
		/// Makes sure receive exception when have bad constant.
		/// </summary>
		[Test(Category="Improper Declaration",Description="Developer created an value with the wrong type.")]
		[ExpectedException(typeof(ArrayTypeMismatchException))]
		public void CreateEnumWithBadConstantTypeDeclaration()
		{
			TestDateTimeEnumClassBadType sEnum = new TestDateTimeEnumClassBadType();
			sEnum.Value = DateTime.Now;
			Assert.IsTrue(sEnum.Value == DateTime.Now,"The value should equal now");
			
		}

		/// <summary>
		/// Makes sure receive exception when have two constants the same.
		/// </summary>
		[Test(Category="Improper Declaration",Description="Developer created two values that are the equal.")]
		[ExpectedException(typeof(NotSupportedException))]
		public void CreateEnumWithDuplicateDeclaration()
		{
			TestDateTimeEnumClassDuplicate sEnum = new TestDateTimeEnumClassDuplicate();
			sEnum.Value = DateTime.Now;
			Assert.IsTrue(sEnum.Value == DateTime.Now,"The value should equal now");
			
		}
		

		/// <summary>
		/// Get array of values from enumeration
		/// </summary>
		[Test(Category="Get",Description="Get array of values.")]
		public void GetValuesForABC()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = TestDateTimeEnumClassABC.A ;
			DateTime[] values = abc.GetValues();
			for (int i = 0;i<values.Length;i++)
			{
				Console.WriteLine("Value at position " + i.ToString() + " is " + values[i].ToString("G"));
			}
			
		}

		
		/// <summary>
		/// Get array of constant names from enumeration
		/// </summary>
		[Test(Category="Get",Description="Get array of Names.")]
		public void GetNamesForABC()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = TestDateTimeEnumClassABC.A ;
			string[] names = abc.GetNames();
			for (int i = 0;i<names.Length;i++)
			{
				Console.WriteLine("Name at position " + i.ToString() + " is " + names[i]);
			}
			
		}

		/// <summary>
		/// Get two dimensional array 
		/// </summary>
		[Test(Category="Get",Description="Get name and values in array.")]
		public void GetNamedValuesFromClassABC()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = TestDateTimeEnumClassABC.C ;
			string[,] namedValues = abc.GetNamedValues();
			for (int i = 0;i<namedValues.GetLength(1);i++)
			{
				Console.WriteLine("Name at position " + i.ToString() + " is " + namedValues[i,0]
							+ " value at position is " + namedValues[i,1]);
			}
			
		}

		/// <summary>
		/// Get DataTable of Name and Values
		/// </summary>
		[Test(Category="Get",Description="Get Name and Values in DataTable.")]
		public void GetNamedValuesDataTableFromClassABC()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = TestDateTimeEnumClassABC.A ;
			DataTable dt = abc.GetNamedValuesTable();
			foreach (DataRow dr in dt.Rows)
			{
				Console.WriteLine(dr["Name"].ToString() + " has value " + dr["Value"].ToString());
			}
			
		}

		/// <summary>
		/// Check if Name returns value's name
		/// </summary>
		[Test(Category="Get",Description="Get Name for assigned Value.")]
		public void GetNamedOfAssignedValueFromClassABC()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			abc.Value = TestDateTimeEnumClassABC.A ;
			Console.WriteLine("Name of value " + TestDateTimeEnumClassABC.A.ToString("G") + " = " + abc.Name);
			Assert.AreEqual(abc.Name,"A");
			
			
		}

		/// <summary>
		/// Check if Name of passed in value
		/// </summary>
		[Test(Category="Get",Description="Get Name for a value.")]
		public void GetNamedOfValueFromClassABC()
		{
			TestDateTimeEnumClassABC abc = new TestDateTimeEnumClassABC();
			
			Console.WriteLine("value " + TestDateTimeEnumClassABC.B.ToString("G")
								+ " has name " + abc.GetNameForValue(TestDateTimeEnumClassABC.B));
			Console.WriteLine("value " + TestDateTimeEnumClassABC.A 
				+ " has name " + abc.GetNameForValue(TestDateTimeEnumClassABC.A));
			
			Assert.AreEqual(abc.GetNameForValue(TestDateTimeEnumClassABC.B),"B");
			
			
		}


        /// <summary>
        /// Set value at instantiation
        /// </summary>
        [Test(Category = "Assign Value", Description = "Instantiate with a value.")]
        public void CreateWithValue()
        {
            TestDateTimeEnumClassABC sEnum = new TestDateTimeEnumClassABC(TestDateTimeEnumClassABC.A);
            Console.WriteLine("Test value is " + sEnum.Value.ToString());
            Assert.IsTrue(sEnum.Value == TestDateTimeEnumClassABC.A, "The value should equal A");

        }

        /// <summary>
        /// Check if value set
        /// </summary>
        [Test(Category = "Assign Value", Description = "Check if value set.")]
        public void AccessValueCheckAssigned()
        {
            TestDateTimeEnumClassABC sEnum = new TestDateTimeEnumClassABC();
            Assert.IsTrue(sEnum.IsValueSet == false, "The value should not be set.");
            sEnum.Value = TestDateTimeEnumClassABC.C;
            Assert.IsTrue(sEnum.IsValueSet == true, "The value should be set.");


        }


	}
}

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)

About the Author

Tim Schwallie
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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 May 2007
Article Copyright 2007 by Tim Schwallie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid