3

Under Project > MyProject Properties > Settings I have an application setting named MyDouble with double type, the scope is User, the default value is 1.5.

I tried watching it, stepping through the entire application, and MessageBoxes to display the value. I've now commented out all uses of it excluding where I am trying to set a class (Form1) level variable. That looks like this:

    double myDouble = Properties.Settings.Default.MyDouble;

The value is always zero. If I MessageBox myDouble or the application setting the result is zero.

Some application settings are working. I called two message boxes in Form1_Shown event. One uses:

    Properties.Settings.Default.MyDouble.ToString() 

and the other uses:

    Properties.Settings.Default.MyInt.ToString()

The second one output the correct value.

This project is a WinForms application written in C#. Using Windows 10 and Visual Studio 2017. This project's form was copied over from a previous project, but I don't think that could affect it. The value in the previous project was the same. I typed these Application settings by hand. I've rechecked to ensure the spelling is correct. I deleted the setup project to no avail. Cleaned and rebuilt the solution... nada. I also checked the app.config file. I don't see anything wrong. Here it is:

    <userSettings>
        <MyProject.Properties.Settings>
            <setting name="MyDouble" serializeAs="String">
            <value>1.5</value>
            </setting>
        </MyProject.Properties.Settings>
    </userSettings>

Does anyone have any ideas why and/or how to fix this?

DavidG
  • 785
  • 8
  • 23
  • have you checked the scope of the setting? – michal krzych Sep 18 '17 at 14:48
  • @user12345 I did. I updated my question to reflect that it has a User scope. All application settings have user scope. – DavidG Sep 18 '17 at 14:50
  • When the default value is 1.5 and you get 0.0 then you've successfully saved the setting at least once. That it is not a value you like, well, that happens. You can edit the user.config file to patch around the (hopefully temporary) problem. It is just a bit hard to find back in c:\users\yourname\appdata\local\projectname\blablabla. – Hans Passant Sep 18 '17 at 14:59

1 Answers1

3

I think: something went wrong.

In Solution Explorer, double-click the .settings file. The default name for this file is Settings.settings. In the Settings designer, find the Name (MyDouble) of your setting. Each row represents a single setting.

See: Using Settings in C#.

You have:

<setting name="MyDouble" serializeAs="String">

And you write:

double myDouble = Properties.Settings.Default.MyDouble;

Convert String to double...may be it wrong?

See this page: Managing Application Settings (.NET) and this answer: What is the best way to store user settings for a .NET application?

Try this code:

Properties.Settings.Default.MyDouble = 1.5;
Properties.Settings.Default.Save();

and after it try this:

var myDouble = Properties.Settings.Default.MyDouble;
Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54
  • The settings.settings file was correct. The other double and integer values also say serialize s string. I have no idea what that means. Honestly, I've never even opened that file until now. The code you offered does fix it. I think somehow I was in a bug of visual studio. I ran your code which, of course, sets a new value, saves it. and now it works. without that code block included. Thanks. I'm selecting this as the correct answer since it did work once I tried forcing the application to overwrite the value and save. – DavidG Sep 18 '17 at 15:41