Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Admit When You're Wrong, Delete the Code, and Move On

4.89/5 (18 votes)
5 Apr 2011CPOL2 min read 22.5K  
What to do when the you're a moron epiphany smacks you in the head just after you've convinced yourself you're pretty clever.
After starting to write this tip/trick, I realized I don't actually need this code, but I figure since I spent all morning on it, it's a shame to delete it without giving someone else a chance to laugh at me, so here it is.

----------------------

I've been working with the Silverlight4 RichTextBox, and it has an interesting property called Xaml. This property returns a string that represents the xaml code used to build the content of the RichTextBox. Also interesting is that no other control has such a property, yet in order to display certain components in a RichTextBox, you need the Xaml to display it as content. One of the components I need to display is a multicolumn Grid.

The grid is built in a child window in a ListBox (this is the point at which I realized I didn't need the code in this tip), and when the user clicks the OK button, the child window iterates through the ListBox items (sort of), and builds the Xaml necessary to display the ListBox with it's items. As you can probably guess by now, I don't need the items contained by a ListBox, I need them in a multicolumn grid. But I digress from my prior digression.

Essentially, I build a collection of xaml elements with their attributes and child elements, and when the time is right, I call the GetXaml() method. (And this is where I realized the whole thing was a waste of time because I could have just saved a collection of strings that represented the xaml in question.)

So, here you see a completely worthless lump of code that I spent about four hours on, that is going to be deleted from our project. I guess the tip here is not to be afraid to admit when you've wasted a whole morning on code that is not only over-complicated, but isn't even necessary. I've been coding for 30 years, and I still do this kind of stuff.

So, for your amusement, I present the most worthless code in the universe (and only because I'd spent about 10 minutes formatting it for CP).

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;

///////////////////////////////////////////////////////////////////////////////////
public class AttributeCollection : Dictionary<string, string>
{
}

///////////////////////////////////////////////////////////////////////////////////
public class ElementCollection : List<XamlElement>
{
}

///////////////////////////////////////////////////////////////////////////////////
public class XamlElement
{

    private string m_name  = "";
    private string m_value = "";
    private ElementCollection   m_children   = new ElementCollection();
    private AttributeCollection m_attributes = new AttributeCollection();

    //..............................................................................
    /// <summary>
    /// Get/set the name of the xaml element (suchs as "ListBox")
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <remarks></remarks>
    public string Name 
    {
        get { return m_name; }
        set { m_name = value; }
    }

    //..............................................................................
    /// <summary>
    /// Sets the value that appears between the element tags (causes the Children list 
    /// to be cleared of items)
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <remarks></remarks>
    public string Value 
    {
        get { return m_value; }
        set { m_value = value; }
    }

    //..............................................................................
    /// <summary>
    /// The list of child xaml elements (XamlElement). When this collection has an item 
    /// added, the Value property is set to an empty string
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <remarks></remarks>
    public ElementCollection Children 
    {
        get { return m_children; }
        set { m_children = value; }
    }

    //..............................................................................
    /// <summary>
    /// The collection of attributes for this xaml element
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <remarks></remarks>
    public AttributeCollection Attributes 
    {
        get { return m_attributes; }
        set { m_attributes = value; }
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Default constructor
    /// </summary>
    /// <remarks></remarks>
    public XamlElement()
    {
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Use this constructor if all you have is the element name
    /// </summary>
    /// <param name="nameValue"></param>
    /// <remarks></remarks>
    public XamlElement(string nameValue)
    {
        this.Name = nameValue;
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Use this constructor if you have a name and a value for this xaml element
    /// </summary>
    /// <param name="nameValue"></param>
    /// <param name="text"></param>
    /// <remarks></remarks>
    public XamlElement(string nameValue, string text)
    {
        this.Name  = nameValue;
        this.Value = text;
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Add an attribute for this xaml element
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <remarks></remarks>
    public void AddAttribute(string key, string value)
    {
        if ((this.Attributes == null)) 
        {
            this.Attributes = new AttributeCollection();
        }
        this.Attributes.Add(key, value);
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Adds the specified key/value pair object to the attributes collection
    /// </summary>
    /// <param name="keyValue"></param>
    /// <remarks></remarks>
    public void AddAttribute(KeyValuePair<string, string> keyValue)
    {
        if ((this.Attributes == null)) 
        {
            this.Attributes = new AttributeCollection();
        }
        this.Attributes.Add(keyValue.Key, keyValue.Value);
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Adds a child element to the Children collection. If text content has previously 
    /// been specified, it is deleted because because you can't have text content and 
    /// children in a xaml element.
    /// </summary>
    /// <param name="element"></param>
    /// <remarks></remarks>
    public void AddChild(XamlElement element)
    {
        if ((this.Children == null)) 
        {
            this.Children = new ElementCollection();
        }
        this.Children.Add(element);
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Adds content to the xaml element. If any children have previously been specified, 
    /// they are removed because you can't have text content and children in a xaml 
    /// element.
    /// </summary>
    /// <param name="text"></param>
    /// <remarks></remarks>
    public void AddTextContent(string text)
    {
        if ((this.Children == null)) 
        {
            this.Children = new ElementCollection();
        }
        this.Children.Clear();
        this.Value = text;
    }

    //-----------------------------------------------------------------------------
    /// <summary>
    /// Retrieves the xaml for this xaml element, including all children and attributes.
    /// </summary>
    /// <returns></returns>
    /// <remarks></remarks>
    public string GetXaml()
    {
        StringBuilder xaml = new StringBuilder("");

        // start the element, and add it's attributes
        xaml.AppendFormat("<{0}", this.Name);
        foreach (KeyValuePair<string, string> attribute in this.Attributes) 
        {
            xaml.AppendFormat(" {0}=\"{1}\"", attribute.Key, attribute.Value);
        }
    
        // now append the element's children (or text content), as well as the 
        // appropriate element closing tag
        if (this.Children.Count > 0) 
        {
            xaml.Append(">");
            foreach (XamlElement child in this.Children) 
            {
                xaml.Append(child.GetXaml());
            }
            xaml.AppendFormat("</{0}>", this.Name);
        } 
        else if (!string.IsNullOrEmpty(this.Value)) 
        {
            xaml.AppendFormat(">{0}</{1}>", this.Value, this.Name);
        } 
        else 
        {
            xaml.Append("/>");
        }
        return xaml.ToString();
    }
}

License

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