Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
The Six Phases of a Project: Enthusiasm. Disillusionment. Panic. Search for the Guilty. Punishment of the Innocent. Praise for non-participants

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:03:01 AM CT Jan 10, 2007
Posted:10:18 PM CT Jan 09, 2007

Convert Integer To Enum Instance in C#

Author: Steve Lautenschlager

SnippetEnumsC#.NET

 Summary

Enums are a powerful construction in C# and other programming languages when you are working with finite sets such as fruits, days of the week or colors. Visual Studio's Intellisense is very nice with Enums because it lists all the options for the programmer to choose from.

But quite often you want to print enums, compare int values, or serialize an enum--and then you have to do some conversions.

The following method may be run in the code-behind file of an ASPX page where you have added a Label control named lblOutput. However, this technique will work in any C# program, not just ASP.NET.

Example: Convert integer to Enum instance

public void EnumInstanceFromInt()
{
   // The .NET Framework contains an Enum called DayOfWeek.
   // Let's generate some Enum instances from int values.

   // Usually you wouldn't cast an instance of an existing Enum to an int
   // in order to create an Enum instance.  :-)  You would have the actual
   // integer value, perhaps a value from a database where the int value of
   // the enum was stored.

   DayOfWeek wednesday = 
      (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Wednesday);
   DayOfWeek sunday = 
      (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Sunday);
   DayOfWeek tgif = 
      (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Friday);

   lblOutput.Text = wednesday.ToString() 
      + ".  Int value = " + ((int)wednesday).ToString() + "<br>";
   lblOutput.Text += sunday.ToString() 
      + ".  Int value = " + ((int)sunday).ToString() + "<br>";
   lblOutput.Text += tgif.ToString() 
      + ".  Int value = " + ((int)tgif).ToString() + "<br>";

}
The Enum.ToObject method takes two arguments. The first is the type of the
enum you want to create as output. The second field is the int to convert.
Obviously, there must be a corresponding Enum entry for the conversion to succeed.

Example: Output

Wednesday. Int value = 3
Sunday. Int value = 0
Friday. Int value = 5

Add New Comment
Convert Integer To Enum Instance in C#
Yoho30 Mar 07, 8:22Reply 
re: Convert Integer To Enum Instance in C#
Steve Lautenschlager30 Mar 07, 20:19Reply 
re: Convert Integer To Enum Instance in C#
Deepak Reddy14 Jun 07, 14:54Reply 
re: Convert Integer To Enum Instance in C#
Brent14 Jun 07, 15:19Reply 
re: Convert Integer To Enum Instance in C#
Ramveer21 Nov 08, 6:18Reply 
Convert Integer To Enum Instance in C#
My Name20 Apr 07, 7:21Reply 
re: Convert Integer To Enum Instance in C#
Steve Lautenschlager20 Apr 07, 20:53Reply 
re: Convert Integer To Enum Instance in C#
Peter21 Jun 07, 8:52Reply 
Convert Integer To Enum Instance in C#
Kimuyen Hoang18 Jul 07, 14:09Reply 
re: Convert Integer To Enum Instance in C#
Kimuyen Hoang18 Jul 07, 15:07Reply 
Convert Integer To Enum Instance in C#
manovich27 Sep 07, 16:45Reply 
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve founded Cambia Research to augment his .NET hobby. Developing and maintaining the site combines his interests in technology, writing and education.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com