<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iDevForFun &#187; DotNet</title>
	<atom:link href="http://www.idevforfun.com/index.php/category/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.idevforfun.com</link>
	<description>.NET Development</description>
	<lastBuildDate>Fri, 13 Jan 2012 20:04:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Code Re-use With Abstract Classes</title>
		<link>http://www.idevforfun.com/index.php/2012/01/13/code-re-use-with-abstract-classes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=code-re-use-with-abstract-classes</link>
		<comments>http://www.idevforfun.com/index.php/2012/01/13/code-re-use-with-abstract-classes/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 15:35:22 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[abstract classes]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[object-oriented programming]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=215</guid>
		<description><![CDATA[TweetAn often overlooked feature of C# (and OO programming in general) is the use of abstract classes to maximise code re-use. Put simply, if you have code in related classes that is the same but needs to call implementation specific methods, you can probably use an abstract class to consolidate this code. Lets dive right ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton215" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2012%2F01%2F13%2Fcode-re-use-with-abstract-classes%2F&amp;via=Lawnmower_Man&amp;text=Code%20Re-use%20With%20Abstract%20Classes&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2012%2F01%2F13%2Fcode-re-use-with-abstract-classes%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>An often overlooked feature of C# (and OO programming in general) is the use of abstract classes to maximise code re-use. Put simply, if you have code in related classes that is the same but needs to call implementation specific methods, you can probably use an abstract class to consolidate this code. Lets dive right in with a simple example. </p>
<p>This is production code that has been stripped back to highlight the use of abstraction. In my scenario I had a number of user controls that had to implement the same functionality to each display three different views. The initialisation code ran through the same method calls in each control but the implementations of the called methods were different. Enter the abstract class &#8230;</p>

<div class="wp_codebox"><table><tr id="p2153"><td class="code" id="p215code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">class</span> ControlBase 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ViewMode <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetSaveButtonEnabled<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetCollapsedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetSummaryView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetExpandedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> HideAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> InitControl<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        SetViewMode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        SetSaveButtonEnabled<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetViewMode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        HideAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>ViewMode<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">:</span>
                SetCollapsedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">:</span>
                SetSummaryView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">:</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">:</span>
                SetExpandedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The InitView() and SetViewMode() methods are the same across all controls. Obviously the actual code to render the correct view is specific to the implementation of the control itself. So how does it work? Firstly, an abstract class can never be instantiated &#8230; it can only be inherited from. Any methods specific to the implementation are declared as abstract. In the abstract base class we know what the method signature should be but we can&#8217;t implement it at this level. Once the signature is provided and declared abstract we can call that method in the abstract class code as demonstrated in the SetViewMode() and InitControl() methods. At runtime this will result in the implementation of the method in the child class being called. </p>
<p>Our inherited class would look something like this &#8230;</p>

<div class="wp_codebox"><table><tr id="p2154"><td class="code" id="p215code4"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Control1 <span style="color: #008000;">:</span> ControlBase
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetSaveButtonEnabled<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetCollapsedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetSummaryView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetExpandedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> HideAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the child class we override the abstract methods. Failing to override all the abstract methods on the base class will result in a compile time error unless your child class is also abstract. </p>
<p>That&#8217;s really all there is to it. By using this technique I saved having the InitControl() and SetViewMode() methods being implemented with the exact same code in all of my child controls. This would at best have been copy-paste coding which is horrible and to be avoided at all times. A bug in one of those methods would have resulted in needing to update all the implementations to fix it. Add the complexity of working in a team and another team member may not realise that the code is repeated in multiple controls. By using the abstract class there is only one implementation to maintain making maintenance much simpler while allowing a much more object oriented design. </p>
<p>More info on <a href="http://msdn.microsoft.com/en-us/library/sf985hc5.aspx" title="MSDN" target="_blank">MSDN</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2012/01/13/code-re-use-with-abstract-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Radio Buttons and Repeaters in Aspx</title>
		<link>http://www.idevforfun.com/index.php/2011/12/12/radio-buttons-and-repeaters/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=radio-buttons-and-repeaters</link>
		<comments>http://www.idevforfun.com/index.php/2011/12/12/radio-buttons-and-repeaters/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 16:04:44 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[radio button]]></category>
		<category><![CDATA[repeater]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=190</guid>
		<description><![CDATA[TweetIf you&#8217;re using a Repeater control and have a requirement to have radio buttons in the repeated rows you may find yourself in a head scratching mess. You may find that checking one radio button does not result in all other buttons being unchecked. The radio buttons will essentially start to behave like a bunch ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton190" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F12%2F12%2Fradio-buttons-and-repeaters%2F&amp;via=Lawnmower_Man&amp;text=Radio%20Buttons%20and%20Repeaters%20in%20Aspx&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F12%2F12%2Fradio-buttons-and-repeaters%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>If you&#8217;re using a Repeater control and have a requirement to have radio buttons in the repeated rows you may find yourself in a head scratching mess. You may find that checking one radio button does not result in all other buttons being unchecked. The radio buttons will essentially start to behave like a bunch of check boxes &#8230; not cool. </p>
<p>With normal html radio buttons we group them together using the &#8216;name&#8217; attribute. All radio buttons with the same value in the name attribute become mutually exclusive resulting in the familiar behaviour where only one radio button in that group can be checked at any one time. In an asp:RadioButton control the name attribute maps to the GroupName parameter which normally works fine. </p>
<p>Things start to go astray when the radio buttons are added to a repeater however. You may be aware that asp.net will insert its own unique id for each control which is not the same as the id you originally gave it in code or markup. It does this to ensure that any elements that have the same id end up with a unique id once rendered. In the case of the repeater this is very necessary as you are taking the same elements with the same id and emitting them multiple times. Unfortunately a repeater also mangles the name attribute of the controls. Normally this can be safely ignored but radio buttons rely on this attribute to work out which group they belong to. All of a sudden each radio button belongs to its own little group and so we end up with check box behaviour. </p>
<p>This is a known issue explained in detail <a href="http://support.microsoft.com/kb/316495" title="here" target="_blank">here</a>, however, Microsoft have not yet fixed this and give no indication of if or when it will be fixed.</p>
<p>So now what? Well &#8230; there are a couple of work-arounds, none of which are particularly elegant. </p>
<p>1) Avoid using the repeater or radio buttons and see if you can solve the problem with other controls. </p>
<p>2) If you&#8217;re stuck with repeaters and radio buttons the most elegant solution is to use javascript to de-select other radio buttons using the click event. </p>
<p>This goes in the &lt;head&gt; tag &#8230;</p>

<div class="wp_codebox"><table><tr id="p1907"><td class="code" id="p190code7"><pre class="javascript" style="font-family:monospace;">&nbsp;
 <span style="color: #006600; font-style: italic;">//I use jquery to make life easier but you don't have to </span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #003366; font-weight: bold;">function</span> MakeRbExclusive<span style="color: #009900;">&#40;</span>rb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rbs <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#rbData :radio'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> rbs.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rbs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> rb<span style="color: #009900;">&#41;</span> rbs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>And our repeater &#8230;</p>

<div class="wp_codebox"><table><tr id="p1908"><td class="code" id="p190code8"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;rbData&quot;&gt;
    &lt;asp:Repeater ID=&quot;repeater&quot; runat=&quot;server&quot;&gt;
        &lt;ItemTemplate&gt;
            &lt;!-- I have bound to simple Dictionary&lt;string, string&gt; created in page load for example purposes --&gt;
            &lt;asp:Label ID=&quot;lbl&quot; runat=&quot;server&quot; AssociatedControlID=&quot;radio&quot;&gt;&lt;%#Eval(&quot;key&quot;)%&gt;&lt;/asp:Label&gt;
            &lt;asp:radioButton ID=&quot;radio&quot; runat=&quot;server&quot; value=&lt;%#Eval(&quot;value&quot;)%&gt; onclick=&quot;MakeRbExclusive(this);&quot;/&gt;
            &lt;br /&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:Repeater&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p><br/><br />
3) Avoid the use of a repeater completely and add your controls dynamically from code behind in a loop to achieve the same result. I *really* don&#8217;t recommend this. It is finicky at best and takes very careful coding. Dynamically added controls are not automatically tracked in viewstate and can create big problems if not carefully coded. I have had to do this in the past but it is definitely an option I&#8217;d only consider once I have exhausted all others. </p>
<p><br/>Happy coding! <img src='http://www.idevforfun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/12/12/radio-buttons-and-repeaters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic Singelton</title>
		<link>http://www.idevforfun.com/index.php/2011/06/09/generic-singelton/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=generic-singelton</link>
		<comments>http://www.idevforfun.com/index.php/2011/06/09/generic-singelton/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 13:55:21 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=126</guid>
		<description><![CDATA[TweetWe are often required to implement the singleton pattern in our code. While doing this yet again I thought there must be a generic way to do this and I came up with the following code &#8230; public static class Singleton&#60;T&#62; where T : class, new&#40;&#41; &#123; private static volatile T _instance; private readonly static ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton126" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F09%2Fgeneric-singelton%2F&amp;via=Lawnmower_Man&amp;text=Generic%20Singelton&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F09%2Fgeneric-singelton%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>We are often required to implement the singleton pattern in our code. While doing this yet again I thought there must be a generic way to do this and I came up with the following code &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12611"><td class="code" id="p126code11"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> volatile T _instance<span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> _lockObj <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T Instance
   <span style="color: #008000;">&#123;</span>
       get
       <span style="color: #008000;">&#123;</span>
           <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
              <span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span>_lockObj<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> _instance <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
           <span style="color: #0600FF; font-weight: bold;">return</span> _instance<span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
This allows you to use any class as a singleton &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12612"><td class="code" id="p126code12"><pre class="csharp" style="font-family:monospace;">var mySingleton <span style="color: #008000;">=</span> GenericSingleton<span style="color: #008000;">&lt;</span>MySingleton<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><br/><br />
It&#8217;s not perfect &#8230; there is nothing stopping the developer from creating a new instance of the class which breaks the singleton pattern. Doing it the normal way makes it impossible to create more than one instance of a class but this way allows the flexibility to use a class as a singleton even if it&#8217;s not implemented that way and also allows a mix of singleton and instances of the same class.</p>
<p>In any event it makes interesting use of generics and I just thought it was cool so I thought I&#8217;d share <img src='http://www.idevforfun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/06/09/generic-singelton/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Viewstate is not evil</title>
		<link>http://www.idevforfun.com/index.php/2011/05/15/viewstate-is-not-evil/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=viewstate-is-not-evil</link>
		<comments>http://www.idevforfun.com/index.php/2011/05/15/viewstate-is-not-evil/#comments</comments>
		<pubDate>Sun, 15 May 2011 16:46:41 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=129</guid>
		<description><![CDATA[TweetViewstate is often touted to be heavy and slow and better off disabled in most aspx applications. When used properly veiwstate can be quite a useful tool with not too much overhead. Use it improperly and all the horror stories you&#8217;ve heard start to come true &#8230; If you view page source in an aspx ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton129" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F15%2Fviewstate-is-not-evil%2F&amp;via=Lawnmower_Man&amp;text=Viewstate%20is%20not%20evil&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F15%2Fviewstate-is-not-evil%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Viewstate is often touted to be heavy and slow and better off disabled in most aspx applications. When used properly veiwstate can be quite a useful tool with not too much overhead. Use it improperly and all the horror stories you&#8217;ve heard start to come true &#8230;</p>
<p>If you view page source in an aspx application that has viewstate enabled site wide you will see the hidden __VIEWSTATE field will have a long encoded string of data. This is quite heavy and takes some time to trasmit betweeen the server and client. Our aim is to minimise the length of this data load to the point where overhead is no longer a concern. </p>
<p>First of all viewstate is enabled by default across the entire project so the first thing we need to do is turn it off. The idea is to opt in to viewstate rather than opt out. We only want to turn it on for the controls where we specifically want to maintain state. </p>
<p>In each masterpage or at the top of each page you can place this code in the page directive to disable the viewstate.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12916"><td class="code" id="p129code16"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> Page EnableViewState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;False&quot;</span>  ViewStateMode<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Disabled&quot;</span><span style="color: #000000; font-weight: bold;">%&gt;</span></pre></td></tr></table></div>

<p><br/><br />
Or you can disable the viewstate across the whole application by putting this in the Pages tag in web config.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12917"><td class="code" id="p129code17"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>Pages EnableViewState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;false&quot;</span>  <span style="color: #006600; font-weight: bold;">/&gt;</span></pre></td></tr></table></div>

<p><br/><br />
Now that Viewstate is off by default &#8230; we can choose which controls we want to maintain state for. Most text fields and simple controls will be fine without viewstate. As a general rule leave it off until you have a specific reason to turn it on. The give away will be when you try to access the value of a control in code behind and get the default value no matter what value was set. In this case its time to turn on viewstate for that control only. We do this by using the same code as we did to turn it off &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12918"><td class="code" id="p129code18"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myDdl&quot;</span> EnableViewState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;True&quot;</span>  ViewStateMode<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Enabled&quot;</span><span style="color: #006600; font-weight: bold;">/&gt;</span></pre></td></tr></table></div>

<p><br/><br />
You can add this to any control and access its value from a postback. If you view the page source again you will see the __VIEWSTATE field is much smaller &#8230; naturally the larger number of controls you enable viewstate for the larger the field will be.</p>
<p>Use it with caution but don&#8217;t write it off as bad practice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/05/15/viewstate-is-not-evil/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bad Examples</title>
		<link>http://www.idevforfun.com/index.php/2011/05/07/bad-examples/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bad-examples</link>
		<comments>http://www.idevforfun.com/index.php/2011/05/07/bad-examples/#comments</comments>
		<pubDate>Sat, 07 May 2011 17:40:44 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=132</guid>
		<description><![CDATA[TweetAll over the web I see the code you see below. While it will work it is wrong &#8230; don&#8217;t do it! A web response stream is a stream like any other, you need to ensure that it is closed when you are finished with it or it will not release its resources. In the ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton132" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F07%2Fbad-examples%2F&amp;via=Lawnmower_Man&amp;text=Bad%20Examples&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F07%2Fbad-examples%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>All over the web I see the code you see below. While it will work it is wrong &#8230; don&#8217;t do it! A web response stream is a stream like any other, you need to ensure that it is closed when you are finished with it or it will not release its resources. In the case of a web response (or request for that matter), failing to close it could mean the server will run out of connections. In a high traffic web site this won&#8217;t take all that long. </p>
<p>Let&#8217;s take a look at the code &#8230; what would happen if the call to Write throws an exception? Execution would stop right there and the exception will get bubbled up the call stack. The call to Close will never happen and the response stream will remain open reducing the server&#8217;s connection capability by one.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p13222"><td class="code" id="p132code22"><pre class="csharp" style="font-family:monospace;"> var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>byteData, <span style="color: #FF0000;">0</span>, byteData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><br/><br />
We can avoid this by putting the call to Close in a finally block as below. This would guarantee that the call to Close will be made even if an exception is thrown. This code is perfectly valid, but cumbersome to have to do every time you need to ensure a resource is released.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p13223"><td class="code" id="p132code23"><pre class="csharp" style="font-family:monospace;">var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
    responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>byteData, <span style="color: #FF0000;">0</span>, byteData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">finally</span>
<span style="color: #008000;">&#123;</span>
    responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
The code can be rewritten as below to produce the same behaviour. The &#8216;using&#8217; keyword automatically calls Dispose (as good as calling Close) on the resource as soon as it falls out of scope resulting in the same behaviour as our finally block. Any time you use a stream, db connection or any resource that needs to be released, use a &#8216;using&#8217; statement and you can&#8217;t go wrong.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p13224"><td class="code" id="p132code24"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>byteData, <span style="color: #FF0000;">0</span>, byteData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/05/07/bad-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq to Objects Performance</title>
		<link>http://www.idevforfun.com/index.php/2010/09/09/linq-to-objects-performance/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-to-objects-performance</link>
		<comments>http://www.idevforfun.com/index.php/2010/09/09/linq-to-objects-performance/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 21:11:32 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=103</guid>
		<description><![CDATA[TweetI have often wondered about Linq to objects. When we use a linq Select or FirstOrDefault statement what is the complexity of the call? Does it iterate through the entire list until it finds the object it is looking for in which case we are dealing with O(n) complexity? The other day I had to ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton103" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F09%2F09%2Flinq-to-objects-performance%2F&amp;via=Lawnmower_Man&amp;text=Linq%20to%20Objects%20Performance&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F09%2F09%2Flinq-to-objects-performance%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>I have often wondered about Linq to objects. When we use a linq Select or FirstOrDefault statement what is the complexity of the call? Does it iterate through the entire list until it finds the object it is looking for in which case we are dealing with O(n) complexity? The other day I had to write a small migration tool that answered my question. Linq to objects does in fact iterate through every item in a list until it stumbles across the object it is looking for. Take the following case&#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p10326"><td class="code" id="p103code26"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> 
<span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> list2<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Public</span> LinqTester<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Populating lists ...&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	list1<span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	list2 <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">100000</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		list1<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		list2<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	list2<span style="color: #008000;">.</span><span style="color: #0000FF;">Sort</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoopTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Starting loop test ... &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var start <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var num <span style="color: #0600FF; font-weight: bold;">in</span> list1<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		list2<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">==</span> num<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	var timeTaken <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> start<span style="color: #008000;">;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time taken = {0}&quot;</span>, timeTaken<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> BinaryTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Starting binary test ... &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var start <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var num <span style="color: #0600FF; font-weight: bold;">in</span> list1<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		list2<span style="color: #008000;">.</span><span style="color: #0000FF;">BinarySearch</span><span style="color: #008000;">&#40;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	var timeTaken <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> start<span style="color: #008000;">;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time taken = {0}&quot;</span>, timeTaken<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//output</span>
<span style="color: #008080; font-style: italic;">//Starting loop test ... Time taken = 00:01:16.5160000</span>
<span style="color: #008080; font-style: italic;">//Starting binary test ... Time taken = 00:00:00.0170000</span></pre></td></tr></table></div>

<p><br/><br />
From this simple test we can see that using a binary search is by far more efficient than using linq to objects. When you think about it the compiler has no way of knowing if the list is sorted or how to compare the objects contained in the list if they are objects rather than primitives. It is impossible for the compiler to optimise the search so it can only do a linear search through the collection until it finds the item it is looking for. If you&#8217;re dealing with small collections this isn&#8217;t an issue but when the collections get large or if you&#8217;re concerned about performance then it&#8217;s worth considering whether linq is the appropriate choice. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/09/09/linq-to-objects-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio Power Commands</title>
		<link>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=visual-studio-power-commands</link>
		<comments>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 20:53:42 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=98</guid>
		<description><![CDATA[TweetPower Commands is a free and extremely useful plugin for visual studio. Among its many features is &#8216;Collapse All&#8217; so that you can collapse the solution tree in the solution explorer and &#8216;Open Command Window&#8217; to open a command window in the directory of the selected project. These are the two features I use the ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton98" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F06%2F10%2Fvisual-studio-power-commands%2F&amp;via=Lawnmower_Man&amp;text=Visual%20Studio%20Power%20Commands&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F06%2F10%2Fvisual-studio-power-commands%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><a href="http://code.msdn.microsoft.com/PowerCommands">Power Commands</a> is a free and extremely useful plugin for visual studio. Among its many features is &#8216;Collapse All&#8217; so that you can collapse the solution tree in the solution explorer and &#8216;Open Command Window&#8217; to open a command window in the directory of the selected project. These are the two features I use the most and prove invaluable as the solution I use at work has 91 projects attached to it &#8230; this can be really painful to navigate when a number of the projects are expanded. Well worth the download &#8230; </p>
<p><a href="http://code.msdn.microsoft.com/PowerCommands">Download Here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comma Delimited Lists</title>
		<link>http://www.idevforfun.com/index.php/2010/02/07/comma-delimited-lists/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=comma-delimited-lists</link>
		<comments>http://www.idevforfun.com/index.php/2010/02/07/comma-delimited-lists/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 18:10:28 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/blog/?p=77</guid>
		<description><![CDATA[TweetWe are commonly required to handle comma delimited lists while developing applications. In the past, I have used for loops or Array.Split() to handle these situations. At least I did until I discovered a handy little class in the .NET framework that is there to handle just such situations. The CommaDelimitedStringCollection class in the System.Configuration ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton77" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F02%2F07%2Fcomma-delimited-lists%2F&amp;via=Lawnmower_Man&amp;text=Comma%20Delimited%20Lists&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F02%2F07%2Fcomma-delimited-lists%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>We are commonly required to handle comma delimited lists while developing applications. In the past, I have used for loops or Array.Split() to handle these situations. At least I did until I discovered a handy little class in the .NET framework that is there to handle just such situations. The CommaDelimitedStringCollection class in the System.Configuration namespace behaves just like a normal generic list including methods for Add, Remove, IndexOf, Count, etc and can be looped through like any other list. This allows the flexibility of searching and sorting and also simplifies the handling of empty lists and removal of trailing commas when returning the comma delimited string. To convert to the comma delimited string all you have to do is call ToString() and you&#8217;re done. </p>
<p>More information can be found <a href="http://msdn.microsoft.com/en-us/library/system.configuration.commadelimitedstringcollection.aspx">here</a></p>

<div class="wp_codebox"><table><tr id="p7728"><td class="code" id="p77code28"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> str1 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;A String&quot;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> str2 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Another String&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//assume System.Configuration has been imported</span>
CommaDelimitedStringCollection strList <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CommaDelimitedStringCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>str1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>str2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Count = &quot;</span> <span style="color: #008000;">+</span> strList<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//output =&gt; Count = 3</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Index of '1' = &quot;</span> <span style="color: #008000;">+</span> strList<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//output =&gt; Index of '1' = 0</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>strList<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">//output =&gt; 1,A String,Another String</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/02/07/comma-delimited-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extension Methods</title>
		<link>http://www.idevforfun.com/index.php/2010/01/16/extension-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=extension-methods</link>
		<comments>http://www.idevforfun.com/index.php/2010/01/16/extension-methods/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 21:55:53 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/blog/?p=44</guid>
		<description><![CDATA[TweetExtension methods are a handy feature that allows you to extend the functionality of existing classes for which you do not have the source. Extension methods must be static and the type following the &#8216;this&#8217; keyword indicates the type that is extended by this method. More information on extension methods can be found here These ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton44" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F01%2F16%2Fextension-methods%2F&amp;via=Lawnmower_Man&amp;text=Extension%20Methods&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F01%2F16%2Fextension-methods%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Extension methods are a handy feature that allows you to extend the functionality of existing classes for which you do not have the source. Extension methods must be static and the type following the &#8216;this&#8217; keyword indicates the type that is extended by this method.</p>
<p>More information on extension methods can be found <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">here</a></p>
<p>These are just some of the extension methods I commonly use to make everyday coding a little easier and more readable.</p>

<div class="wp_codebox"><table><tr id="p4430"><td class="code" id="p44code30"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Reformat a string so that the first letter of every word is capitalised and every other letter is lower case</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;str&quot;&gt;String to format&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Formatted string&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToTitleCase<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">string</span> str<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    CultureInfo ci <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CultureInfo<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;en&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    TextInfo cc <span style="color: #008000;">=</span> ci<span style="color: #008000;">.</span><span style="color: #0000FF;">TextInfo</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> cc<span style="color: #008000;">.</span><span style="color: #0000FF;">ToTitleCase</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Convert this string to its enum type</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;Type of enum&lt;/typeparam&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumValue&quot;&gt;string value to parse&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Enum type parsed from string&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T ConvertToEnum<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">string</span> enumValue<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">struct</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsEnum</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InvalidEnumArgumentException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;T must be an enum&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, enumValue, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Get the value of a table column from a reader object</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;Type  of expected return value&lt;/typeparam&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;reader&quot;&gt;Reader oject to get the value from&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;columnName&quot;&gt;Name of column to retrieve the value from&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Column Value&lt;/returns&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;exception cref=&quot;NullReferenceException&quot;&gt;If T is not nullable and the column value is null&lt;/exception&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T GetColumnValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IDataReader reader, <span style="color: #6666cc; font-weight: bold;">string</span> columnName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">IsDBNull</span><span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">GetOrdinal</span><span style="color: #008000;">&#40;</span>columnName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
           <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>
               <span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsGenericType</span> <span style="color: #008000;">&amp;&amp;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetGenericTypeDefinition</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>Nullable<span style="color: #008000;">&lt;&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
               <span style="color: #008000;">||</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#41;</span>
           <span style="color: #008000;">&#123;</span>
               <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
           <span style="color: #008000;">&#125;</span>
           <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NullReferenceException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DB Value is null&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
       <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>reader<span style="color: #008000;">&#91;</span>columnName<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//The above methods are called like this ...</span>
<span style="color: #6666cc; font-weight: bold;">string</span> myStr <span style="color: #008000;">=</span><span style="color: #666666;">&quot;exAmple sTrinG&quot;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>myStr<span style="color: #008000;">.</span><span style="color: #0000FF;">ToTitleCase</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Output --&gt; Example String</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//assume we have the namespace Microsoft.SqlServer.Management.Smo imported</span>
<span style="color: #6666cc; font-weight: bold;">string</span> varChar <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;VarChar&quot;</span><span style="color: #008000;">;</span>
SqlDataType testVrChr <span style="color: #008000;">=</span> varChar<span style="color: #008000;">.</span><span style="color: #0000FF;">ConvertToEnum</span><span style="color: #008000;">&lt;</span>SqlDataType<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//where reader is an IDataReader</span>
<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">?</span> value <span style="color: #008000;">=</span> reader<span style="color: #008000;">.</span><span style="color: #0000FF;">GetColumnValue</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">?&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ColName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/01/16/extension-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Windows UI Threading</title>
		<link>http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=windows-ui-threading</link>
		<comments>http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 22:07:00 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/blog/?p=5</guid>
		<description><![CDATA[TweetUsing background threads to handle long running processes such as fetching data from the db should be common practice when developing windows forms. Failure to do so will result in a clunky UI that will lock up and may appear as crashed to the user. It also allows you to do nice things like updating ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton5" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F01%2F10%2Fwindows-ui-threading%2F&amp;via=Lawnmower_Man&amp;text=Windows%20UI%20Threading&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F01%2F10%2Fwindows-ui-threading%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Using background threads to handle long running processes such as fetching data from the db should be common practice when developing windows forms. Failure to do so will result in a clunky UI that will lock up and may appear as crashed to the user. It also allows you to do nice things like updating progress bars etc. </p>
<p>A common trap is trying to call a method which updates a UI control from a background thread. This will result in the &#8220;Cross Thread Call&#8221; exception which can frighten many developers away from threading in the IU. Here is a pattern I tend to follow when threading UI that takes a lot of the mystery out of things and keeps the UI running smoothly. </p>
<p>First we have a method that calls a long running process. In this method we simply create a new thread in which to run the process and get it started.</p>

<div class="wp_codebox"><table><tr id="p533"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p5code33"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Do some long running process on a background thread</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     lbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
     Thread worker <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #008000;">&#40;</span>DoSomething<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Long running process </span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">+=</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
          UpdateProgressBar<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
          Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>   
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In each iteration of the loop we update the value of a progress bar. This however can only be achieved on the UI thread and this method was called on the background thread. Normally this would throw an exception as the invoke keyword is not used but because of the way the UpdateProgressBar method has been written this is not an issue. </p>
<p>The code below shows how to write any method that updates a UI control so that it can be called safely from any thread and will always execute on the UI thread no matter what the thread the caller is on.</p>

<div class="wp_codebox"><table><tr id="p534"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p5code34"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Something that updates a UI control that </span>
<span style="color: #008080; font-style: italic;">/// might be called from a background thread </span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;progress&quot;&gt;&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> UpdateProgressBar<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> progress<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//Wrap any code that updates a UI control in this anonymous delegate</span>
     Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span> work <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> progressCount<span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>progressCount <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> progressCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>progressCount <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> progressCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
        progressBar1<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> progressCount<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>progressCount <span style="color: #008000;">==</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> lbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Done!&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">//Check if we are running on a background thread and if so use Invoke</span>
     <span style="color: #008080; font-style: italic;">//otherwise just call the delegate</span>
     <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeRequired</span><span style="color: #008000;">&#41;</span> Invoke<span style="color: #008000;">&#40;</span>work, progress<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF; font-weight: bold;">else</span> work<span style="color: #008000;">&#40;</span>progress<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Func is an anonymous delegate … all logic that affects controls should be wrapped in this. The last two lines handle the differing threads. InvokeRequired determines if we are on the UI thread or the background thread. If we are on a background thread it will return true and we call Invoke on the anonymous method to ensure it is executed on the UI thread. If we are already on the UI thread the method is just called without Invoke.</p>
<p>By structuring the control update methods this way they can be called from any thread and will never throw an error due to cross thread calls. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

