Geeks With Blogs
Jonathan Starr's Blog Thoughts on C#, Ajax, WCF, LINQ, Agile et al.

improve :my => 'code'

This came in handy for me, and I thought others might feel the same way... 

Say you have one enumeration used as arguments for a method, and you created a simplified enumeration to override the original method to create a method that is easier to consume...

Example:

 

 public enum XTypes{

   PFM = 0,

   STAT = 1,

   YIELD = 2,

   RANK = 3

}

and

public enum XTypesSimplified{

   PFM = 0,

   STAT = 1

}

You can use the following to convert one into another.

 

 public class EnumHelper

{  

public static T EnumToEnum<T, U>(U enumArg)

   {

    if (!typeof(T).IsEnum) throw new Exception("This method only takes enumerations.");  

    if (!typeof(U).IsEnum) throw new Exception("This method only takes enumerations.");

    try

      {

        return (T) Enum.ToObject(typeof(T), enumArg);  

      }

    catch  

      {

         throw new Exception ( string.Format ("Error converting enumeration {0} value {1} to enumeration {2} " ,  

         enumArg.ToString(),

         typeof(U).ToString(),

         typeof(T).ToString()) );

       }

   }

}

 

Here's an example of using the code...

XTypesSimplified xtypesSimplified = XTypesSimplified.PFM;

XTypes xtypes = EnumHelper.EnumToEnum<XTypes, XTypesSimplified>(xtypesSimplified);

   

Comments welcome!

Jonathan

Posted on Wednesday, October 17, 2007 6:54 PM C# | Back to top


Comments on this post: Converting One Enumeration to Another Enumeration

# re: Converting One Enumeration to Another Enumeration
Requesting Gravatar...
Couple of Comments:
You can add constraints to the class decleration and dispense with runtime cheking
public static T EnumToEnum<T, U>(U enumArg) where T: Enum where U:Enum {

Not sure where to use such conversion. Surly if all you wanted is the underyling decimal value, you could simply say
public class demo
{

public enum Enum1
{ x, y, z }

public enum Enum2
{ r
,
s, t }

public static void Main()
{
Enum1 target;

target = (Enum1)(int)Enum2.s;
Console.WriteLine(target.ToString());
}

}
You will have to catch an exception if it occurs.
I am also curious about the general idea here. Enums are value types and the "full" and "reduced" enums you declare are actually 2 different types altogether. Is there a funtion f(Enum1) and g(Enum2)? are you using enums only to "strongly type" their names?

Is this a schema devised to populate some drop down with only the simplified types but internally use the more robust one?

Left by nuri on Oct 24, 2007 3:15 AM

# re: Converting One Enumeration to Another Enumeration
Requesting Gravatar...
Nuri,

I should have pointed out how I was using this more in my post. In my case, I needed to create a facade for a business class method that used lots of enumerations as arguments... Based on the value for one of the arguments, many of the choices for other enumerations were invalid. Instead of allowing the user to call the method with invalid arguments, I created new methods (one for each value of the restricting enumeration) that had smaller enumerations for the dependent types...

In order to this this in an object oriented way, I had these new methods call the original method, and had to convert the simpler, restricted enumerations to the originals.

While your code works, (1) it uses to conversions (yours converts to an int, and then to the new enumeration) not one as mine does (2) it does not provide a mechanism for exception handling.

Thanks for your comments Nuri!

(Note that I also had to eventually change this code to handle nullable enumeration conversions, so it was important to put this in a method... Also, I have a later post that shows how enumerations can be converted by string (though I think my algorithm should be improved).

Jonathan
Left by Jonathan on Oct 28, 2007 1:49 PM

# re: Converting One Enumeration to Another Enumeration
Requesting Gravatar...
This is actually wrong. Enum is not a valid type constraint. You can only constrain on stuct, IComparable, IConvertible, IFormattale. This method can also be implemented as an extension method.
Left by Steve Solomon on May 14, 2009 9:58 AM

# re: Converting One Enumeration to Another Enumeration
Requesting Gravatar...
Further to me earlier comment, this is how it can be expressed as an extension method

public static TDest Convert<TDest>(this Enum value) where TDest : struct, IComparable, IConvertible, IFormattable {

if(!typeof(TDest).IsEnum) throw new Exception("This method can only convert to an enumerations.");

try {
return (TDest) Enum.ToObject(typeof(TDest), value);
}
catch {
throw new Exception(string.Format("Error converting enumeration {0} to enumeration {1} ",
value, typeof(TDest)));
}

}
Left by Steve Solomon on May 14, 2009 10:07 AM

Your comment:
 (will show your gravatar)


Copyright © Jonathan Starr | Powered by: GeeksWithBlogs.net