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 CPOL
Using Generics to make a different kind of enumeration: easy to comment, and supports many types.
using System;
using Adapdev.UnitTest;
using Enums;

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

		
		/// <summary>
		/// Declares and sets values twice for TestDateTimeEnumABC
		/// </summary>
		[Test]
		public void SetABCtoTwoValues()
		{
			TestDateTimeEnumABC abc = new TestDateTimeEnumABC();
			abc.Value = TestDateTimeEnumABC.A;
			Assert.IsTrue(abc.Value == new DateTime(2005,01,22),"The value should equal '01/22/2005'");
			abc.Value = TestDateTimeEnumABC.B;
			Assert.IsTrue(abc.Value == new DateTime(2005,01,23),"The value should equal '01/23/2005'");
		}

		/// <summary>
		/// Declare abc and xyz TestDateTimeEnums.
		/// Makes sure can do different enums from abstrct DateTimeEnum.
		/// Confirms return correct value.
		/// </summary>
		[Test]
		public void SetABCAndXYZValues()
		{
			TestDateTimeEnumABC abc = new TestDateTimeEnumABC();
			abc.Value = TestDateTimeEnumABC.A;
			Assert.IsTrue(abc.Value == new DateTime(2005,01,22),"The value should equal '01/22/2005'" );
			TestDateTimeEnumXYZ xyz = new TestDateTimeEnumXYZ();
			xyz.Value = TestDateTimeEnumXYZ.X;
			Assert.IsTrue(abc.Value == new DateTime(2005,01,22),"The value should equal '01/22/2005'");
			Assert.IsTrue(xyz.Value == new DateTime(2005,3,15),"The value should equal '03/15/2005'");
		}

		/// <summary>
		/// Makes sure receive exception when set to bad value.
		/// </summary>
		[Test]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void AssignABCaBadValue()
		{
			TestDateTimeEnumABC abc = new TestDateTimeEnumABC();
			abc.Value = new DateTime(2000,01,01);
			Assert.IsTrue(abc.Value == new DateTime(2000,01,01),"The value should equal 01/01/2000");
			
		}


		/// <summary>
		/// Makes sure receive exception when have bad constant.
		/// </summary>
		[Test]
		[ExpectedException(typeof(ArrayTypeMismatchException))]
		public void CreateEnumWithBadConstantTypeDeclaration()
		{
			TestDateTimeEnumBadType sEnum = new TestDateTimeEnumBadType();
			sEnum.Value = new DateTime(2000,1,1);
			Assert.IsTrue(sEnum.Value == new DateTime(2000,1,1),"The value should equal 01/01/2005");
			
		}

		/// <summary>
		/// Makes sure receive exception when have bad constant.
		/// </summary>
		[Test]
		[ExpectedException(typeof(NotSupportedException))]
		public void CreateEnumWithDuplicateConstantDeclaration()
		{
			TestDateTimeEnumDuplicate sEnum = new TestDateTimeEnumDuplicate();
			sEnum.Value = new DateTime(2000,1,1);
			Assert.IsTrue(sEnum.Value == new DateTime(2000,1,1),"The value should equal 01/01/2005");
			
		}
		

		/// <summary>
		/// Get array of values from enumeration
		/// </summary>
		[Test]
		public void GetValuesForABC()
		{
			TestDateTimeEnumABC abc = new TestDateTimeEnumABC();
			//abc.Value = TestDateTimeEnumABC.DateTimeNull ;
			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]
		public void GetNamesForABC()
		{
			TestDateTimeEnumABC abc = new TestDateTimeEnumABC();
			//abc.Value = TestDateTimeEnumABC.DateTimeNull ;
			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]
		public void GetNamedValuesFromABC()
		{
			TestDateTimeEnumABC abc = new TestDateTimeEnumABC();
			//abc.Value = TestDateTimeEnumABC.DateTimeNull ;
			string[,] namedValues = abc.GetNamedValues();
			for (int i = 0;i<namedValues.GetLength(1);i++)
			{
				Console.WriteLine("Name at position " + i.ToString() + " is " + namedValues[0,i]
							+ " value at position is " + namedValues[1,i]);
			}
			
		}


	}
}

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)

Share

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 | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 12 May 2007
Article Copyright 2007 by Tim Schwallie
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid