9

I've looked around and nothing has helped so far.

I currently have this code:

namespace InstaShot
{
    [Serializable]
    public class Hotkey
    {
        public uint Modifier { get; private set; }
        public int Key { get; private set; }
        public Hotkey(uint modifier, int key)
        {
            this.Modifier = modifier;
            this.Key = key;
        }
    }
}

Yet whenever I try and create a setting in my application settings, I get:

Type 'InstaShot.Hotkey' is not defined.

When clearly it is... It works for other classes inside the same namespace, just not that one... it's really annoying me now.

Steve Konves
  • 2,648
  • 3
  • 25
  • 44
user1769205
  • 141
  • 1
  • 2
  • 3
  • 5
    Are you trying to add an app.config configuration section? The problem you are having is a little unclear. – Tejs Oct 23 '12 at 18:22
  • Yeah, I want it to appear in app.config. – user1769205 Oct 23 '12 at 18:23
  • 1
    See how to create custom configuration sections http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx – Sergey Berezovskiy Oct 23 '12 at 18:24
  • 4
    Looks like you might want to follow this guide: http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx - Unless it's not posted, I don't see anything about a class inheriting from `ConfigurationSection`, etc... – Tejs Oct 23 '12 at 18:24
  • But I have another class that is similar, and I can use that in my app settings just fine, and it saves it to the config file perfectly, all these other methods are confusing and they seem pointless. – user1769205 Oct 23 '12 at 18:30
  • 1
    Can you give an example of one of these _other classes_? – Adam Kostecki Oct 23 '12 at 19:24

2 Answers2

39

Though the question author has likely moved on it is worth posting an answer in case others come across the same issue.

First let me clarify the question since comments on the question indicate there was some ambiguity. The OP is not asking about custom configuration sections. Rather, the question is about using standard settings--the settings pane you get when you open the Settings tab on the project properties in Visual Studio, as shown:

standard Visual Studio project settings

As to the answer, you must add a default constructor, i.e. a no-argument constructor where you assign default values. Once I did that, I was able to specify the type to get the result shown in the figure above. (Note that if your custom type is in the same assembly as your settings, it will not appear in the list of available types when you open the dropdown in the settings designer, nor--once you select browse--will it appear in that list either, but at that point you can type in the fully qualified name.)

Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
  • Can I have CustomType in Scope application? – Rand Random Jul 03 '15 at 14:04
  • 6
    How does one populate the "value" when the custom type comprises of two or more properties? Thanks. – Chris Walsh Aug 04 '15 at 08:32
  • 1
    Also note the default constructor must be public. – Tyler Daniels Sep 10 '15 at 20:50
  • 1
    FWIW - I had to make sure project compiled without errors then close VS, reopen before I could type in the name and be found. (VS2015) – Automate This Feb 10 '17 at 23:15
  • Another hurdle I had - maybe would help others - make sure the Class is *public* – Oded Ben Dov Mar 14 '19 at 08:18
  • Losing my mind here - got it working on sample WPF project. Can't bring the file into other project I'm working on. Tried rebuilding, reopening, re-verifying everything hundreds of times.... Is there some cache build that I'm missing? – Oded Ben Dov Mar 14 '19 at 09:19
  • I struggled with this and ended up including a library with my custom enum and referencing that in the Setting.Settings/Browse... was found and worked, where it did not work as part of the same class. – amalgamate Nov 06 '19 at 17:14
  • Though maybe too late for @ChrisWalsh, it seems people want an answer for populating default values in a custom Settings type. See [this answer](https://stackoverflow.com/a/6470015/3791245) for how: give your type a custom TypeConverter that translates a string into the type. Then you can enter the default values as a string in the designer, which the TypeConverter converts for you (assuming you formatted the string in a way the TypeConverter likes). – Sean Skelly Oct 21 '20 at 23:53
  • FWIW: I took the idea from the previous comment and answered a similar question. That answer is a [how-to guide for setting default values of custom Settings types](https://stackoverflow.com/a/64470058/3791245). – Sean Skelly Oct 22 '20 at 00:26
1

Here is a more common solution, which can be used for collections as well: press View code in Settings to open Settings.cs

Add your property this way:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("")]
    public YourType PropertyName
    {
        get => (YourType)this["PropertyName"];
        set => this["PropertyName"] = value;
    }

YourType here can be collection like List<object>. DefaultSettingValueAttribute is used to initialize with a default constructor, else it will be null as default value.

arteny
  • 339
  • 1
  • 4
  • 14
  • I didn't care about getting the custom type into the Settings Designer, so this did the trick. It makes perfect sense - just add the property to the Settings class. – Jacob Robbins May 11 '21 at 14:31