Click here to Skip to main content
15,896,912 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;
using System.IO;

// Part of RJConfig V1.3

// Classes for the File Variable

namespace RJConfig
{
	/// <remarks>
	/// The FileVariable class holds the Name and String value for a variable that can be 
	/// saved to and restored from a file.
	/// </remarks>
	public class FileVariable
	{
		/// <summary>
		/// The value string.
		/// </summary>
		protected string mData;
		/// <summary>
		/// The variable name.
		/// </summary>
		protected string mName;
		/// <summary>
		/// Public constructor.
		/// </summary>
		/// <param name="Name">Name of the variable.</param>
		/// <param name="Data">String representing the variable value.</param>
		public FileVariable (string Name, string Data)
		{
			mData = Data;
			mName = Name;
		}
		/// <summary>
		/// Public access property for the value string.
		/// </summary>
		public string Data
		{
			get
			{
				return mData;
			}
			set
			{
				mData = value;
			}
		}
		/// <summary>
		/// Public read only property for the Variable name.
		/// </summary>
		public string Name
		{
			get
			{
				return mName;
			}
		}
		/// <summary>
		/// Function to convert the string value to and from an int.
		/// </summary>
		public int IntValue
		{
			get
			{
				try {
					return Int32.Parse(mData);
				} catch {
					return 0;	// Returns the value 0 if the string can't be parsed as an int.
				}
			}
			set
			{
				mData = string.Format("{0}", value);
			}
		}
		/// <summary>
		/// Function to convert the string value to and from a bool.
		/// </summary>
		public bool BoolValue
		{
			get
			{
				return ((string.Compare(mData, "1") == 0) || (string.Compare(mData, "TRUE", true) == 0) || (string.Compare(mData, "ON") == 0));
				// false is returned for anything but true as above.
			}
			set
			{
				mData = string.Format("{0}", value ? "true" : "false");
			}
		}
	}
	/// <remarks>
	/// An extended LinkedList with FileVariables.
	/// FileVariables uses linked lists instead of dictionaries as ConfigVariables since there can
	/// be more than one FileVariable with the same name.
	/// </remarks>
	public class FileVariableList : LinkedList<FileVariable>
	{
		/// <summary>
		/// Find the first FileVariable with a specified name.
		/// </summary>
		/// <param name="VariableName">Name of the variable.</param>
		/// <returns>A FileVariable object for the first FileVariable found with the specified name.
		/// Null if no variable with that name exists.</returns>
		public FileVariable FindVariable (string VariableName)
		{
			if (string.IsNullOrEmpty(VariableName))
				return null;
			foreach (FileVariable fv in this) {
				if (fv.Name == VariableName)
					return fv;
			}
			return null;
		}
		/// <summary>
		/// Used to iterate through the linked list of FileVariables. Call FirstVariable to get
		/// the first FileVariable in the linked list.
		/// </summary>
		/// <param name="Node">A reference parameter for the node in the linked list. This must be used 
		/// for all subsequent calls to NextVariable</param>
		/// <returns>The first FileVariable in the linked list. Null if the list is empty.</returns>
		public FileVariable FirstVariable (ref LinkedListNode<FileVariable> Node)
		{
			Node = First;
			if (Node != null)
				return Node.Value;
			return null;
		}
		/// <summary>
		/// Used to iterate through the linked list of FileVariables. Must be preceded with a call
		/// to FirstVariable.
		/// </summary>
		/// <param name="Node">A reference parameter for the node in the linked list. This must be used
		/// for all subsequent calls to NextVariable</param>
		/// <returns>The next FileVariable in the linked list. Null if there are no more variables in
		/// the list.</returns>
		public FileVariable NextVariable (ref LinkedListNode<FileVariable> Node)
		{
			Node = Node.Next;
			if (Node != null) {
				return Node.Value;
			}
			return null;
		}
		/// <summary>
		/// Removes a FileVariable node from the linked list.
		/// </summary>
		/// <param name="fv">The FileVariable to be removed.</param>
		public void RemoveVariable (FileVariable fv)
		{
			Remove(fv);
		}
		/// <summary>
		/// Creates a FileVariable and adds it last to the linked list.
		/// Note that there can be more than one variable with the same name in the list.
		/// </summary>
		/// <param name="Variable">The name of the variable.</param>
		/// <param name="Data">The value, as a string, for the variable.</param>
		/// <returns>The newly created FileVariable object.</returns>
		public FileVariable AddVariable (string Variable, string Data)
		{
			FileVariable fv = new FileVariable(Variable, Data);
			AddLast(fv);
			return fv;
		}
		/// <summary>
		/// Find a named variable in the linked list and create it if it doesn't exist.
		/// This can only be used if there only should be variables with unique names in the list.
		/// </summary>
		/// <param name="Variable">Name of the variable.</param>
		/// <param name="Default">The default string value for the variable if it doesn't already exist.</param>
		/// <returns>The newly created or first found variable with the specified name.</returns>
		/// <exception cref="Exception">
		/// Thrown if the variable name is null or empty.
		/// </exception>
		public FileVariable FindVariableCreate (string Variable, string Default)
		{
			if (string.IsNullOrEmpty(Variable)) {
				throw new Exception("Variable name can not be null or empty!");
			}
			FileVariable fv = FindVariable(Variable);
			if (fv == null) {
				fv = new FileVariable(Variable, Default);
				AddLast(fv);
			}
			return fv;
		}
		/// <summary>
		/// Convert the linked list of FileVariables to XML nodes and add them to the 
		/// XML document, ready to be saved to file.
		/// </summary>
		/// <param name="xd">The XML document for the file the variables exists in.</param>
		/// <param name="xn">The XML node where the created child node should be added.</param>
		public void ToXmlDoc (XmlDocument xd, XmlNode xn)
		{
			XmlElement xeVar, xeData;
			foreach (FileVariable fv in this) {
				xeVar = xd.CreateElement("Variable");
				xeVar.SetAttribute("Name", fv.Name);
				xeData = xd.CreateElement("Value");
				try {
					xeData.InnerXml = fv.Data;	// If it can't be saved as an XML element 
				} catch {
					xeData.InnerText = fv.Data;
				}
				xeVar.AppendChild(xeData);
				xn.AppendChild(xeVar);
			}
		}
	}
}

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