Converting String to Enum and Vice Versa

  1.  enum Latters { A=1, B = 2, C = 3, D = 4,E=5,F=6,G=7 };  
  2.   
  3.   
  4.       string[] stringLatters = { "0""2""8""H""C""D""E, I" };  
  5.       foreach (string stringLatter in stringLatters)  
  6.       {  
  7.          try {  
  8.             Latters Latter = (Latters) Enum.Parse(typeof(Latters), stringLatter);          
  9.             if (Enum.IsDefined(typeof(Latters), Latter) | Latter.ToString().Contains(","))    
  10.                Console.WriteLine("Converted '{0}' to {1}.", stringLatter, Latter.ToString());  
  11.             else  
  12.                Console.WriteLine("{0} is not an underlying value of the Latters enumeration.", stringLatter);  
  13.          }  
  14.          catch (ArgumentException) {  
  15.             Console.WriteLine("'{0}' is not a member of the Latters enumeration.", stringLatter);  
  16.          }  
  17.       }  
  18. // The example displays the following output:   
  19. //       Converted '1' to A.   
  20. //       Converted '2' to B.   
  21. //       8 is not an underlying value of the Latters enumeration.   
  22. //       'H' is not a member of the Latters enumeration.   
  23. //       Converted 'C' to C.   
  24. //       'I' is not a member of the Latters enumeration.   
  25. //       Converted 'D, D' to E, E.  

//Converting enum to string
string USERTYPE = UserType.CL.ToString() ;