Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom enum as application setting type in C#?

Tags:

c#

enums

settings

If have an enum in C#:

[Serializable]
public enum OperatingSystem 
{
    Windows,
    Macintosh
}

For my application I use the application settings, where I can select of which Type a setting should be. I thought when I select Browse, I could choose my enum or type the fully qualified path to select that enum as the Type.

Edit:
I set the type to my Enum, but in the Value (where Windows, Macintosh should be) only Windows is visible and i'm able to enter any string.

like image 383
MysticEarth Avatar asked Feb 02 '10 13:02

MysticEarth


People also ask

What is enum data type in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Which datatype can be used with enum?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

What is enum in C sharp?

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members: C# Copy. enum Season { Spring, Summer, Autumn, Winter }

Are enumerations available in C sharp?

Enumeration (or enum) is a value data type in C#. It is mainly used to assign the names or string values to integral constants, that make a program easy to read and maintain.


Video Answer


3 Answers

I see this in VC# Express 2005. The Browse.. "Select a Type" dialog shows only the System and Microsoft namespaces. However if you insert the full name of the type into the Selected Type textbox it should accept it.

like image 54
AlanN Avatar answered Oct 19 '22 11:10

AlanN


Small addition to all previous answers. As for me - it was needed to BUILD solution before my custom enum has been successfully added to the "Custom type" text box.

like image 40
Vyacheslav Enis Avatar answered Oct 19 '22 12:10

Vyacheslav Enis


Sure - just add a serializable enum to your project, select browse and type in the namespace qualified name, e.g. ClassLibrary1.OperatingSystems. Bingo.

"I set the type to my Enum, but in the Value (where Windows, Macintosh should be) only Windows is visible and i'm able to enter any string".

Have you tried entering something other than 'windows' or 'macintosh'? The behavior you see is as close to what you want as you are going to get. I am pretty confident on that.

In any case... good luck.

See it done in 30 seconds here.

like image 14
Sky Sanders Avatar answered Oct 19 '22 11:10

Sky Sanders