Question

I am very new at all this c# Windows Phone programming, so this is most probably a dumb question, but I need to know anywho...

            IsolatedStorageSettings appSettings =
                IsolatedStorageSettings.ApplicationSettings;

        if (!appSettings.Contains("isFirstRun"))
        {
            firstrunCheckBox.Opacity = 0.5;

            MessageBox.Show("isFirstRun not found - creating as true");

            appSettings.Add("isFirstRun", "true");
            appSettings.Save();
            firstrunCheckBox.Opacity = 1;
            firstrunCheckBox.IsChecked = true;
        }
        else
        {
            if (appSettings["isFirstRun"] == "true")
            {
                firstrunCheckBox.Opacity = 1;
                firstrunCheckBox.IsChecked = true;
            }
            else if (appSettings["isFirstRun"] == "false")
            {
                firstrunCheckBox.Opacity = 1;
                firstrunCheckBox.IsChecked = false;
            }
            else
            {
                firstrunCheckBox.Opacity = 0.5;
            }
        }          

I am trying to firstly check if there is a specific key in my Application Settings Isolated Storage, and then wish to make a CheckBox appear checked or unchecked depending on if the value for that key is "true" or "false". Also I am defaulting the opacity of the checkbox to 0.5 opacity when no action is taken upon it.

With the code I have, I get the warnings

Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'

Can someone tell me what I am doing wrong. I have explored storing data in an Isolated Storage txt file, and that worked, I am now trying Application Settings, and will finally try to download and store an xml file, as well as create and store user settings into an xml file. I want to try understand all the options open to me, and use which ever runs better and quicker

Was it helpful?

Solution

If you explicitly cast the results of the value retrieval from the appSettings to string like this:

        if ((string)appSettings["isFirstRun"] == "true")
        {
            firstrunCheckBox.Opacity = 1;
            firstrunCheckBox.IsChecked = true;
        }
        else if ((string)appSettings["isFirstRun"] == "false")
        {
            firstrunCheckBox.Opacity = 1;
            firstrunCheckBox.IsChecked = false;
        }
        else
        {
            firstrunCheckBox.Opacity = 0.5;
        }

and that will make the warnings go away.

OTHER TIPS

IsolatedStorageSettings are stored as Dictionary. So in general you would need to cast it explicitly to whatever type you need to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top