Alternative to Enum string values as return types and parameters

S

Sanjay Pais

I know that string/char enum is not possible in c# (.NET2.0)
I need to create the equivalent of this:

public enum HOW_GOOD
{
AWESOME = "A",
GREAT= "G",
NOT_TOO_BAD = "N",
TERRIBLE="T"
}

i wanted to use this enum as a parameter/ return type for methods

Eg
public HOW_GOOD HowAreYouFeeling(string MyName)

{
return HOW_GOOD.NOT_TOO_BAD;
}
or

public string HowAreYouFeeling(HOW_GOOD m_HowGood)

{

return "Not Too Bad";
}


The rationale behind this is that this approach makes better readability for
the developers and makes my DBA happy :) as I will store only char values in
the DB. The dataset table results will have the char value too. However, if
I am forced to use enum's then the only alternative is numbers which are
really meaningless by themselves.

Thanks in advance

Sanjay
 
N

Nicholas Paldino [.NET/C# MVP]

Sanjay,

What I would do is use an enumeration with numbers. Then, to each field
of the enumeration, I would attach an attribute with the information that is
related to that enumeration member. For example, you could create an
attribute named HowGoodAttribute which takes a string in the constructor,
and has one property, the string that is passed in the constructor. Then,
you would do something like this on your enumeration:

// HOW_GOOD doesn't follow the public naming conventions for .NET, btw
public enum HowGood
{
[HowGood("A")]
Awesome,
[HowGood("G")]
Great,
[HowGood("N")]
NotTooBad,
[HowGood("T")]
Terrible
}

Then, when you need to access the string, you can use reflection to get
the attribute using the enumeration value. Basically, what you do is take
the string name of the enumeration value and get the static field that is
attached to that enumeration's type. Once you have the FieldInfo for that
field, you can get the attribute through a call to GetCustomAttributes.
With the attribute in hand, you can get the string assigned to it easily.

This way, you can pass around the enumeration value, and when you need
the extra information, get it from the attribute.

I've done this so many times that I wrote some utility methods that
handle that (it takes a type, the enumeration value, and optionally, the
type of attribute to get). It's a great use of attributes, IMO, and also a
good way to store more complex static data.

Hope this helps.
 
G

Guest

One way, would be to use the [Description()] attribute (found within
System.ComponentModel before each tag, so your enum would end as:

public enum HOW_GOOD
{
[Description("Awesome")]
AWESOME = "A",
[Description("Great")]
GREAT= "G",
[Description("Not Too Bad")]
NOT_TOO_BAD = "N",
[Description("Terrible")]
TERRIBLE="T"
}

And every time you want the description, use the following function:

public static string GetEnumDescription(object value)
{
string retVal = "";
try
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fieldInfo.GetCustomAttributes
(typeof(DescriptionAttribute), false);
retVal =
((attributes.Length>0)?attributes[0].Description:value.ToString());
}
catch( NullReferenceException )
{
//Occurs when we attempt to get description of an enum value that
does not exist
retVal = "Unknown";
}
return retVal;
}

One warning about this though... because this uses reflection to get the
string, it does cost you a bit of time, so if this is being done a lot back
to back, you might want to think about caching.

Brendan
 
F

Francois Bonin [C# MVP]

A simple solution would be
public class HowGood
{
public const string Awesome = "A";
public const string Great = "G";
public const string NotTooBad = "N";
public const string Terrible = "T";
}

You could then use the class variables just as an enum: HowGood.Awesome,
HowGood.Great ...

I changed the capitalization to keep in line with casing-conventions but you
can use what you want...

Cois
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top