Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using own enum in settings

I would like to use my own enum in a project setting (from Visual studio, menu project, properties, tab settings).

I can select a lot of default types there, but even types from other projects in my solution, but not the project itsself.

Is it possible to use an enumeration type from the project itsself as type for a setting?

like image 505
Michel Keijzers Avatar asked Aug 11 '12 18:08

Michel Keijzers


People also ask

How do I assign an enum to a string?

There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.

Can you add values to enum?

By default enums have their own string values, we can also assign some custom values to enums.

Should enums be in separate files?

If the enum is only used within one class, it should be placed within that class, but if the enum is used between two or more classes, it ought to either be in it's own code file, or in a conglomerated code file that contains all the enum for a particular assembly.

Do enums have default values?

The default value of an enum E is the value produced by the expression (E)0 . Without overriding the default values, printing default(E) returns Foo since it's the first-occurring element.


2 Answers

Yes, but it takes a bit of twiddling. See this article for pointers on how to accomplish it.

like image 199
kprobst Avatar answered Sep 24 '22 23:09

kprobst


Not as far as I would know.

But you could do something like:

string setting = ConfigurationManager.AppSettings.Get("yourSetting");
YourEnum yourEnumVariable = (YourEnum)Enum.Parse(typeof(YourEnum), setting);

(Written just here without IDE sytax checking, please forgive any syntax mistakes.)

like image 33
Jens H Avatar answered Sep 26 '22 23:09

Jens H