<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://codeforeternity.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Technology : C#</title><link>http://codeforeternity.com/blogs/technology/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Cool TrimText Extension Method for TextBox</title><link>http://codeforeternity.com/blogs/technology/archive/2008/03/03/cool-trimtext-extension-method-for-textbox.aspx</link><pubDate>Mon, 03 Mar 2008 08:01:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:87</guid><dc:creator>raj</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;Extension Methods are one of the most powerful features of .NET 3.5 and they can add a lot of flexibility to your code and help in cutting down your code size significantly if used wisely. I have been using a lot of Extension Methods in my code lately. One of them is the TrimText Extension Method for TextBox. While taking user input on any webform or winform (for example when users enter their First Name / Last Name), often, users accidentally add an extra space in the end. If you are a good coder, you would handle such common user mistakes by making sure to trim these input values before adding them to the database. Prior to .NET 3.5, we would have to write the below code to trim the TextBox text:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.Text = TextBox1.Text.Trim();&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.Text = TextBox1.Text.Trim()&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Now lets see how Extension Methods can help cut down the size of our above code. We create a simple TrimText Extension Method in a static class (C#) or module (VB) like below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;public static class ExtensionMethods&lt;br /&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void TrimText(this TextBox t)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;t.Text = t.Text.Trim();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;}&lt;/font&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;Imports System.Runtime.CompilerServices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;Public Module ExtensionMethods&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Extension()&amp;gt; _&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Sub TrimText(ByVal t As TextBox)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Text = t.Text.Trim()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;End Module&lt;/font&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Once we have our TrimText Extension Method in place, we can achieve the same task by simply calling the TrimText Extension Method from any TextBox object:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.TrimText();&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.TrimText()&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Notice how we have cut down the code and the code looks so much neater now ;-) I will post a few more examples of useful Extension Methods which I am using over the coming weeks. For those new to .NET 3.5, click&amp;nbsp;&lt;a class="" href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target="_blank"&gt;here&lt;/a&gt; to know more about Extension Methods.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=87" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Extension+Methods/default.aspx">Extension Methods</category></item></channel></rss>