11

Can an enum value be saved as a setting, using the Properties.Settings.Default["MySetting"] syntax of C#? I tried to create a setting in my project's property pages, but only system classes appeared in the list of available types.

If it can be done, how do I do it? Thanks in advance for your help.

David Veeneman
  • 18,912
  • 32
  • 122
  • 187

3 Answers3

17

just store it as an int and convert it when needed.

Properties.Settings.Default["MySetting"] = myEnumValue;

// and later 
var settingValue = Properties.Settings.Default["MySetting"];
MyEnum value = (MyEnum)settingValue;

If you feel the need you can use Enum.IsDefined(typeof(MyEnum), value) to make sure it is valid. You can also store a string value so that it is in a human-readable format in your config file:

Properties.Settings.Default["MySetting"] = myEnumValue.ToString();

// and later 
var settingValue = Properties.Settings.Default["MySetting"];
MyEnum value = (MyEnum)Enum.Parse( typeof(MyEnum), settingValue );
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 7
    Or store a `string`, and then use `Enum.TryParse`. More human-readable that way. – Ben Voigt Aug 25 '11 at 22:14
  • 1
    There is a better answer [here](http://stackoverflow.com/questions/2184209). In summary, in the Type column, select "Browse...", and then type the full name of the enum in the "Selected type" text box at the bottom. – Tsahi Asher Dec 21 '16 at 14:07
1

It is an old post but I think this solution is worth publishing for whom may encounter the same issue.
Basically it consists in creating a new library to be referenced by the main project so that this library exposes the enum as a new type that can be selected from Properties.Settings.settings. In my case I want to enumerate levels of severity.

The new Library
Under your current solution, create a new empty Class Library with the code below:

namespace CustomTypes
{
    [Serializable]
    public enum Severity
    {
        INFO,
        WARNING,
        EXCEPTION,
        CRITICAL,
        NONE
    }
}

Reference the Library

  • Compile and reference the newly created library in all the projects that are going to use this type.
  • Now open your project's Properties => Settings.
    The new library may not be yet visible in the type DropDown list. If you don't see it, select Browse at the bottom of the DropDown and try to find the library.
    If it is still not visible, type the full path to the new type in the Selected Type field. (In this example, enter "CustomTypes.Severity" as shown below:

    enter image description here

    From now on, the new type should be visible and usable in Properties.Settings.settings.

    enter image description here
Jean-François
  • 374
  • 3
  • 8
0

Here is how I set the Setting type to MyEnum enum type:

  1. Create your new property setting of type string (type string will be changed to MyEnum type in the next steps)

  2. Open Settings.Designer.cs

:enter image description here

  1. Modify the return type of your new Property from string to your enum type MyEnum:

    Given the Setting Enum Type is named MyEnum(in Company.Enums namespace), edit the return type to be of type global::Company.Enums.MyEnum:

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("MySetting")]
        public global::Company.Enums.MyEnum MyEnum{
            get {
                return ((global::Company.Enums.MyEnum)(this["MyEnum"]));
            }
        }
    

This way it looks much cleaner, with only one statement; type safety is ensured on the client side, without the need to do the unnecessary string to enum parsing.

MyEnum value = Properties.Settings.Default["MySetting"];
Pinte Dani
  • 1,989
  • 3
  • 28
  • 35
  • 3
    The problem with this approach is that VS will clobber your changes if you ever make any alteration to the .settings. You should never alter *.Designer.cs files. – Tim Sparkles Jul 29 '15 at 21:23
  • 4
    What you can do is create another file extending the partial class defined in the designer, containing a conversion property. – Tim Sparkles Jul 29 '15 at 21:24