using System; using System.Collections.Generic; namespace Woodpecker.Specialized.Enhancement { public abstract class AttributeSet { #region Fields /// /// A dictionary with string objects as keys and 'object' objects as values. /// protected Dictionary Attributes = new Dictionary(); #endregion #region Methods /// /// Sets a attribute with a value. If the attribute is already set, the old attribute will be removed. /// /// The attribute to set. /// The value of the attribute to set. public void setAttribute(string Attribute, object Value) { if (this.Attributes.ContainsKey(Attribute)) this.Attributes.Remove(Attribute); if(Value != null && Value != DBNull.Value) // Valid value all the way this.Attributes.Add(Attribute, Value); } /// /// Tries to unset a given attribute. /// /// The attribute to unset. public void unsetAttribute(string Attribute) { this.Attributes.Remove(Attribute); } /// /// Returns true if the attribute collection has a value set for a given attribute. /// /// The attribute to check. public bool hasSetAttribute(string Attribute) { return this.Attributes.ContainsKey(Attribute); } /// /// Tries to return the value of a given attribute as an object. Null is returned if the attribute is not set. /// /// The attribute to retrieve the value of. public object getAttribute(string Attribute) { if (this.hasSetAttribute(Attribute)) return this.Attributes[Attribute]; else return null; } /// /// Returns the value of a given attribute as a string. If the given attribute isn't set, a blank string is returned. /// /// The attribute to retrieve the value of. public string getStringAttribute(string Attribute) { object obj = this.getAttribute(Attribute); if (obj != null) return (string)obj; else return ""; } #endregion } }