Oct 292009
 

This is the beginning of a guide to enumerations that i’m working on for Code-Project.

When dealing with data driven application design look up tables are something that have been used heavily to help express a value for something that can be cross referenced.  For instance if you want to store the alignment of text that you are storing in a database you wouldn’t want to store the full string representation of the alignment.   You also wouldn’t want to create a look up table as your alignment options are unlikely to change and it would only cause to create an extra unneeded table in your database structure. This is where enumerations come in.

An enumeration is a fairly basic structure to grasp, in it’s most basic form it is a class that contains code defined string/value pairs.  The important thing to note here is that the value of an enumeration can only be of Type byte, sbyte, short, ushort, int, uint, long, or ulong.  One thing that is important to note here is that all of these types are whole numeric numbers.   Writing an enumeration is easy, you simply declare your enumeration by giving it a name and then inside of the enumeration you declare all your options.  You can also declare the values you would like your individual enumerations to represent.  This is optional but if you don’t then the compiler will assign values for you so it’s generally better to assign values yourself as then you have more control.

A basic enumeration that would be used to represent horizontal alignment is as follows:

public enum HAlign
{
Left = 0,
Center = 1,
Right = 2
}

In this case the value of the enumeration can be stored in the database as a tinyint or comparable type.  You only need to reserve a small amount of space to store the values of an enumeration.  In code you would reference the enumeration using HAlign.Left or any comparable option.  That works great when you need to specify the value when you are writing the code, but what happens when you need to give the user the option to select the enumeration value.

That was the specific issue that I ran into, I needed to use an enumeration to populate a drop drown that would have the string representations and values for an enumeration to allow the user to select exactly what they would like to use. There are multiple ways to do this, it’s fairly easy to loop through an enumeration and pull out the string value pairs, unfortunately in my case I was using a Dev Express control and needed to use an object data source control.  So that’s where my method that I wrote comes into play, there are two different versions of it that you will see below this post both with sample calls to show how they would be used.

The first example would be geared towards full code use as it accepts an enumeration type.  This is good for when you want to write code behind that will streamline the creation of data sources for enumeration driven controls.  The second would be used when you want to create and ASP.NET object data source and use it to directly populate another control.  This method takes in a string that is the fully qualified name of the enumeration.  It was done this way because there are not a whole lot of options when selecting the values for the parameters of an object data source.

One final thing to note, in the examples I’ve included some code that will modify the enumeration name and replace all underscores with a space.  This is used solely for display purposes for when you have multiple words in a single enumeration string.  As you can not define an enumeration that has spaces in it this is just one of the many implementations that you could use to modify the display properties.  Unfortunately this is something that needs to be done when the enumerations are defined as such it’s up to the developer to decide which method is best.

Stay tuned for Part two in which I will discuss the best method I’ve found for turning a value from the database back into an enumeration.

/// <summary>
        /// Method takes in an Enumeration Type Name as a string and returns a DataTable of 
        /// All Name / Value pairs
        /// </summary>
        /// <param name="sEnumerationName">Fully Qualified Type Name of Enumeration to Use</param>
        /// <returns>Data Table containing the Display and Value pairing for the Enumeration</returns>
        public static System.Data.DataTable GetEnumerationTable(string sEnumerationName)
        {
            System.Data.DataTable dtEnumerationTable = new System.Data.DataTable();
            dtEnumerationTable.Columns.Add("Text", typeof(string));
            dtEnumerationTable.Columns.Add("Value", typeof(long));

            try
            {
                foreach (string sText in Enum.GetNames(Type.GetType(sEnumerationName)))
                {
                    System.Data.DataRow drType = dtEnumerationTable.NewRow();

                    drType["Text"] = sText.Replace("_", " ");
                    drType["Value"] = (long)Enum.Parse(Type.GetType(sEnumerationName), sText);

                    dtEnumerationTable.Rows.Add(drType);
                }
            }
            catch { }

            return dtEnumerationTable;
        }

        /// <summary>
        /// Method takes in an Enumeration Type and returns a DataTable of 
        /// All Name / Value pairs
        /// </summary>
        /// <param name="EnumerationType">Type Value of Enumeration to Use</param>
        /// <returns>Data Table containing the Display and Value pairing for the Enumeration</returns>
        public static System.Data.DataTable GetEnumerationTable(Type EnumerationType)
        {
            System.Data.DataTable dtEnumerationTable = new System.Data.DataTable();
            dtEnumerationTable.Columns.Add("Text", typeof(string));
            dtEnumerationTable.Columns.Add("Value", typeof(long));

            try
            {
                foreach (string sText in Enum.GetNames(EnumerationType))
                {
                    System.Data.DataRow drType = dtEnumerationTable.NewRow();

                    drType["Text"] = sText.Replace("_", " "); ;
                    drType["Value"] = (long)Enum.Parse(EnumerationType, sText);

                    dtEnumerationTable.Rows.Add(drType);
                }
            }
            catch { }

            return dtEnumerationTable;
        }

  One Response to “Fun With Enumerations Part 1 – Using Enumerations as a Data Source”

  1. […] I’ve finally gotten around to finishing this second post. The first part of the Post can be viewed here. […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>