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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vladimir NaniPosted Apr 10, 2010, 1:47 PM
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 GhostPosted Apr 12, 2010, 4:05 AM
This makes more sense now!
Cheers,