Click here to Skip to main content
Click here to Skip to main content

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

, 4 Apr 2011
Rate this:
Please Sign up or sign in to vote.
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).
 
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)

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberjfriedman13-Mar-13 16:17 
Questionmy vote of #5 PinmemberBillWoodruff3-Aug-12 12:19 
Questionnice Pinmemberzenwalker198523-Mar-12 21:05 
GeneralReason for my vote of 5 What a great message! Thanks for sha... Pinmemberdmjm-h12-Apr-11 6:10 
GeneralI'll 5 this because to admit and delete is far more venerabl... PinmemberS Houghtelin12-Apr-11 2:07 
GeneralI hate it when that happens... Although you always learn som... PinmemberNaerling11-Apr-11 20:35 
GeneralIt must be easier to man up to your mistakes when you are 10... Pinmemberwizardzz7-Apr-11 8:09 
GeneralBrilliant! PinmemberPhilip Liebscher6-Apr-11 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 5 Apr 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid