views:

434

answers:

2

Hello everyone. 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

+3  A: 

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.

Waleed Al-Balooshi
Thanks so much, I have it working now. Tomorrow I will move onto trying XML, and learning Xelement.If I can get all that working, I can start on building my first proper app, some kind of weather app I think for Windows Phone.
Martin Anderson
@Martin Anderson, great I am happy that it worked for you.
Waleed Al-Balooshi
@Martin: See that big empty tick mark next to this answer? Click it. ;)
AnthonyWJones
A: 

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

Vijay Verma