<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/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>Huthaifa Afanah : Orcas</title><link>http://weblogs.asp.net/istofix/archive/tags/Orcas/default.aspx</link><description>Tags: Orcas</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Another simple use of extension methods!!</title><link>http://weblogs.asp.net/istofix/archive/2008/03/26/another-simple-use-of-extension-methods.aspx</link><pubDate>Wed, 26 Mar 2008 18:15:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6032792</guid><dc:creator>IsToFix</dc:creator><author>IsToFix</author><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/istofix/rsscomments.aspx?PostID=6032792</wfw:commentRss><comments>http://weblogs.asp.net/istofix/archive/2008/03/26/another-simple-use-of-extension-methods.aspx#comments</comments><description>&lt;div class="code"&gt;While my team and I doing our daily work. One of our new team members asked that he needs the user to select a value from drop-down list and this value should be converted to a nullable integer value. &lt;/div&gt;  &lt;p&gt;The small issue he faces that he need to parse the value selected in one line of code and make sure that if the user does not select a value the default value will be parsed to &lt;font color="#0000ff"&gt;null&lt;/font&gt; and do not cause a problem -in each drop-down there is a "[Select]" item with empty string value&amp;nbsp; -&lt;/p&gt;  &lt;p&gt;I wondered a &lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;font color="#000000"&gt;.Parse&lt;/font&gt; function returns &lt;font color="#0000ff"&gt;null&lt;/font&gt; when the parsed value is &lt;font color="#0000ff"&gt;null&lt;/font&gt;&amp;nbsp; but throws&amp;nbsp; "Input string was not in a correct format" exception when try to parse an empty string. And when use&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;font color="#000000"&gt;.TryParse&lt;/font&gt;&amp;nbsp; it will result with 0 value when parsing operation failed.&lt;/p&gt;  &lt;p&gt;In such simple scenario extension methods proof it self. Simply we can create an extension method call it SafeParse for example ....&lt;/p&gt;  &lt;div class="code"&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;font color="#000000"&gt;?&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;SafeParse(&lt;/font&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#2b91af"&gt;String&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Value)&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;font color="#000000"&gt;.IsNullOrEmpty(Value))&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#0000ff"&gt;null;&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#0000ff"&gt;else&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;font color="#000000"&gt;.Parse(Value)&lt;/font&gt;&lt;font color="#0000ff"&gt;;&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&amp;nbsp;&lt;/div&gt;  &lt;div class="code"&gt;&amp;nbsp;&lt;/div&gt;  &lt;div class="code"&gt;and then you could simply use it like this:&lt;/div&gt;  &lt;div class="code"&gt;&amp;nbsp;&lt;/div&gt;  &lt;div class="code"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;font color="#000000"&gt;?&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;MyNullableValue&lt;/font&gt;&lt;font color="#0000ff"&gt;;&lt;/font&gt;&lt;font color="#808080"&gt;     &lt;br&gt;&lt;/font&gt;&lt;font color="#000000"&gt;MyNullableValue&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&lt;font color="#808080"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;DropDownList1.SelectedValue.SafeParse()&lt;/font&gt;&lt;font color="#0000ff"&gt;;&lt;/font&gt; &lt;/div&gt;  &lt;div class="code"&gt;&amp;nbsp;&lt;/div&gt;  &lt;div class="code"&gt;Extension methods provide a simple and easy use which will ease programmer day day work, and make simple issues disappear just like 1,2,3&amp;nbsp; &lt;/div&gt;  &lt;div class="code"&gt;&amp;nbsp;&lt;/div&gt;  &lt;div class="code"&gt;Thanks for VS2008,thanks for extension methods, thanks for Microsoft team &lt;/div&gt;  &lt;div class="code"&gt;&amp;nbsp;&lt;/div&gt;  &lt;div class="code"&gt;Happy programming...!!&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6032792" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/istofix/archive/tags/.Net/default.aspx">.Net</category><category domain="http://weblogs.asp.net/istofix/archive/tags/VS2008/default.aspx">VS2008</category><category domain="http://weblogs.asp.net/istofix/archive/tags/Extension+Methods/default.aspx">Extension Methods</category><category domain="http://weblogs.asp.net/istofix/archive/tags/Orcas/default.aspx">Orcas</category></item></channel></rss>