Click here to Skip to main content
15,886,873 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.1K   164   26  
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 TestStringEnums
	{
		//public const string StringNull = ""; //we don't like nulls!
		//public const string StringNotApplicable = "NOTAPPLICABLE";

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

		/// <summary>
		/// Declare abc and xyz TestStringEnums.
		/// Makes sure can do different enums from abstrct StringEnum.
		/// Confirms return correct value.
		/// </summary>
		[Test]
		public void SetABCAndXYZValues()
		{
			TestStringEnumABC abc = new TestStringEnumABC();
			abc.Value = TestStringEnumABC.A;
			Assert.IsTrue(abc.Value == "A","The value should equal 'A'");
			TestStringEnumXYZ xyz = new TestStringEnumXYZ();
			xyz.Value = TestStringEnumXYZ.X;
			Assert.IsTrue(abc.Value == "A","The value should equal 'A'");
			Assert.IsTrue(xyz.Value == "X","The value should equal 'X'");
		}

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


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

		/// <summary>
		/// Makes sure receive exception when have bad constant.
		/// </summary>
		[Test]
		[ExpectedException(typeof(NotSupportedException))]
		public void CreateEnumWithDuplicateDeclaration()
		{
			TestStringEnumDuplicate sEnum = new TestStringEnumDuplicate();
			sEnum.Value = "N";
			Assert.IsTrue(sEnum.Value == "N","The value should equal 'N'");
			
		}
		
		/// <summary>
		/// Get array of values from enumeration
		/// </summary>
		[Test]
		public void GetValuesForABC()
		{
			TestStringEnumABC abc = new TestStringEnumABC();
			
			string[] values = abc.GetValues();
			for (int i = 0;i<values.Length;i++)
			{
				Console.WriteLine("Value at position " + i.ToString() + " is " + values[i]);
			}
			
		}

		
		/// <summary>
		/// Get array of constant names from enumeration
		/// </summary>
		[Test]
		public void GetNamesForABC()
		{
			TestStringEnumABC abc = new TestStringEnumABC();
			
			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()
		{
			TestStringEnumABC abc = new TestStringEnumABC();
			
			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)


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