The triplet class
posted by Kevin Wolff on 26 Jan 2007

I used the triplet class for the first time today!  Yeeeeha!  The triplet class is a little known class in the System.Web.UI namespace that is predominantly used for viewstate.  Ever since I heard of it I've been dying to try it out.  It served the purpose very well.  It would be really cool if there was a typed dictionary version of the triplet but it works well enough as is so I can get by.

My purpose in using the triplet is in applying stylesheets to a page.  I store a key to make sure the stylesheet doesn't get applied twice, the path to the stylesheet and the media property for the stylesheet.

The AddStyleSheet method looks like this (:

public void AddStylesheet(string key, string path, string media)
{
    foreach (Triplet t in styleSheetList)
    {
        if (t.First.ToString().ToLower() == key) { return; }
    }
    styleSheetList.Add(new Triplet(key.ToLower(), path, media));
}

I would normally have opted to use a typed dictionary but I needed to store the media property as well.  I use a dictionary for adding javascript files to the page like so:

public void AddJavascriptFile(string key, string scriptFile)
{
    if (!javscriptFile.ContainsKey(key))
    {
        javscriptFile.Add(key, scriptFile);
    }
}

This is a much more elegant solution since I don't need to iterate through the list.  I guess I could have used a dictionary with the stylesheets except I would have had to joing the path and media property together with a seperator char and then split them again when I pull them out for use.  But... I really just wanted to find a use for the triplet class.  Now I have, and I like it.

2 comment(s)  | viewed 339 time(s)
Tags: Code

Comments

Kevin Wolff - 26 Jan 2007 1:31 PM
I should probably make mention that I learned about this class from one of my favorite programming gurus Mads Kristensen.
http://www.madskristensen.dk/blog/The+Class+You+Didnt+Know+Existed+.aspx
Rob Miller - 30 Jun 2009 9:20 PM
Cool site.
Name:
Email:
Comment:

Tags

Code,  Bug,  Update,  Tutorial