#50 – Application-Scoped Properties

The Application class includes a property named Properties that is an IDictionary of properties, stored as keyword/value pairs.  You can use this dictionary to store any properties that should have application scope.

Property values can be read from or written to the Properties dictionary from any thread, in a thread-safe manner.

 private void Application_Startup(object sender, StartupEventArgs e)
 {
     this.Properties.Add("Debug", false);
     this.Properties.Add("Logger", null);

     // Set properties based on command line parameters
     foreach (string a in e.Args)
     {
         if (a.ToLower() == "/debug")
             this.Properties["Debug"] = true;
         else if (a.ToLower() == "/logging")
             this.Properties["Logger"] = new MyAppLogger("Logfile.txt");
     }
 }

Each entry in the dictionary is of type System.Collections.DictionaryEntry, so it has a Key property (type object) and a Value property (type object).  This means that you can use objects of any type as either keys or values.  You’ll most often use strings as keys and various objects as values.

Advertisements