none
Convert comma separate string to enums RRS feed

  • Question

  • hi there,

    ive got, say the following sample code:

    [Flags]
        public enum Types
        {
            Type1 = 1,
            Type2 = 2,
            Type3 = 3,
            Type4 = 4,
            Type5 = 5
        }

    and a variable holding the following string:

    var stringTypes = "Type2,Type4,Type5";

    How can i convert this string into a property/field which returns these 3 string as 3 piped enum values eg.

    Types expected = Types.Type2 | Types.Type4 | Types.Type5;

    thanks

    Thursday, October 30, 2014 1:55 AM

Answers

  • Hello Milsnips,

    >>How can i convert this string into a property/field which returns these 3 string as 3 piped enum values eg.

    You could try to use the Enum.Parse Method (Type, String) as:

    string stringTypes = "Type2, Type4, Type5";
    
    Types expected = (Types)Enum.Parse(typeof(Types), stringTypes);
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Blackwood Thursday, October 30, 2014 2:20 PM
    • Marked as answer by Milsnips Thursday, October 30, 2014 11:48 PM
    Thursday, October 30, 2014 6:57 AM
    Moderator
  • not quite what i was expecting.. if one or more of the strings is a type, no enums are returned, where i wanted the valid ones still returned. here's the code i wrote to do it:

    public static VersionType ToEnums(this string stringValues)
            {
                if (string.IsNullOrWhiteSpace(stringValues)) return default(VersionType);
    
                var parsedTypes = new VersionType ();
                foreach (var s in stringValues.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    VersionType outEnum;
                    if (!Enum.TryParse(s, false, out outEnum)) continue;
                    parsedTypes = parsedTypes | outEnum;
                }
    
                return parsedTypes;
            }
    


    • Marked as answer by Milsnips Thursday, October 30, 2014 10:49 AM
    • Edited by Milsnips Thursday, October 30, 2014 11:49 PM
    Thursday, October 30, 2014 10:49 AM

All replies

  • Hello Milsnips,

    >>How can i convert this string into a property/field which returns these 3 string as 3 piped enum values eg.

    You could try to use the Enum.Parse Method (Type, String) as:

    string stringTypes = "Type2, Type4, Type5";
    
    Types expected = (Types)Enum.Parse(typeof(Types), stringTypes);
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Blackwood Thursday, October 30, 2014 2:20 PM
    • Marked as answer by Milsnips Thursday, October 30, 2014 11:48 PM
    Thursday, October 30, 2014 6:57 AM
    Moderator
  • not quite what i was expecting.. if one or more of the strings is a type, no enums are returned, where i wanted the valid ones still returned. here's the code i wrote to do it:

    public static VersionType ToEnums(this string stringValues)
            {
                if (string.IsNullOrWhiteSpace(stringValues)) return default(VersionType);
    
                var parsedTypes = new VersionType ();
                foreach (var s in stringValues.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    VersionType outEnum;
                    if (!Enum.TryParse(s, false, out outEnum)) continue;
                    parsedTypes = parsedTypes | outEnum;
                }
    
                return parsedTypes;
            }
    


    • Marked as answer by Milsnips Thursday, October 30, 2014 10:49 AM
    • Edited by Milsnips Thursday, October 30, 2014 11:49 PM
    Thursday, October 30, 2014 10:49 AM