views:

737

answers:

4

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.

Situation:
alt text

A: 

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
Please see my edit for clarification :)
MysticEarth
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
+4  A: 

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
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
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
FYI, this also works for user settings. I tried it, works great!
MPelletier
+2  A: 

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
As you can see in my post, I already have a serializable enum. That's the problem :)
MysticEarth
what is the problem? that is as close as you are going to get. ;-)
Sky Sanders
A: 

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