brady gaster

yadnb

"there is no box," the wise man said

i'm going to have to pick on a good pal in this post. in hopes, i guess, to point out a nice way of solving programming problems by using (unfortunately through a good pal of mine who totally rocks as a coder, businessman, and architect) a little dose of old-fashioned guesswork. sometimes, following up on “i hate that this class lacks XYZ functionality” with a hefty dose of patience and creativity can help you accomplish precisely what you wanted, all through the process of simple inheritance and extension.

my buddy Robert has one of these rather legitimate complains about certain objects in the framework. or in this case, the old framework. early in my .net development i began to miss the old MSXML property, “Xml.” remember for a moment how this property works - it simply returns all of the XML - similar to the OuterXml property in the XmlDocument object. basically, i extend the XmlDocument object and add an additional property to it. the simplest kind of inheritance-provided functionality is defined below in the code for this discussion:

public class MyXmlDocument : System.Xml.XmlDocument
    {
        public string Xml
        {
            get
            {
                return this.OuterXml;
            }
        }
    }

see, not too difficult, when you see it all in action, eh? so - let's get to the culprit for this whole idea for a post. Robert's desire for there to be a property on the DropDownList object that returned the SelectedItem's Value property. Here ya go:

public class MyDropDownList : System.Web.UI.WebControls.DropDownList 
    {
        public string SelectedValue
        {
            get
            {
                if(this.SelectedItem != null)
                    return this.SelectedItem.Value;
                return null;
            }
        }
    }

so, through picking on a money guy, super developer, ingenious architect and land-speed-record-holder-code, i've explained how a little trickery via inheritance can, in a sense, give you just what you asked for (or didn't think to ask for!).

Comments

JosephCooney said:

This always annoys me too. "Blah doesn't have X" - in many cases implementing X is not that hard. Sure, it's nice if the framework does all the work for you (and in .NET it so often does), but ultimately you are the master of your own destiny. In some cases (like the sealed imagelist class in winforms) there is no way around the problem, but those are few and far between. Also sometimes when you try and implement X for yourself you see why it was not such a great idea to begin with. Truly there is no box, except maybe for Don.
# July 28, 2003 11:20 PM

Robert McLaws said:

What I was mad about was that the SelectedItem.Value property was not settable, and that there was no documentation in 1.0 on how to do so. Once I figured it out, I created an identical property, and added get AND set information, and turned it into a custom server control for my future 1.0 projects.

I also talk about this process in an article for Builder.com that I wrote about 2 months ago, and will be published soon.

Thank you for the kind words Brady :).
# July 29, 2003 12:48 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)