How to: Get and Set Application-Scope Properties

This example shows how to both get and set application-scope properties using Properties.

Example

Application exposes a data store for properties that can be shared across an AppDomain: Properties.

The property data store is a dictionary of key/value pairs that can be used like so:

      ' Set an application-scope property
      Application.Current.Properties("MyApplicationScopeProperty") = "myApplicationScopePropertyValue"
// Set an application-scope property
Application.Current.Properties["MyApplicationScopeProperty"] = "myApplicationScopePropertyValue";
      ' Get an application-scope property
      ' NOTE: Need to convert since Application.Properties is a dictionary of System.Object
      Dim myApplicationScopeProperty As String = CStr(Application.Current.Properties("MyApplicationScopeProperty"))
// Get an application-scope property
// NOTE: Need to convert since Application.Properties is a dictionary of System.Object
string myApplicationScopeProperty = (string)Application.Current.Properties["MyApplicationScopeProperty"];

There are two considerations to make when using Properties. First, the dictionary key is an object, so you need to use exactly the same object instance when both setting and getting a property value (note that the key is case-sensitive when using a string key). Second, the dictionary value is an object, so you will need to convert the value to the desired type when getting a property value.

Because the dictionary value is an object, you can just as easily use custom types as simple types, like so:

      ' Set an application-scope property with a custom type
      Dim customType As New CustomType()
      Application.Current.Properties("CustomType") = customType
// Set an application-scope property with a custom type
CustomType customType = new CustomType();
Application.Current.Properties["CustomType"] = customType;
      ' Get an application-scope property
      ' NOTE: Need to convert since Application.Properties is a dictionary of System.Object
      Dim customType As CustomType = CType(Application.Current.Properties("CustomType"), CustomType)
// Get an application-scope property
// NOTE: Need to convert since Application.Properties is a dictionary of System.Object
CustomType customType = (CustomType)Application.Current.Properties["CustomType"];

See Also

Reference

IDictionary