Click here to Skip to main content
15,895,656 members
Articles / Programming Languages / C#

Class Library to Automatically Maintain Persistent Variables, Properties and Data Between Sessions

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
11 Jan 2008CPOL15 min read 44.1K   594   43  
A class library that maintains persistent variables, properties and data between sessions in XML files. It supports default values for new variables and events when the values in the XML file are changed
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;

// Part of RJConfig V1.3

// Classes for the Config Item which contains Config Variables.
namespace RJConfig
{
	/// <summary>
	/// Delegate signature for the ConfigFileItemChanged event. This event is raised when FileWatchEnabled
	/// is set to true and some external source has changed an element in the Item where the event is subscribed.
	/// </summary>
	/// <param name="sender">The Item instance where there has been a change.</param>
	/// <param name="e">CfgEventArgs</param>
	public delegate void ConfigFileItemChanged (object sender, CfgEventArgs e);
	/// <remarks>
	/// The class for an Item in the config/properties file. The Item contains Variables.
	/// The Item class is derived from the VariableDict class.
	/// </remarks>
	public class Item : VariableDict
	{
		/// <summary>
		/// Event that is raised when FileWatchEnabled is set to true and the Variabled in this Item
		/// has been changed in the file.
		/// </summary>
		public event ConfigFileItemChanged OnConfigFileItemChanged;
		/// <summary>
		/// Name of the Item
		/// </summary>
		protected string mName;
		/// <summary>
		/// Public constructor
		/// </summary>
		/// <param name="Name">Name of the Item</param>
		public Item (string Name)
		{
			mName = Name;
		}
		/// <summary>
		/// Public read only property for the name of the Item
		/// </summary>
		public string Name
		{
			get
			{
				return mName;
			}
		}
		/// <summary>
		/// This function is called for all Items for a Config object when its underlying file has been
		/// changed and FileWatchEnabled is set to true.
		/// If the Variables in this Item doesn't match the Variable in the Item in the file,
		/// the ConfigFileItemChanged event is raised.
		/// </summary>
		/// <returns>true if Variables in this Item doesn't match the Variables in the file.</returns>
		public bool CheckFileItemChange ()
		{
			bool ItemChanged = false;
			foreach (KeyValuePair<string, CfgVarNode> kvp in this) {
				if (kvp.Value.CheckFileVariableChange()) {
					ItemChanged = true;
				}
			}
			if (ItemChanged && (OnConfigFileItemChanged != null)) {
				OnConfigFileItemChanged(this, new CfgEventArgs());
			}
			return ItemChanged;
		}
	}
	/// <remarks>
	/// ItemDict is a class that extends a Dictionary for Items. The key is the name of the Item
	/// and the value is the Item object.
	/// </remarks>
	public class ItemDict : Dictionary<string, Item>
	{
		/// <summary>
		/// Create and add an Item object to the Item dictionary.
		/// The Item is only added if it doesn't already exist.
		/// The Item is identified by its name. All Items in one Section must have an unique name.
		/// </summary>
		/// <param name="ItemName">The name of the Item to be created</param>
		/// <returns>Returns the newly created Item object or the already existing Item with the specified name</returns>
		public Item AddItem (string ItemName)
		{
			Item i;

			if (!TryGetValue(ItemName, out i)) {
				i = new Item(ItemName);
				Add(ItemName, i);
			}
			return i;
		}
		/// <summary>
		/// Get the specified Variable in the specified Item from the Item dictionary.
		/// </summary>
		/// <param name="ItemName">Name of the Item where the variable exists.</param>
		/// <param name="VariableName">The name of the Variable.</param>
		/// <returns>A CfgVarNode object for the Variable. Null if the Variable and/or the Item doesn't exist</returns>
		public CfgVarNode FindVariable (string ItemName, string VariableName)
		{
			Item i;
			if (TryGetValue(ItemName, out i))
				return i.FindVariable(VariableName);
			else
				return null;
		}
		/// <summary>
		/// Save all Items in the dictionary to the underlying FileConf object.
		/// The actual saving is done by the Config class.
		/// </summary>
		public void SaveToFileConf ()
		{
			foreach (KeyValuePair<string, Item> kvp in this) {
				kvp.Value.SaveToFileConf();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Svep DesignCenter
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions