I do mine like this:
public enum ExampleEnum
{
[Description("MyUnknownDescription")] Unknown = 0
[Description("Items of this enum are the coolest")] SuperCool =
1
}
Ok.. then I have a EnumHelper.
using System;
using System.ComponentModel;
using System.Reflection;
using System.Xml;
namespace MyCompany.Enums
{
/// <summary>
/// Summary description for EnumHelper.
/// </summary>
public class EnumHelper
{
private EnumHelper()
{
//only static methods
}
public static string GetDescription(System.Enum value)
{
//if a description is defined for an enum value, this procedure will get
it
//Example of defining a description with an enum value
/*
*
public enum ExampleEnum
{
[Description("MyUnknownDescription")] Unknown = 0
}
*
* */
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribut e),
false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
}
I kinda get the best of both worlds.
I use the enums as enums 99% of the time.
But I have get a long description if I ever need it.
"MarkusR" <ma*******@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.com...
Good day, bare with me while I probably ask a very novice question...
I have multiple projects that use the same constants. I want to create
a common file they can share.
Is this the best way to do it:
using System;
namespace myconsts
{
class orderconsts
{
public const int ORDER_STATUS_HOLD = 1;
public const int ORDER_STATUS_ERROR = 2;
public const int ORDER_STATUS_READYTOSHIP = 3;
public const int ORDER_STATUS_PACKAGED = 4;
public const int ORDER_STATUS_SHIPPED = 5;
public const int ORDER_STATUS_VOIDED = 6;
}
}
and access it using orderconsts.ORDER_STATUS_HOLD
-Markus_R