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.
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 );
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
type
DropDown list. If you don't see it,
select Browse at the bottom of the DropDown and try to find the library. Selected Type
field. (In this example, enter "CustomTypes.Severity" as shown below:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With