22

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.

Adaline Simonian
  • 4,596
  • 2
  • 24
  • 35
MysticEarth
  • 2,646
  • 4
  • 33
  • 53

6 Answers6

40

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.

AlanN
  • 516
  • 4
  • 2
  • It does indeed. But the values aren't displayed, or it only displays a textbox with "Windows" in it. But I can even give it the value of "this is a string". – MysticEarth Feb 02 '10 at 14:48
  • 1
    It is normal, in the XML the data is stored as string. In theory you can enter another value than one in the Enum (this is the reason why Enum.IsDefined exist). It will throw an Exception but you can do it :). – Julien N Feb 03 '10 at 13:13
  • 1
    FYI, this also works for user settings. I tried it, works great! – MPelletier Jul 16 '10 at 20:53
18

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.

Vyacheslav Enis
  • 1,676
  • 12
  • 17
15

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.

Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
1

I know this post is very old and has been marked as answered. But maybe I can save some time for people out there that tries the solution(s) mentioned above.

I am using VS2019 and as shown by AlanN and Sky Sanders, one is able to define the custom type, by entering either the full name manually, or by picking it from the list. The latter only works, when it was defined in another referenced assembly (don't forget to build it beforehand), which is a known limitation as explained here: DocMicrosoft: Create Application Settings using the designer.

My whole point of doing it that way was to give the user fixed options so that he does not have to check which strings are allowed and which are not. The problem is that the options do not consistently show up. Sometimes, after compiling your programm, you will not see the options anymore; they are gone. Sometimes, after restarting Visual Studio, they are back again. This problem was already stated by Sean in another stackoverflow post here.

BNB
  • 21
  • 3
0

I don't know if I have understood your question very well, but when I create a custom configuration section to use at my application configuration files I create a enum property using a TypeConverter attribute together with a EnumConverter.

I hope it helps, but let me know if I understood your question incorrectly.

CARLOS LOTH
  • 4,675
  • 3
  • 38
  • 44
  • I'm not experienced with the scenario you described, but I'd give the SettingsSerializeAsAttribute a chance. Try to apply it to your enum and pass the SettingsSerializeAs.String as the parameter. – CARLOS LOTH Feb 02 '10 at 14:14
0

Don't forget to add a reference to the project as the enum shall (!) not be in you main-project, it should be (!) somewhere else. Then where it says "string" in your settings-tab of the main-projects' properties, you can choose another option by clicking the drop-down box. Select "Browse" from the options. You will see some generic Microsoft options, but type your enum-name prefixed by its namespace such as Common.MyEnum. That should do the trick.

Feruncho
  • 1
  • 1