Enum.TryParse
Enum.TryParse
I am trying to use Enum.TryParse but don't see it on intellisense, closest one found is Parse
Tipu
Re: Enum.TryParse
Are you using the System namespace?
http://msdn.microsoft.com/en-us/library/dd783499.aspx
http://msdn.microsoft.com/en-us/library/dd783499.aspx
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!
Ciege...
Ciege...
Re: Enum.TryParse
Thank you Ciege for looking into it- following error returned on compile --
The non-generic method 'System.Enum.Parse(System.Type, string)' cannot be used with type arguments (CS0308) - C:\GenericMethods\Enumeration.cs:64,19
The non-generic method 'System.Enum.Parse(System.Type, string)' cannot be used with type arguments (CS0308) - C:\GenericMethods\Enumeration.cs:64,19
Code: Select all
using System;
using System.Collections.Generic;
using System.Collections;
enum TestEnv
{
A1,
A2,
A3,
}
Console.WriteLine("Type Env Name");
string userValue = Console.ReadLine();
TestEnv myValue;
//convert userValue to myValue
//if (Enum.Parse(typeof(TestEnv ),userValue)) //converting userValue,ignoring case true,
if (Enum.Parse<TestEnv >(userValue, true, out myValue))//converting userValue,ignoring case true,
{ //outputting myValue
switch (myValue)
{
case TestEnv.A1:
Console.WriteLine("A1 ");
break;
case TestEnv.A2:
Console.WriteLine("A2 ");
break;
case TestEnv.A2:
Console.WriteLine("A3 ");
break;
default:
Console.WriteLine("Try again");
break;
}
}
else
{
Console.WriteLine("No Env Available ");
}
Console.ReadLine();
}
}
}
Tipu