Saturday, August 4, 2012

Using Extension methods in Asp.Net 3.0 and higher .net framework

Since the dawn of asp.net, web based development has become a lot easier. It provides a great deal of control over designs and code. Let's say, one of them is Extension methods. It make it easy to add functionality to an existing type using a natural syntax. Also, since the extension methods appear in Visual Studio's IntelliSense drop-down list, they are easier to find and use than wrapper class equivalents. In this article, I show you how to create extension methods in asp.net.

Extension Methods in ASP.NET

At first, we need to create one class as below.
//Extensions.cs
namespace System
{
    public static class Extensions
    {
        public static int? ToNullInt(this string value)
        {
            int result;
            if (int.TryParse(value, out result))
            {
                return result;
            }
            return null;
        }
        public static int ToIntOrDefaultInt(this string value, int result = 0)
        {
            int.TryParse(value, out result);
            return result;
        }
        public static bool? ToNullBool(this string value)
        {
            bool result;
            if (bool.TryParse(value, out result))
            {
                return result;
            }
            return null;
        }
        public static bool ToBoolOrDefaultBool(this string value, bool result = false)
        {
            bool.TryParse(value, out result);
            return result;
        }
        public static string ToStringFormat(this int value)
        {
            return value.ToString("#,##0");
        }
        public static string ToStringFormat(this double value)
        {
            return value.ToString("#,##0.00");
        }
    }
}
we need an extension method to mark the function as static so that it can be accessed at any time without declaring object and secondly, mark the first parameter with the "this" keyword. This keyword basically tells the CLR that when this extension method is called, to use "this" parameter as the source.

Copy the following code to page load event in code-behind for using extension methods we created above.
string strVal = "12345";
int? nullableIntVal = strVal.ToNullInt();
//return 12345

int intVal = strVal.ToIntOrDefaultInt();
//return 12345

string blnVal = "True";
string blnVal2 = "Yes"; // can not parse
string blnVal3 = "1"; // can not parse
bool? nullableBool = blnVal.ToNullBool();
//return True

bool? nullableBool2 = blnVal2.ToNullBool();
//return Null since can not parse

bool? nullableBool3 = blnVal3.ToNullBool();
//return Null since can not parse

bool bln = blnVal.ToBoolOrDefaultBool();
//return True

bool bln2 = blnVal2.ToBoolOrDefaultBool();
//return False since can not parse

bool bln3 = blnVal3.ToBoolOrDefaultBool();
//return False since can not parse

string strIntFormat = intVal.ToStringFormat();
//return 12,345

double dblVal = 12345.67;
string strDblFormat = dblVal.ToStringFormat();
//return 12,345.67 
Now, run it again! I hope you guys those who are new to extension methods will able to write more better and reusable codes but I can not test it in Microsoft Visual Web Developer 2010 Express. Please drop a line if you know why. Thank.

No comments:

Post a Comment