Skip to main content
Email Password   helpLost your password?
Screenshot - HTMLExt.gif

Background

One thing many developers wrestle with is classes that don't have the features they want/need. How many times have you been working on a project and wished there was a .toHex() function built right into the Integer type?

A few developers have created wrapper classes, these allow you to add virtual functions to existing objects and extend let's say System.String with a new virtual method called .toPiglatin().

With .NET 3, you no longer need to use those extension methods, it's built right into the language. I am going to show you how to create a simple extension that's part of the color type and is called .toHTML().

Building the Extension Class

This could not possibly get any easier.

using System;

namespace HTMLExt
{
    public static class MyExtensions
    {
        public static string toHex(this System.Byte thisNumber)
        {
            return String.Format("{0:x2}", thisNumber).ToUpper();
        }

        public static string toHTML(this System.Drawing.Color thisColor)
        {
            return String.Format("#{0}{1}{2}", thisColor.R.toHex(), 
			thisColor.G.toHex(), thisColor.B.toHex());
        }
    }
}

This is the heart of the entire program, this is where you define methods that will extend existing objects. First you will need to name your namespace and make the MyExtensions static class.

The first is a simple .toHex() extension that will extend System.Byte so you can use statements like this:

string HexValue = Color.Red.R.toHex();

This will yield the expected result of FF.

The second extension is called .toHTML() and it extends System.Drawing.Color.
What this extension does is it formats all the .toHex() results as a valid HTML color and returns it so you can do stuff like this:

string HTMLColor = Color.Blue.toHTML(); 

This will yield the expected result of #0000FF.

Sample Application

What the sample application does is it takes the color you selected with the standard colorDialog and returns its HTML value. Here is the function that does it all:

private void button1_Click(object sender, EventArgs e)
{
    if (colorDialog1.ShowDialog() == DialogResult.OK)
        HtmlColor.Text = colorDialog1.Color.toHTML();
}

Pretty self explanatory, it displays a color dialog, then converts the selected color to HTML. Well, that's it for this brief article, I hope you have fun with this new technique. I know I have several uses in mind already.

Extensions.zip

These are some other extensions I wrote to start my core library of extensions:

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSmall bugs in Extension.zip Pin
amx3000
23:38 7 Nov '07  
General.NET 3.0 or C# 3.0? Pin
Mystic Taz
22:53 8 Oct '07  
GeneraltoUpper Pin
Laurent Muller
20:45 8 Oct '07  
GeneralRe: toUpper Pin
Matthew Hazlett
21:10 8 Oct '07  
GeneralObservation Pin
Matthew Hazlett
9:05 2 Oct '07  
GeneralNicely done! Pin
blackjack2150
1:42 2 Oct '07  
GeneralUse ColorTranslator Pin
Uwe Keim
20:19 1 Oct '07  
GeneralRe: Use ColorTranslator Pin
Matthew Hazlett
20:32 1 Oct '07  


Last Updated 7 Nov 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009