Tim Bostwick
Tim Bostwick

Reputation: 332

Solution Explorer for Visual Studio C# 2013 Express

I'm running VS 2013 Express targetting 4.5. I have a Windows Forms test app called ConfigMgrTest2. Two questions as I move around the Solution Explorer:

(1) Using the Settings Designer tool, I created an Application setting called 'applicationSetting1'. To access this setting, I need this syntax:

var r = ConfigMgrTest2.Properties.Settings.Default.applicationSetting1;

I don't understand why I need to call this through the Default property. Both Default and 'applicationSetting' are members of the Settings class. And 'Default' pertains to the 'defaultInstance' Why not call my 'applicationSetting' directly like this:

var r = ConfigMgrTest2.Properties.Settings.applicationSetting1;

(2) The Settings class is created in two parts. In the Settings.Designer.cs file, the Default property is created. In Settings.cs, the Settings event handlers are created. In one of my projects, I can see the Settings.cs file in Solution Explorer but in another, I seem to be missing this node.

Upvotes: 1

Views: 324

Answers (1)

Craig W.
Craig W.

Reputation: 18175

Your setting (applicationSetting1 in this case) is an instance member of the Settings class. The Default property represents an instance of that class via a Singleton pattern. In order to do what you want to do in your second example you would constantly need to be creating an instance of Settings in your code, for example:

var settings = new ConfigMgrTest2.Properties.Settings();
var r = settings.applicationSetting1;

This would be a pain for the develop and also potentially leave you with several instances of the class hanging around.

I just took a look in my copy of Visual Studio 2013 (Professional). I see the Settings.settings file, which is an XML file, and the Settings.Designer.cs file, which is the C# code that actually generates the Settings class code.

Upvotes: 1

Related Questions