Share via


IsolatedStorageSettings.Item Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets or sets the value associated with the specified key.

Namespace:  System.IO.IsolatedStorage
Assembly:  System.Windows (in System.Windows.dll)

Syntax

Public Property Item ( _
    key As String _
) As Object
public Object this[
    string key
] { get; set; }

Parameters

Property Value

Type: System..::.Object
The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element that has the specified key.

Implements

IDictionary<(Of <(TKey, TValue>)>)..::.Item[([(TKey])])

Remarks

This property provides the ability to access a specific element in the collection by using the following syntax:

myCollection["key"] (C#)

myCollection("key") (Visual Basic)

You can also use the Item property to add new elements by setting the value of a key that does not exist in the Dictionary<(Of <(TKey, TValue>)>). For example, use the following syntax to add a new element:

myCollection["myNonexistentKey"] = myValue (C#)

myCollection("myNonexistentKey") = myValue (Visual Basic)

However, if the specified key already exists in the Dictionary<(Of <(TKey, TValue>)>), setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.

A key cannot be nullNothingnullptra null reference (Nothing in Visual Basic), but a value can be if the value type TValue is a reference type.

Examples

The following example saves a string value from a TextBox named tbName to a user setting called name. It then accesses the saved name and uses it to display a personalized greeting in a TextBox named tbResults. If the name key does not exist, the "Hello, World" greeting is displayed instead. This example is part of a larger example for the IsolatedStorageSettings class.

userSettings("name") = tbName.Text
userSettings["name"] = tbName.Text;
' Retrieve and set user name.
Try
    Dim name As String = CType(userSettings("name"), String)
    tbGreeting.Text = "Hello, " & name
Catch ex As System.Collections.Generic.KeyNotFoundException
    ' No preference is saved.
    tbGreeting.Text = "Hello, World"
End Try
// Retrieve and set user name.
try
{
    string name = (string)userSettings["name"];
    tbGreeting.Text = "Hello, " + name;
}
catch (System.Collections.Generic.KeyNotFoundException)
{
    // No preference is saved.
    tbGreeting.Text = "Hello, World";
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

IsolatedStorageSettings Class

System.IO.IsolatedStorage Namespace