I have to work with .NET 3.5 but want to use TryParse method which I know belongs to .NET 4.0. Then I searched the web for the subject and I think I found the best solution in [Simon Mourier's answer] (https://stackoverflow.com/a/6161718/1043198)!
So I created a class of my own like follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace MyClasses
{
public class MyEnum
{
// here goes all of Simons's code
}
}
then I tried to use that class in a new project like follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyClasses;
namespace MyEnumerations
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type in a name: ");
string userValue = Console.ReadLine();
MyEnumeration myValue;
if (MyEnum.EnumTryParse(myValue,userValue, out myValue))
{
switch (myValue)
{
case MyEnumeration.Elem1:
Console.WriteLine("Elem1 caught!");
break;
case MyEnumeration.Elem2:
Console.WriteLine("Elem2 caught");
break;
case MyEnumeration.Elem3:
Console.WriteLine("Elem3 caught");
break;
default:
Console.WriteLine("Does not compute");
break;
}
}
Console.ReadLine();
}
}
enum MyEnumeration
{
Elem1,
Elem2,
Elem3
}
}
but I can't get the proper sintax for using the "EnumTryParse" method: specifically it seems I can't pass correctly the first parameter which must be "Type type".
As it comes out I'm a total newby of C# and I know for sure I must be missing something that will make me whap my head once I'll get the proper sintax. but for now I'm stuck with it.
thank you for any possible help
As the error message is saying, you need to pass the type of the enum as the first argument. You also need to change the type of myValue
to match the out
parameter of EnumTryParse
, so:
object myValue;
if (MyEnum.EnumTryParse(typeof(MyEnumeration), userValue, out myValue))
{
MyEnumeration actualValue = (MyEnumeration) myValue;
...
}
You might also want to consider my Unconstrained Melody project, which would allow:
MyEnumeration myValue;
if (Enums.TryParseName(userValue, out myValue))
{
// Do stuff
}
Or with an explicit type argument:
MyEnumeration myValue;
if (Enums.TryParseName<MyEnumeration>(userValue, out myValue))
{
// Do stuff
}
Unconstrained Melody has a bunch of generic methods, including extension methods, which allow all this to be done without the boxing and casting which is normally involved.
The last parameter of the EnumTryParse
method is out object
, so the argument you pass must be of type object
, not MyEnumeration
(because ref
and out
parameters are invariant). Since this is not very convenient, I suggest you make the method generic instead:
public static bool EnumTryParse<T>(string s, out T value)
{
value = default(T);
if (Enum.IsDefined(typeof(T), s))
{
value = (T)Enum.Parse(typeof(T), s);
return true;
}
return false;
}
You can then call it like this:
if (MyEnum.EnumTryParse(userValue, out myValue))
(the generic type parameter is inferred automatically from the type of myValue
, so you don't need to specify it)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With