Enum.TryParse

Ask general questions here.
omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Enum.TryParse

Post by omayer » Mon Jan 28, 2013 9:35 pm

I am trying to use Enum.TryParse but don't see it on intellisense, closest one found is Parse
Tipu

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Enum.TryParse

Post by Ciege » Mon Jan 28, 2013 9:50 pm

Are you using the System namespace?
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...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Enum.TryParse

Post by omayer » Thu Jan 31, 2013 5:53 pm

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

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