Re: Alternative to Enum string values as return types and parameters

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Sanjay Pais" <sanjay@xxxxxxxxxx> wrote in message
news:%23na3xWafFHA.1412@xxxxxxxxxxxxxxxxxxxxxxx
>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
>


.



Relevant Pages

  • Confused by implementing an IEnumerable with iterator blocks
    ... I wanted to write a class to encapsulate an enumeration of all the files in a directory. ... but when hundreds of files are involved and the directory is on a remote server this causes a substantial delay, so I wanted to design an enumeration that would browse through the root directory and its subdirectories incrementally as iteration took place. ... having read up only briefly on how "yield" works: ... public string Root ...
    (microsoft.public.dotnet.languages.csharp)
  • Windows.forms.keys collection
    ... I have the below code for sending each character in a string as a Keys.x enumeration. ... I've found that when I convert the char, it does not match the enumeration item index and thereby generates a very random output. ... Dim strCaseID As String = "s" ...
    (microsoft.public.dotnet.framework)
  • Re: enum.ToString()
    ... public static string GetEnumValueDescription ... DescriptionAttribute pobjAttribute = ... How do I display the descriptive string ... you should use attributes to attribute the>> elements in the enumeration. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic List Find.
    ... Elasped time for Enumeration is 00:00:13.0926257 ... Dim Products As New List ... Private Function FindProduct(ByVal code As String) As Product ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Generic way of writing this enum code?
    ... // Check to make sure that T is an enumeration. ... // Create the string builder. ... because there's no constraint making T convertible ... foreach (EnumType1 type in types) ...
    (microsoft.public.dotnet.languages.csharp)