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 HowGoodAttribut e 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 GetCustomAttrib utes.
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]
-
mv*@spam.guard. caspershouse.co m
"Sanjay Pais" <sa****@nospam. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 HowAreYouFeelin g(string MyName)
{
return HOW_GOOD.NOT_TO O_BAD;
}
or
public string HowAreYouFeelin g(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