public T GetOne
{
T one = (T)Enum.Parse(typeof(T), o.ToString());
return one;
}
Thanks,
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vladimir Nani29 reputationPosted Apr 10, 2010, 9:47 AM EDT
enum Numbers
{
One, Two, Three
}
static void Main(string[] args)
{
Numbers n = (Numbers)Enum.Parse(typeof(Numbers), "Three"));
}
Enum.Parse function will return an enumerated object if your string will match name of one from Numbers.
Then you explicitly cast it to your Numbers enum.
public T GetOne
{
T one = (T)Enum.Parse(typeof(T), o.ToString());
return one;
}
This is a generic function which does the same but with some enumeration T.
The previous example could be done with your function like that:
enum Numbers
{
One, Two, Three
}
static void Main(string[] args)
{
Numbers n = GetOne
Console.WriteLine(n.ToString());
}
public static T GetOne
{
T one = (T)Enum.Parse(typeof(T), o.ToString());
return one;
}
String "Three" is passed as param but you can pass any object.
This may help you: http://msdn.microsoft.com/en-us/library/essfb559.aspx
Space Ghost45 reputationPosted Apr 12, 2010, 12:05 AM EDT
This makes more sense now!
Cheers,