Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using Enum.TryParse for parsing value to enum

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 337
    Comment on it

    This is a demo of using Enum.TryParse method for converting string values to enum type.

    We have function Enum.TryParse under System namespace in Enum class. This method is used for parsing string values to enum type. Signature of the method is as follows :

    // Summary:
            //     Converts the string representation of the name or numeric value of one or
            //     more enumerated constants to an equivalent enumerated object. The return
            //     value indicates whether the conversion succeeded.
            //
            // Parameters:
            //   value:
            //     The string representation of the enumeration name or underlying value to
            //     convert.
            //
            //   result:
            //     When this method returns, contains an object of type TEnum whose value is
            //     represented by value. This parameter is passed uninitialized.
            //
            // Type parameters:
            //   TEnum:
            //     The enumeration type to which to convert value.
            //
            // Returns:
            //     true if the value parameter was converted successfully; otherwise, false.
      public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct;
    

    It returns true if parsing was successful and false in opposite case

    It gives result as out type parameter if parsing is successful.

    Here is a demo program:

    using System;
    namespace SendEmail
    {
        class Program
        {
            static void Main(string[] args)
            {
                BadgeType badgeType;
                BadgeType badgeTypePotluck;
                BadgeType badgeTypeNone;
    
                string valueForParsing = "1";
    
                //Will get value for index 1
                if (Enum.TryParse(valueForParsing, out badgeType))
                    Console.WriteLine(badgeType.ToString());
                else
                    Console.WriteLine(string.Format("Enum does not contain given value : {0}", valueForParsing));
    
                valueForParsing = "PotLuck";
    
                //Will get index for value "Potluck"
                if (Enum.TryParse(valueForParsing, out badgeTypePotluck))
                    Console.WriteLine((int)badgeTypePotluck);
                else
                    Console.WriteLine(string.Format("Enum does not contain given value : {0}", valueForParsing));
    
                valueForParsing = "none";
                //Will return false as enum does not contain any value for "none"
                if (Enum.TryParse(valueForParsing, out badgeTypeNone))
                    Console.WriteLine((int)badgeTypePotluck);
                else
                    Console.WriteLine(string.Format("Enum does not contain given value : {0}", valueForParsing));
    
                Console.Read();
            }
    
            /// <summary>
            /// Enum with starting index as 1
            /// </summary>
            public enum BadgeType
            {
                Twingle = 1,
                PotLuck,
                Poll
            }
        }
    }
    

    In above program we have an Enum with the name BadgeType. We are parsing three different values for this enum. In first parsing we are giving it the index value and it is parsing successfully and returning string value as result. In second parsing we are giving it the string value, it is parsing successfully and printing index value result. In third case, we are giving it a string value none which is not contained in Enum thats why it is returning false and we are printing custom error message.

    Result of above program will appear like this :

    Twingle
    2
    Enum does not contain given value : none

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: