Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.
Recent comments

Extension methods are great way to add and use custom functionality on your objects.

So what is an Extension method?

In a short and simple describing: instead of creating a method that takes an instance of your object, applies your changes and then returns you the instance of that control, you can create an Extension method for that specific object to do the same thing but in a way of built-in within your object.

Example: if we had a TextBox that needed to have a valid text input:


<TextBox x:Name=”InputEmail/>

And you have:

string inputEmail = InputEmail.Text;

You can do:

If (inputEmail != “” && ValidateEmail(inputEmail) == true)
{
   .
   .
}
.
.
private bool ValidateEmail(string getInputEmail)
{
   .
   .
}

or you can do it in a much cleaner and easier to understand way using “IsEmailValid” extension, which extends the string:

if (inputEmail.IsEmailValid())
{

}

You can immediately notice from the above example that how much time and line of codes you will save by simply using Extensions instead of re-writing your custom methods to apply the same operation on a common object or element within a project that uses that feature in many places.

How to add an Extension method to Silverilght objects?

First we need to add a new static class that contains a static method for our new Extension method (IsEmailValid).

public static class FrameworkElementExtensionExtensions
{
    public static bool IsEmailValid (this string getInputEmail)
    {
        bool isMatched = true;
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        
        if (getInputEmail != “” && regex.IsMatch(s))
        {
            isMatched = false;
        }
        return regex.IsMatch(s);
    }
}

Note in the IsEmailValid method we used the keyword “this” before string getInputEmail, this simply means the IsEmailValid Extension method belongs to objects of type “string”.

To use the above extension method anywhere within your project, all you have to do is to import the reference to any of your classes, where you like the extension to be available, by adding “using FrameworkElementExtensionExtensions;” to that class.

Now you should even get the IsEmailValid method in your Visual Studio Intellisense when you add a dot after any string variable. So now you could simply do:

if (inputEmail.IsEmailValid()){
    HtmlPage.Window.Alert("Email was successfully submitted!");
}

Above example was a very simple example, I will update the example solution regularly to include more advanced examples for you to use (copy & paste!) in the future, so please check this post later for updates. Also, this example, along with more examples in the future, is available to download from CodePlex from here.

Posted by Damon Serji on 6. October 2009 09:37 under: Intermediate
 with 0 Comments

Comments

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview