Developing for Modern Windows

Tips, tricks, and guides for developing on modern Windows platforms

Encapsulate an isolated storage setting in a variable

This is a simple tip that works with both Windows Phone and Windows 8. Dealing with isolated storage settings can be a bit cumbersome. Wouldn’t it be easier if you could treat a stored setting or value like a regular variable?

This tip shows you how to do that using Visual in Windows 8 and Windows Phone apps.

What does this tip achieve?

Instead of directly interacting with a setting stored in your app’s isolated storage, this technique lets you treat your settings value like any other variable (or more specifically a property).

Compare the following code for changing the value of the stored (i.e. persistent in isolated storage) NumberOfLives:

NumberOfLives = x + 1

As compared to:

ApplicationData.Current.RoamingSettings.Values("settingname") = "hello world"

Note: you must also make sure the isolated storage setting is created before trying to write to it, or you’ll get an error.

Reading the value:

X = NumberOfLives


X =  ApplicationData.Current.RoamingSettings.Values("settingname")

As you can see, encapsulating the isolated storage value as a property makes the rest of your code easier to write, read, and maintain.

How is the property set up?

The beauty of this technique is that it is very flexible. Properties give you the opportunity to create variables that contain their own internal logic. In this example we’ll just create a very simple property that reads and writes a specified isolated storage value.

Private _numberOfLives as Integer
Public Property NumberOfLives as Integer
Return Convert.ToInteger(ApplicationData.Current.RoamingSettings.Values("NumberOfLivesStored")
End Get
_numberOfLives = value
ApplicationData.Current.RoamingSettings.Values("NumberOfLivesStored") = _numberOfLives.ToString
End Set
End Property

A detailed look at the code

Let’s look at the code line by line so you can understand exactly what is happening here.

  1. This line sets up a private variable that is used internally by the property. Code outside of the property doesn’t access this. It’s named the way it is as a conventional way to mirror the property’s own name.
  2. This declares the property as an integer (whole number) variable.
  3. Get marks the start of the code that is executed when the property’s value is being requested (i.e. read).
  4. The return line hands over the value in isolated storage as the result of the Get. This is, in effect, the value of NumberOfLives, but rather than being stored as a variable it has been generated by the Get logic (by reading it from the isolated storage setting).
  5. End Get ends the Get block of code
  6. Set marks the code for setting the value of the property. It is called when the code assigns a value to NumberOfLives, e.g. NumberOfLives = 3.
  7. In this line the variable _numberOfLives is set to “value”. Value is the value that was given to the property to set its value. So in the example above, “value” = 3.
  8. This line takes the value and stores it in the isolated storage key.
  9. End Set ends the set block of code
  10. End Property finishes off the property code.

Is that it?

If you add the code above into your project it will most likely fail! The code is correct, but you’re missing something. To work with isolated storage settings you need to create them before reading or writing their values.

There are two obvious ways to do this. The most foolproof way is to always check if the property exists before reading or writing to it. You could alternatively implement logic in your project that creates all isolated storage properties before they are ever used.

In a future post I’ll put up a full implementation of settings that incorporates logic to check if isolated storage settings exist before attempting to read or write them.

How do I use this?

Once you implemented properties in this way they can be used just like regular variables…except their values will persist when the app is closed! This is ideal for storing application settings that need to accessed by your code a lot.

Leave a Reply

Your email address will not be published.