<?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>Developing For .NET &#187; Visual Studio</title>
	<atom:link href="http://www.developingfor.net/category/visual-studio/feed" rel="self" type="application/rss+xml" />
	<link>http://www.developingfor.net</link>
	<description>Real World .NET Methods, Tricks, and Examples</description>
	<lastBuildDate>Mon, 01 Mar 2010 16:32:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Setting Conditional Breakpoints in VS2008</title>
		<link>http://www.developingfor.net/visual-studio/setting-conditional-breakpoints-in-vs2008.html</link>
		<comments>http://www.developingfor.net/visual-studio/setting-conditional-breakpoints-in-vs2008.html#comments</comments>
		<pubDate>Tue, 17 Jun 2008 17:28:18 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/?p=100</guid>
		<description><![CDATA[I subscribe to Sara Ford&#8217;s excellent Blog Visual Studio 2008 Tip of the Day.Â  I have to admit though, that I rarely use the tips.Â  They tend to be a little too deep in the mud for my tastes, although I will forever be grateful for her showing us how to change colors for things [...]]]></description>
			<content:encoded><![CDATA[<p>I subscribe to Sara Ford&#8217;s excellent Blog <a title="Sara Ford, VS2008 Tip of the Day" href="http://blogs.msdn.com/saraford/archive/tags/Visual+Studio+2008+Tip+of+the+Day/default.aspx" target="_blank">Visual Studio 2008 Tip of the Day</a>.Â  I have to admit though, that I rarely use the tips.Â  They tend to be a little too deep in the mud for my tastes, although I will forever be grateful for her showing us how to change colors for things like Matching Braces.</p>
<p>Today, however, she has a great tip that you should probably read: <a title="Sara Ford, VS2008 Tip of the Day" href="http://blogs.msdn.com/saraford/archive/2008/06/17/did-you-know-you-can-set-conditional-breakpoints-239.aspx" target="_blank">how to set conditional breakpoints</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/visual-studio/setting-conditional-breakpoints-in-vs2008.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Foray into Unit Testing with Visual Studio 2008</title>
		<link>http://www.developingfor.net/visual-studio/first-foray-into-unit-testing-with-visual-studio-2008.html</link>
		<comments>http://www.developingfor.net/visual-studio/first-foray-into-unit-testing-with-visual-studio-2008.html#comments</comments>
		<pubDate>Thu, 12 Jun 2008 19:28:03 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/?p=99</guid>
		<description><![CDATA[I&#8217;ve read about Unit Testing and Test Driven Development (TDD) but have never attempted to use it or even really understand it.  I recently became intrigued by it watching Rob Conery&#8217;s excellent video series. Today I began putting the final touches on the Authorize.Net code I am going to release, so I thought this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve read about Unit Testing and Test Driven Development (TDD) but have never attempted to use it or even really understand it.  I recently became intrigued by it watching <a title="Rob Conery" href="http://blog.wekeroad.com/mvc-storefront/" target="_blank">Rob Conery&#8217;s excellent video series</a>. Today I began putting the final touches on the Authorize.Net code I am going to release, so I thought this would be a good opportunity to try some testing.</p>
<p>I have never done any Unit Testing, and even though I wasn&#8217;t sure where to start, I figured it out pretty quickly. First things first, I added a Test Project to my solution.  This created all the necessary framework code as one might expect.  I deleted the default TestMethod and began adding TestMethods of my own, intuitively marked with the <em>[TestMethod]</em> attribute.  A little Google hopping and I quickly learned about <em>Assert.xxx()</em> methods.</p>
<p>When it came time to actually run the tests, I was a little stumped.  I read where you could right click the method name and execute &#8220;Run Tests&#8221;, but it seems to only run one at a time.  In the test results window, there is a button for &#8220;Run All test&#8221;, but it too only ran the last executed test.  Finally, I changed the Test project to my Solution Start Project, and pressing F5 to execute will run all the tests.</p>
<p>Here is the code from my first stab at it:</p>
<div class="dean_ch" style="white-space: nowrap;">using System;<br />
using System.<span class="me1">Text</span>;<br />
using System.<span class="me1">Collections</span>.<span class="me1">Generic</span>;<br />
using System.<span class="me1">Linq</span>;<br />
using Microsoft.<span class="me1">VisualStudio</span>.<span class="me1">TestTools</span>.<span class="me1">UnitTesting</span>;<br />
using DevelopingForDotNet.<span class="me1">AuthorizeNet</span>;</p>
<p>namespace TestAuthorizeNet<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">///</span><br />
&nbsp; &nbsp; <span class="co1">/// Summary description for UnitTest1</span><br />
&nbsp; &nbsp; <span class="co1">///</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>TestClass<span class="br0">&#93;</span><br />
&nbsp; &nbsp; public class UnitTest1<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public UnitTest1<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// TODO: Add constructor logic here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; private TestContext testContextInstance;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">///</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">///Gets or sets the test context which provides</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">///information about and functionality for the current test run.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">///</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public TestContext TestContext<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> testContextInstance;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; testContextInstance = value;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">#region Additional test attributes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// You can use the following additional attributes as you write your tests:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Use ClassInitialize to run code before running the first test in the class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// [ClassInitialize()]</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// public static void MyClassInitialize(TestContext testContext) { }</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Use ClassCleanup to run code after all tests in a class have run</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// [ClassCleanup()]</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// public static void MyClassCleanup() { }</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Use TestInitialize to run code before running each test</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// [TestInitialize()]</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// public void MyTestInitialize() { }</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Use TestCleanup to run code after each test has run</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// [TestCleanup()]</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// public void MyTestCleanup() { }</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">#endregion</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>TestMethod<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> TestCreditCardNumberWithCharactersConvertedToJustNumbers<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TransactionRequestInfo req = new TransactionRequestInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> testValue = <span class="st0">&quot;1234-5678-9012-3456&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.<span class="me1">CardNumber</span> = testValue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">AreEqual</span><span class="br0">&#40;</span><span class="st0">&quot;1234567890123456&quot;</span>, req.<span class="me1">CardNumber</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>TestMethod<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> TestZipCodeNotNumericThrowsArgumentException<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TransactionRequestInfo req = new TransactionRequestInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> testValue = <span class="st0">&quot;034JB&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.<span class="me1">Zip</span> = testValue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">Fail</span><span class="br0">&#40;</span><span class="st0">&quot;Exception not thrown.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch <span class="br0">&#40;</span>ArgumentException aex<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">IsTrue</span><span class="br0">&#40;</span><span class="kw2">true</span>, <span class="st0">&quot;Exception Thrown Properly&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>TestMethod<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> TestZipCodeNumericButGreaterThan99999<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TransactionRequestInfo req = new TransactionRequestInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> testValue = <span class="st0">&quot;58634789&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.<span class="me1">Zip</span> = testValue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">Fail</span><span class="br0">&#40;</span><span class="st0">&quot;Exception not thrown.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch <span class="br0">&#40;</span>ArgumentException aex<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">IsTrue</span><span class="br0">&#40;</span><span class="kw2">true</span>, <span class="st0">&quot;Exception Thrown Properly&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>TestMethod<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> TestZipCodeValidReturnsLeadingZeros<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TransactionRequestInfo req = new TransactionRequestInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> testValue = <span class="st0">&quot;586&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.<span class="me1">Zip</span> = testValue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">AreEqual</span><span class="br0">&#40;</span><span class="st0">&quot;00586&quot;</span>, req.<span class="me1">Zip</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>A few notes worth mentioning:</p>
<ol>
<li>I found a bunch of sites that talked about Testing in Visual Studio, about half and half claiming it was a good thing.  What I did not find so readily was a good tutorial.  I did finally come across a good post at <a title="Geek Zone" href="http://www.geekzone.co.nz/vs2008/4819" target="_blank">http://www.geekzone.co.nz/vs2008/4819</a>, a blog I had never visited before.</li>
<li>What I have done so far would certainly not qualify as TDD, I&#8217;m just experimenting at this phase.  I did, however, try to write tests that failed before correcting them to pass, but they were all on pre-existing methods.</li>
<li>I tried to follow the &#8220;use long, descriptive test method names&#8221; rule.</li>
<li>I need to understand mocking.  Right now, I am creating full fledged class instances.</li>
<li>It seems a tad slow.  I can&#8217;t imagine what this would be like with hundreds of tests to run.  I&#8217;m sure there are ways to handle this issue.</li>
</ol>
<p>My plan is to include the test Project along with the entire Authorize.Net Solution.  Whether it will be helpful or not, I don&#8217;t really know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/visual-studio/first-foray-into-unit-testing-with-visual-studio-2008.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008 Setup Project Frustrations</title>
		<link>http://www.developingfor.net/setup-and-deployment/visual-studio-2008-setup-project-frustrations.html</link>
		<comments>http://www.developingfor.net/setup-and-deployment/visual-studio-2008-setup-project-frustrations.html#comments</comments>
		<pubDate>Wed, 05 Mar 2008 00:56:19 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Setup And Deployment]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/setup-and-deployment/visual-studio-2008-setup-project-frustrations.html</guid>
		<description><![CDATA[I have not needed to create a Setup project since installing VS2008, but this afternoon I needed to share a utility program with a coworker friend of mine (who is not technical), so I opened the solution and added a Setup and Deployment project.  Everything seemed to be fine.  I added the primary [...]]]></description>
			<content:encoded><![CDATA[<p>I have not needed to create a Setup project since installing VS2008, but this afternoon I needed to share a utility program with a coworker friend of mine (who is not technical), so I opened the solution and added a Setup and Deployment project.  Everything seemed to be fine.  I added the primary output of my application&#8217;s project and adjusted the company name and product name properties, as well as some other settings, just like I used to do in VS2005.</p>
<p>The problem occurred when I wanted to create Start Menu and Desktop shortcuts for the application. In VS2005, double clicking the <em>Application Folder</em> would show you the list of DLLs, EXEs, and other files that would be installed. You could right-click the main project output file and create a shortcut to the program, which could then be copied to the <em>User&#8217;s Desktop</em> and <em>User&#8217;s Program Menu</em> directories.  And you could also add icons and assign them to the shortcuts.</p>
<p>This was what I wanted to do in VS2008, but double clicking on the folders in File System does nothing.  As a result, you cannot select the output file as instructed in the <a href="http://msdn2.microsoft.com/en-us/library/sat42980.aspx" title="MSDN Documentation" target="_blank">MSDN documentation</a>. This is very frustrating: the Setup and Deployment project has always worked well for me (these are internal distribution installers), but now with VS2008 it no longer appears to function properly.  The resulting MSI did work and did install the software, but without an icon and without shortcuts in the desired locations.</p>
<p>If you know the solution for this, please leave a comment.  If I find a solution to this, I&#8217;ll be sure to share it.  For now, all I can do is share the pain.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/setup-and-deployment/visual-studio-2008-setup-project-frustrations.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Event Handling made even easier</title>
		<link>http://www.developingfor.net/c-20/event-handling-made-even-easier.html</link>
		<comments>http://www.developingfor.net/c-20/event-handling-made-even-easier.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 23:42:05 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/event-handling-made-even-easier.html</guid>
		<description><![CDATA[Event Handling Made Easy is my favorite post I&#8217;ve written.  Mostly, because I&#8217;m lazy and I have a bad memory.  I love events, but I find it difficult to remember all the bits and pieces in my head, so I wrote the post to give me some documentation.
NOTE: the following feature is NOT [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.developingfor.net/net-20/event-handling-made-easy.html" title="Event Handling Made Easy">Event Handling Made Easy</a> is my favorite post I&#8217;ve written.  Mostly, because I&#8217;m lazy and I have a bad memory.  I love events, but I find it difficult to remember all the bits and pieces in my head, so I wrote the post to give me some documentation.</p>
<p>NOTE: the following feature is NOT new: it has been available since .NET 2.0, but I learned about it while examining the new features in 2008.</p>
<p>I recently came across the <em>EventHandler&lt;T&gt;</em> generic delegate. In this case, T represents any EventArgs object, so when you create your custom EventArgs class just make sure that you inherit from EventArgs.  Now, when you register the Event Handler with the object, you reference <em>EventHandler&lt;T&gt;</em> instead of your custom event name:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// registering EventHandler&lt;T&gt;</span><br />
person.<span class="me1">MaritalStatusChangedEvent</span> +=new EventHandler&lt;MaritalStatusChangedEventArgs&lt;<span class="br0">&#40;</span>person_MaritalStatusChangedEvent<span class="br0">&#41;</span>;</div>
<p>This approach buys us a couple of things. First, it means you no longer need to declare a public delegate to define the EventHandler signature.  Instead, in your class, you simply declare a <em>public EventHandler&lt;CustomEventArgs&gt; CustomEventName</em>.  I don&#8217;t know about you, but anytime I can avoid using &#8220;delegate&#8221; I get a smile on my face!  Also, it means that event registration code is a little more standardized, and is syntactically a little nicer.</p>
<p>So here is the whole process, revisited and updated:</p>
<p><strong>Step 1:</strong> Create our custom EventArgs class.  Be sure it inherits from <em>EventArgs</em>.</p>
<div class="dean_ch" style="white-space: nowrap;">class MaritalStatusChangedEventArgs : EventArgs<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; public readonly <span class="kw4">string</span> Message;<br />
&nbsp; &nbsp; public MaritalStatusChangedEventArgs<span class="br0">&#40;</span><span class="kw4">string</span> message<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Message = message;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><strong>Step 2:</strong> Define the EventHandler&lt;T&gt; in the firing class.</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// Generic EventHandle&lt;T&gt; delegate definition</span><br />
public event EventHandler&lt;MaritalStatusChangedEventArgs&gt; MaritalStatusChangedEvent;</div>
<p><strong>Step 3:</strong> Fire the Event.</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// No change here</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span>MaritalStatusChangedEvent != <span class="kw2">null</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; MaritalStatusChangedEvent<span class="br0">&#40;</span>this, new MaritalStatusChangedEventArgs<span class="br0">&#40;</span><span class="st0">&quot;Marital Status is now &quot;</span> + _maritalStatus<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p><strong>Step 4:</strong> Register the EventHandler&lt;T&gt; listener.</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// Use the EventHandler&lt;T&gt; format when registering</span><br />
person.<span class="me1">MaritalStatusChangedEvent</span> +=new EventHandler&lt;MaritalStatusChangedEventArgs&gt;<span class="br0">&#40;</span>person_MaritalStatusChangedEvent<span class="br0">&#41;</span>;</div>
<p>When you register the Event listener, Visual Studio will help out by offering to stub out the listening method.  Just hit tab when prompted and it will insert the method stub for you and place your cursor inside the method.  Naturally, it throws our old friend <em>NotImplementedException</em>.  This is a nice little addition that means less coding for you!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/c-20/event-handling-made-even-easier.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008 Class Diagram</title>
		<link>http://www.developingfor.net/visual-studio/visual-studio-2008-class-diagram.html</link>
		<comments>http://www.developingfor.net/visual-studio/visual-studio-2008-class-diagram.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 21:51:23 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/visual-studio-2008-class-diagram.html</guid>
		<description><![CDATA[I&#8217;ll be returning to my series on &#8220;Upgrading your C# skills&#8221; next week. For now, though, I wanted to share with you a cool new option in VS2008: Class Diagrams.
Class Diagrams
A Class Diagram is a new file type that allows you to visually create, edit, and manage Classes, Structs, Enums, Interfaces, Delegates, and more.  [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be returning to my series on &#8220;Upgrading your C# skills&#8221; next week. For now, though, I wanted to share with you a cool new option in VS2008: Class Diagrams.</p>
<p><strong>Class Diagrams</strong></p>
<p>A Class Diagram is a new file type that allows you to visually create, edit, and manage Classes, Structs, Enums, Interfaces, Delegates, and more.  It is a quick and easy way to add Property and Method stubs to existing Classes or to create entire class shells.  You can even manage Events and Fields.  The interface is immediately familiar and intuitive.  Diagrams can even be exported to Images.</p>
<p>To begin, open a project in Visual Studio and follow these steps:</p>
<ul>
<li>In the Solution Explorer window, right click the project and select &#8220;Add -&gt; New Item&#8221;.</li>
<li>From the Add New Item dialog box, highlight &#8220;Class Diagram&#8221; and give it a name.</li>
<li>Press the Add button.  The Class Diagram file will be added to your project and the designer will open.</li>
</ul>
<p>You will now have a Blank Diagram to work with in the designer.  To add an existing class to the diagram, simply drag and drop it from the Solution Explorer window onto the design surface.  To create a new Class (or Struct, Enum, etc.), right click in the designer and expand the &#8220;Add -&gt;&#8221; menu item.  Select the appropriate type and follow the instructions.  Easy as pie.</p>
<p>Once you have an item in the designer, it will look something like this (Click on Screen Shot for Full Size):</p>
<p><a href="http://www.developingfor.net/wp-content/uploads/ClassDiagram.png" title="Class Diagram Screenshot"><img src="/wp-content/uploads/ClassDiagram.png" title="Class Diagram Screenshot" alt="Class Diagram Screenshot" width="450" /></a></p>
<p>I moved the Class Details window to the right, but the default will have it on the bottom.  Just like working with a Control in a Form, you will need to select the class in the designer window in order for its details to show in the Class Detail window.  And you can have multiple classes in a diagram.</p>
<p>When you create a new item, you will be prompted to either create a new file or append to an existing file.  As you can see, the Class Detail window allows you to easily add Methods, Properties, Fields, and Events to a class.  You can edit the Name of an element, the Type, the Access Modifier, and the Summary.  The Summary column is the XML Comments description: if you leave it blank, then there will be no XML comments at all.  If you supply a Summary, it will be added to the source code.  You can also elect to &#8220;Hide&#8221; an element, which only means that it will be not listed in the Class Diagram itself.</p>
<p>So let&#8217;s talk about what code the IDE generates from this designer.  Consider the Person class listed above: this class already existed and was dropped into the Class Diagram designer.  I then added the <em>DOB</em>, <em>MaritalStatus</em>, and <em>IsMilitaryVeteran</em> properties using the Class Detail window.  Now the code looks like this:</p>
<div class="dean_ch" style="white-space: nowrap;">class Person<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; public Person<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; public <span class="kw4">string</span> FirstName <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; public <span class="kw4">string</span> LastName <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; public <span class="kw4">int</span> Age <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Date Of Birth</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public <span class="kw4">string</span> DOB<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Marital Status Code</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public <span class="kw4">char</span> MaritalStatus<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Exposes whether or not this Person ever served in the Armed Forces</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public bool IsMilitaryVeteran<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>The properties were created, but not as I would have expected.  I would have expected these to be default <a href="http://www.developingfor.net/index.php?s=%22Automatic+Properties%22&amp;sbutt=Go" title="Automatic Properties on DevelopingFor.Net">Automatic Property</a> stubs. Instead, they are classic property stubs, and the getter throws a <em>NotImplementedException</em>.  The good news is that this will compile as is.  The bad news is that it is surely not the behavior you want, so you will have to do some work to implement the details.  And you cannot simply remove the <em>NotImplementedException</em> because the getter must have a return path.  Of course, a simple copy and paste can quickly convert these to Automatic Properties, but it is unfortunate that this isn&#8217;t the default behavior.</p>
<p>Which brings up another good point: the Property is created, but with no corresponding variable, and yet it still compiles.  This tells me that the Automatic Property mechanism is still in play here, but it causes a problem when you go to implement the details: there is no variable to return, so you must create one.  A pretty minor point, but worth noting none-the-less.  And again, I think this issue could have been avoided if the standard Automatic Property stub had been implemented (or at least optional).</p>
<p>So let&#8217;s make this a little more robust and add some other goodies.  First, since I have to implement the details anyway, I&#8217;m going to add fields to support my new properties.  Next, I&#8217;m going to add a <em>GetFullName</em> method, and I&#8217;m going to override the <em>ToString</em> method.  Finally, I&#8217;m going to add a <em>MaritalStatusChangedEvent</em> Event Handler.  Now let&#8217;s look at the results:</p>
<div class="dean_ch" style="white-space: nowrap;">class Person<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; private <span class="kw4">string</span> _dob;<br />
&nbsp; &nbsp; private bool _isMilitaryVeteran;<br />
&nbsp; &nbsp; private <span class="kw4">char</span> _maritalStatus;</p>
<p>&nbsp; &nbsp; public event EventHandler MaritalStatusChangedEvent;</p>
<p>&nbsp; &nbsp; public Person<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; public <span class="kw4">string</span> FirstName <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; public <span class="kw4">string</span> LastName <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; public <span class="kw4">int</span> Age <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Date Of Birth</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public <span class="kw4">string</span> DOB<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Marital Status Code</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public <span class="kw4">char</span> MaritalStatus<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Exposes whether or not this Person ever served in the Armed Forces</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public bool IsMilitaryVeteran<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Retrieves the Full Name of the Person formatted as &quot;Last, First&quot;.</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; public <span class="kw4">string</span> GetFullName<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; public <span class="kw4">string</span> ToString<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw new System.<span class="me1">NotImplementedException</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Much as expected, the fields are simple fields, the Event is declared, and the methods are stubbed but all throw a <em>NotImplementedException</em>. This all compiles right out of the box, but if you want to really override <em>ToString()</em>, you&#8217;ll need to add the keyword yourself.  Again, it would be nice if the tool detected that for you and prompted you to select the desired behavior, but it is not that big of a deal.</p>
<p>Finally, the Class Diagram tool has some options.  When you open one, VS2008 will include a new Menu and a new Toolbar.  Explore these options a little and you&#8217;ll find some other helpful items.  In this last screen shot, representing the completed class above, I have elected to show full signature detail in the Designer. (Click on Screen Shot for Full Size).</p>
<p><a href="http://www.developingfor.net/wp-content/uploads/ClassDiagram2.png" title="Class Diagram Screenshot"><img src="/wp-content/uploads/ClassDiagram2.png" title="Class Diagram Screenshot" alt="Class Diagram Screenshot" width="450" /></a></p>
<p>So I encourage you to explore this feature.  For rapid stubbing, I think you&#8217;ll find this a very handy and useful tool.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/visual-studio/visual-studio-2008-class-diagram.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Changing Visual Studio Shortcuts</title>
		<link>http://www.developingfor.net/visual-studio/changing-visual-studio-shortcuts.html</link>
		<comments>http://www.developingfor.net/visual-studio/changing-visual-studio-shortcuts.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 18:21:06 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/changing-visual-studio-shortcuts.html</guid>
		<description><![CDATA[OK, so I&#8217;ve been playing with VS2008 for a few days now, and I had a minor annoyance.  At first, I was inclined to blame the designers, but it turns out it was my own fault, or rather, my own lack of knowledge.
My complaint was that in VS2005, &#8220;F6&#8243; is the Shortcut Key for [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so I&#8217;ve been playing with VS2008 for a few days now, and I had a minor annoyance.  At first, I was inclined to blame the designers, but it turns out it was my own fault, or rather, my own lack of knowledge.</p>
<p>My complaint was that in VS2005, &#8220;F6&#8243; is the Shortcut Key for &#8220;Build Solution&#8221;, but in VS2008 it had been returned to &#8220;Ctrl+Shift+B&#8221; (like VS2003).  At first, I was upset by this, but I did a little digging and was chagrined to find that my VS2005 Keyboard Settings had been changed from &#8220;Default&#8221; to &#8220;Visual C# 2005&#8243;.  I checked in VS2008, and sure enough when I changed the Mapping Scheme to &#8220;Visual C# 2005&#8243;, my beloved F6 had been restored. There are several options here (none of which is C# 2008).</p>
<p>In the process, I also found that you can set these Shortcuts yourself.  If you do not want to switch, but instead wish to customize the Default settings (which can always be restored later), follow these steps:</p>
<ol>
<li>Go to Tools -&gt; Options -&gt; Keyboard.</li>
<li>In &#8220;Show Commands Containing&#8221;, type &#8220;Window.NextSplitPane&#8221; (this is what F6 points to now &#8211; I found this out by first by skipping all the other steps and going directly to #8 below).</li>
<li>The Current Shortcut &#8220;F6&#8243; for &#8220;Window.NextSplitPane&#8221; will be shown in the &#8220;Shortcuts for Selected Command Box&#8221;.</li>
<li>Press the &#8220;Remove&#8221; button.</li>
<li>Go back to the &#8220;Show Commands Containing&#8221; and type &#8220;Build Solution&#8221;.</li>
<li>The Current Shortcut &#8220;Ctrl+Shift+B&#8221; for &#8220;Build.BuildSolution&#8221; will be shown in the &#8220;Shortcuts for Selected Command Box&#8221;.</li>
<li>You do NOT need to Remove this, but you can if you wish.  To do so, press the &#8220;Remove&#8221; button again.</li>
<li>Put your cursor in the &#8220;Press shortcutkeys&#8221; textbox and press the &#8220;F6&#8243; function key.</li>
<li>Press the &#8220;Assign&#8221; button.</li>
<li>Press the &#8220;OK&#8221; button to save your changes.</li>
</ol>
<p>Changes to the Keyboard Mapping Scheme should show immediately in the IDE.  You can have multiple shortcuts for a given function, but naturally a shortcut can only be assigned once.</p>
<p>Now, obviously, I had changed the Keyboard Mapping Scheme in VS2005, I just don&#8217;t remember doing it.  Visual Studio is just such an incredible tool with so many options: I&#8217;ll bet that most of us don&#8217;t know the half of it.  And something like this you may do once, to tweak your setup, and then never think about again.  At any rate, I hope this helps someone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/visual-studio/changing-visual-studio-shortcuts.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Upgrade your C# Skills part 2 &#8211; Compiler Inference, Object Initializers, and Anonymous Types</title>
		<link>http://www.developingfor.net/c-30/upgrade-your-c-skills-part-2-compiler-inference-object-initializers-and-anonymous-types.html</link>
		<comments>http://www.developingfor.net/c-30/upgrade-your-c-skills-part-2-compiler-inference-object-initializers-and-anonymous-types.html#comments</comments>
		<pubDate>Tue, 27 Nov 2007 22:00:22 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/upgrade-your-c-skills-part-2-compiler-inference-object-initializers-and-anonymous-types.html</guid>
		<description><![CDATA[Well, that Title sure is a mouthful!  &#8220;Compiler Inference&#8221;, &#8220;Object Initializers&#8221;, &#8220;Anonymous Types&#8221;: as fancy as these items sound, they are among some of the more mundane changes to C# 3.0.  Don&#8217;t get me wrong: these changes lay the groundwork for all the really cool stuff going on in C#, especially LINQ.  [...]]]></description>
			<content:encoded><![CDATA[<p>Well, that Title sure is a mouthful!  &#8220;Compiler Inference&#8221;, &#8220;Object Initializers&#8221;, &#8220;Anonymous Types&#8221;: as fancy as these items sound, they are among some of the more mundane changes to C# 3.0.  Don&#8217;t get me wrong: these changes lay the groundwork for all the really cool stuff going on in C#, especially LINQ.  Truthfully, I should have covered these first, but I had actually been <em>dreaming</em> about Extension Methods, so I hope you&#8217;ll understand if I had to get that out of my system!</p>
<p><strong>Compiler Inference</strong></p>
<p>It probably isn&#8217;t fair for me to say this is &#8220;mundane&#8221;, but look at the two examples below:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// Old Way</span><br />
Person p = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">string</span> name = p.<span class="me1">LastName</span> + <span class="st0">&quot; &quot;</span> + p.<span class="me1">FirstName</span>;</p>
<p><span class="co1">// New Way</span><br />
var p = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
var name = p.<span class="me1">LastName</span> + <span class="st0">&quot; &quot;</span> + p.<span class="me1">FirstName</span>;</div>
<p>Doesn&#8217;t seem like much, does it?  All we did was replace the explicit Type with a new keyword called <strong>var</strong>.  When you see var, what you are seeing is an instruction to the compiler to figure out for itself what Type the resulting variable will be.  This is NOT var like in PHP or other loosely typed languages.  Once assigned by the compiler, all the Strongly Typed rules still apply.  And var can only be used for local variables, so it won&#8217;t be replacing all type references any time soon.</p>
<p>In the example above, &#8220;var p = new Person();&#8221;, the compiler will infer from the return type that variable &#8220;p&#8221; should be of type &#8220;Person&#8221;. Granted, in this simple example it doesn&#8217;t really mean much.  It does mean less typing, especially when looping through generic collections, but where it will really come into play is with Anonymous Types and LINQ.  In fact, without Compiler Inference, Anonymous Types could not function, but more on that later in the article.</p>
<p>There is some debate going on as to when to use Compiler Inference. Some feel that using var whenever possible is just lazy and leads to less readable code.  These people think that you should use it only when it is really needed.  In other words, if I know p is going to be a Person, then why not say so explicitly?  Personally, I don&#8217;t have an opinion.  I&#8217;ll use it when it seems convenient and of course where it is required.  It certainly makes looping through Collections nicer to write:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// assume people is a List&lt;Person&gt;</span><br />
foreach <span class="br0">&#40;</span>var person in people<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;{0} {1} is {2} years old.&quot;</span>, person.<span class="me1">FirstName</span>, person.<span class="me1">LastName</span>, person.<span class="me1">Age</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I have to think that if Person is later subclassed, fewer changes would be required to make this code continue to function.  Is it lazy? Perhaps, but we all know that a good programmer is a lazy thief!</p>
<p><strong>Object Initializers</strong></p>
<p>Another cool feature are Object Initializers.  Initializers allow you to set a series of Property variables when you create the object.  How many times have you coded something like this:</p>
<div class="dean_ch" style="white-space: nowrap;">Person p = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
p.<span class="me1">FirstName</span> = <span class="st0">&quot;John&quot;</span>;<br />
p.<span class="me1">LastName</span> = <span class="st0">&quot;Smith&quot;</span>;<br />
p.<span class="me1">Age</span> = <span class="nu0">32</span>;</div>
<p>Now, you can shorten this considerably.  Sort of like Array Initializers, which I&#8217;m sure we&#8217;ve all used, the syntax is a list of property=value pairs inside curly braces immediately following a constructor:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// You could, of course, use var here&#8230;</span><br />
Person p = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;John&quot;</span>, LastName = <span class="st0">&quot;Smith&quot;</span>, Age = <span class="nu0">32</span><span class="br0">&#125;</span>;</div>
<p>An interesting side note about IntelliSense: naturally, inside the initializer block, IntelliSense will show you the list of properties available for the Person object. As you use Properties in the Initializer block, you will notice that they disappear from IntelliSense.  This is a quick way to make sure you set every property (if you need to), and should help prevent listing duplicate properties (which, incidentally, throws a compiler error).</p>
<p>Now, for some extra coolness, you can embed Object Initialization inside a Collection&#8217;s Object Initializing block, so that this:</p>
<div class="dean_ch" style="white-space: nowrap;">List&lt;person&gt; people = new List&lt;person&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
Person p = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
p.<span class="me1">FirstName</span> = <span class="st0">&quot;John&quot;</span>;<br />
p.<span class="me1">LastName</span> = <span class="st0">&quot;Smith&quot;</span>;<br />
p.<span class="me1">Age</span> = <span class="nu0">32</span>;<br />
people.<span class="me1">Add</span><span class="br0">&#40;</span>p<span class="br0">&#41;</span>;</p>
<p>Person p2 = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
p2.<span class="me1">FirstName</span> = <span class="st0">&quot;Jimmy&quot;</span>;<br />
p2.<span class="me1">LastName</span> = <span class="st0">&quot;Crackcorn&quot;</span>;<br />
p2.<span class="me1">Age</span> = <span class="nu0">57</span>;<br />
people.<span class="me1">Add</span><span class="br0">&#40;</span>p2<span class="br0">&#41;</span>;</p>
<p>Person p3 = new Person<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
p3.<span class="me1">FirstName</span> = <span class="st0">&quot;Mary&quot;</span>;<br />
p3.<span class="me1">LastName</span> = <span class="st0">&quot;Contrary&quot;</span>;<br />
p3.<span class="me1">Age</span> = <span class="nu0">44</span>;<br />
people.<span class="me1">Add</span><span class="br0">&#40;</span>p3<span class="br0">&#41;</span>;</div>
<p>Can now be done like this:</p>
<div class="dean_ch" style="white-space: nowrap;">var people = new List&lt;person&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span>new Person<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;John&quot;</span>, LastName = <span class="st0">&quot;Smith&quot;</span>, Age = <span class="nu0">32</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp;new Person<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;Jimmy&quot;</span>, LastName = <span class="st0">&quot;Crackcorn&quot;</span>, Age = <span class="nu0">57</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp;new Person<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;Mary&quot;</span>, LastName = <span class="st0">&quot;Contrary&quot;</span>, Age = <span class="nu0">44</span><span class="br0">&#125;</span><span class="br0">&#125;</span>;</div>
<p>As you can see, this is very concise and readable code.  Less Typing = More Cool!</p>
<p><strong>Automatic Properties</strong></p>
<p>Since we are talking about properties, at least in a round about way, I want to throw in a quick item you may not be aware of called Automatic Properties.  We all know that we are supposed to be exposing access to our variables via properties, but a lot of the time all we want to do is to be able to read and write the variable data.  As such, we end up with a lot of code that looks like this:</p>
<div class="dean_ch" style="white-space: nowrap;">private <span class="kw4">string</span> _firstName;<br />
public <span class="kw4">string</span> FirstName<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; get <span class="br0">&#123;</span> <span class="kw1">return</span> _firstName; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; set <span class="br0">&#123;</span> _firstName = value; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Now, there is nothing wrong with this approach, but it feels a little verbose.  To address this, you can now use an Automatic Property:</p>
<div class="dean_ch" style="white-space: nowrap;">public <span class="kw4">string</span> FirstName <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
<p>The two main differences are that you do not define the variable, and you do not have to write the getter and setter logic.  In fact, you&#8217;ll notice there aren&#8217;t even curly braces for get or set.  The upside is that it is again less code for a common function.  The downside is that you do not have an internal variable to access: you must use the Property name, even internally.  Also, the Property must contain both the get and the set, so you cannot have a read-only Automatic Property.  You also cannot add custom logic: get and set default behaviors are it.</p>
<p>The plus, though, is that if you start with an Automatic Property and need to add any of this functionality, you simply create the variable and update the Property as you would have before.  There is nothing magic about Automatic Properties, this is just another example of the Compiler doing menial work for you. One last tidbit: in VS2008, if you use the &#8220;prop&#8221; code snippet, you will get the format of an Automatic Property by default.  It really replaces the need for my <a title="Property Builder" href="http://www.developingfor.net/free-code/propertybuilder">Property Builder</a> tool.</p>
<p><strong>Anonymous Types</strong></p>
<p>When I first learned about Anonymous Types, I didn&#8217;t think it would be all that big of a deal.  The more I think about it though, the more I can see a use for it.  And when I saw how they are used with LINQ, I was sold.  Basically, you can define a Type without naming it or creating a separate class definition.  Let&#8217;s revisit our Person and People class from before, only this time I am not going to define the Person Class:</p>
<div class="dean_ch" style="white-space: nowrap;">var p1 = new <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;John&quot;</span>, LastName = <span class="st0">&quot;Smith&quot;</span>, Age = <span class="nu0">32</span>, FirstName=<span class="st0">&quot;Harold&quot;</span><span class="br0">&#125;</span>;<br />
var p2 = new <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;Jimmy&quot;</span>, LastName = <span class="st0">&quot;Crackcorn&quot;</span>, Age = <span class="nu0">57</span><span class="br0">&#125;</span>;<br />
var p3 = new <span class="br0">&#123;</span>FirstName = <span class="st0">&quot;Mary&quot;</span>, LastName = <span class="st0">&quot;Contrary&quot;</span>, Age = <span class="nu0">44</span><span class="br0">&#125;</span>;</div>
<p>Here I have created three instances of my new Type, but I have not explicitly defined the type definition.  Instead, the compiler has created a class for me and given it the Properties I listed (using our new friend, Compiler Inference).  All of the properties are read-only, so once created, the values cannot be changed.   Since the three objects above are all identical in their property names, all three objects are of the same (unknown) type.  And IntelliSense still works as well: Typing &#8220;p1.&#8221; will reveal a list of the properties.  I said earlier in the article that Anonymous Types could not function without Compiler Inference: hopefully it is obvious now what is happening above.  &#8220;var&#8221; is the only option here, because the Type of the variable <em>does not exist</em> at design time.  The Compiler must infer the Type to be of whatever class the Compiler creates, otherwise, you could not have Anonymous Types in a language like C# because of its strong typing.</p>
<p>Of course, there is a catch: as of yet, I have not figured out a way to use these in a list.  You cannot use a Generic list, because there is no Type for strong typing.  You can store them as Objects, but then you cannot cast them back out because there is no type.  So what does this particular method buy you?  In my opinion, not much.  There could be an argument for using this method to replace the use of temporary Structs, a method I use frequently to store grouped information.  But since I can&#8217;t store them in a list or pass these objects around, it would be a very limited approach.</p>
<p>There is, however, a necessary use for Anonymous Types: LINQ requires them to be able to create custom query results as a Collection.  We&#8217;ll explore that more later this week, so be sure to check back.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/c-30/upgrade-your-c-skills-part-2-compiler-inference-object-initializers-and-anonymous-types.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Upgrade your C# Skills part 1 &#8211; Extension Methods</title>
		<link>http://www.developingfor.net/c-30/upgrade-your-c-skills-part-1-extension-methods.html</link>
		<comments>http://www.developingfor.net/c-30/upgrade-your-c-skills-part-1-extension-methods.html#comments</comments>
		<pubDate>Mon, 26 Nov 2007 22:15:33 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Free Code]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/upgrade-your-c-skills-part-1-extension-methods.html</guid>
		<description><![CDATA[DOWNLOAD the Code!
Now that I have VS2008 and .NET 3.5 installed, I am going to begin a series of articles on some of the new features you can use in C#.  My hope is to add one new article a day for the rest of this week.  Along the way, we&#8217;ll explore some [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.developingfor.net/free-code/developingfordotnetextensions" title="DevelopingForDotNet.Extensions">DOWNLOAD</a> the Code!</p>
<p>Now that I have VS2008 and .NET 3.5 installed, I am going to begin a series of articles on some of the new features you can use in C#.  My hope is to add one new article a day for the rest of this week.  Along the way, we&#8217;ll explore some of the new language features for C# 3.0.  Truthfully, they aren&#8217;t all that new since C# 3.0 has been around for about a year, but support for them is now built in to Visual Studio, so using them is now realistic.  Also, with the release of VS208, I&#8217;m sure this will be the first time most developers will be exposed to them. I&#8217;m going to start by introducing one of my favorite enhancements: Extension Methods.</p>
<p><strong>Extension Methods</strong></p>
<p>Extension methods offer us a way to expand class functionality even if we do not have access to the code for those classes (including <em>sealed </em>classes).  In other words, we can add our own functionality to any object type.  Have you ever looked at the String class and said &#8220;why can&#8217;t I do {fill in the blank} with a String?&#8221;  If so, you probably created your own method, passed it the string in question, and consumed the return value:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw4">string</span> s = <span class="st0">&quot;1234&quot;</span>;<br />
<span class="kw4">string</span> z = Reverse<span class="br0">&#40;</span>s<span class="br0">&#41;</span>;</div>
<p>If you&#8217;ve ever written code like this, then Extension Methods are for you.  In the case above, it should be obvious that Reverse is a static method, since it is not attached to an instance.  Wouldn&#8217;t it be nice instead to write this?</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw4">string</span> s = <span class="st0">&quot;1234&quot;</span>;<br />
<span class="kw4">string</span> z = s.<span class="me1">Reverse</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>I know it is a subtle difference, and in this case not a terribly functional one, but hopefully you can see the difference.  Instead of passing the string variable to a static method, you can treat your methods as though they belong to the string class.  And these new methods are available in Intellisense: when I type &#8220;s.&#8221;, I will see my extension methods right alongside all the native methods, which makes finding and using them much more palatable.  And you will find in the new Intellisense that Microsoft itself is making heavy use of Extension Methods.  You can tell in two ways: first, the icon for extension methods is the familiar purple box but accented with a blue down arrow.  Secondly, when the method description pops up in Intellisense, the description is preceded by &#8220;(extension)&#8221;.</p>
<p>Let&#8217;s look at a more reasonable example. Using the <em>Regex</em> class, you can determine whether or not string matches a Regular Expression pattern by passing a string and a pattern to the <em>Regex.IsMatch()</em> method:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw4">string</span> s = <span class="st0">&quot;This is awesome&quot;</span>;<br />
<span class="kw1">if</span> <span class="br0">&#40;</span>Regex.<span class="me1">IsMatch</span><span class="br0">&#40;</span>s, <span class="st0">&quot;awe&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Yep, this is a match!&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Sorry, no match.&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I think it would be handy on occasion to simply &#8220;ask&#8221; the string itself if it matches a certain pattern:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw4">string</span> s = <span class="st0">&quot;This is awesome&quot;</span>;<br />
<span class="kw1">if</span> <span class="br0">&#40;</span>s.<span class="me1">IsRegexMatch</span><span class="br0">&#40;</span><span class="st0">&quot;awe&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Yep, this is a match!&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Sorry, no match.&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>Pretty neat, huh?  OK, I admit it doesn&#8217;t appear to lessen your code, but to me it can make your code make more syntactic sense.  And given a more complex example, it could do a lot for you.  <a href="http://www.richardhaleshawgroup.com/RHSGroup/DesktopDefault.aspx" title="Richard Hale Shaw" target="_blank">Richard Hale Shaw</a> showed us an example of a <em>.ForEach</em> extension for <em>IEnumerable&lt;T&gt;</em> collections that would knock your socks off!  Imagine being able to loop through an entire Collection and perform some action on each item in a single line of code, without passing the Collection off to a method?  It would look something like this:</p>
<div class="dean_ch" style="white-space: nowrap;">DirectoryInfo dir = new DirectoryInfo<span class="br0">&#40;</span><span class="st0">&quot;C:<span class="es0">\\</span>&quot;</span><span class="br0">&#41;</span>;<br />
FileInfo<span class="br0">&#91;</span><span class="br0">&#93;</span> files = dir.<span class="me1">GetFiles</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
files.<span class="me1">ForEach</span>&lt;fileinfo&gt;<span class="br0">&#40;</span>f =&gt; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;{0} {1}&quot;</span>, f.<span class="me1">FullName</span>, f.<span class="me1">Length</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>For now, ignore the code between the () &#8211; that&#8217;s a Lambda Expression, and we&#8217;ll get to those later in the week.  Just understand that with this sample above, each FileInfo object in the array will have the passed Action applied to it.  How many <em>foreach</em> loops do you think this little nugget could eliminate?</p>
<p>Now, I hope this will get you interested in Extension methods: it didn&#8217;t take me too long to think these are very cool!  To get you started, I&#8217;m adding a new project called <a href="http://www.developingfor.net/free-code/developingfordotnetextensions" title="DevelopingForDotNet.Extensions">DevelopingForDotNet.Extensions</a> to the <a href="http://www.developingfor.net/free-code" title="Free Code!">Free Code</a> page .  My take on Richard&#8217;s method(s) are included, along with a handful of String and Numeric operations.  Nothing too fancy, but hopefully enough to help get you started.</p>
<p><strong>Enough already, how do I do it?</strong></p>
<p>Creating Extension Methods is very simple.  First, you must follow these simple rules:</p>
<ol>
<li>Extension Methods must be <strong>static</strong>, defined in a <strong>static</strong> class</li>
<li>Extension Methods must be <strong>public</strong></li>
<li>The <strong>this</strong> keyword precedes the first parameter</li>
</ol>
<p>We&#8217;ll take these one at a time.  First, the method must be static because of how the compiler handles extensions.  Behind the scenes, whenever an Extension Method is employed, the compiler actually generates code to call the static method.  The idea of the Extension Method is just a visual layer for the developer: behind the scenes the actual static method is being called.  Also, these methods can be called in a static fashion, rather than as methods attached to an instance.  Which brings up another good point: the class name the Extension Methods are in is essentially irrelevant (unless you are going to call them explicitly).  I put all my extensions in a single static class (imaginatively called &#8220;ExtensionMethods&#8221;).</p>
<p>Second, they probably do not absolutely have to be public, but if they aren&#8217;t then you are severely limiting the functional scope, so what&#8217;s the point?</p>
<p>Finally, preceding the first parameter with &#8220;this&#8221; is what tells the compiler that this is an Extension Method.  It is also probably going to be the main source of initial confusion, because you do not pass this parameter (unless you are calling the method explicitly).</p>
<p>Overall, these are fairly simple rules to follow.  Here is a handy method I&#8217;ve used for a while:</p>
<div class="dean_ch" style="white-space: nowrap;">public <span class="kw4">static</span> <span class="kw4">string</span> RightAdjust<span class="br0">&#40;</span><span class="kw4">string</span> s, <span class="kw4">int</span> Size, <span class="kw4">char</span> FillCharacter<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">string</span> ch = FillCharacter.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>s.<span class="me1">Length</span> &gt; Size<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException<span class="br0">&#40;</span><span class="st0">&quot;Size of Value larger than Requested Size.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>s.<span class="me1">Length</span> &lt; Size<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; s = ch + s;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> s;<br />
<span class="br0">&#125;</span></div>
<p>This method receives a string and right adjusts it to the given size, filling the leading characters in with the passed char.  Calling it looks like this:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw4">string</span> s = <span class="st0">&quot;1234&quot;</span>;<br />
s = RightAdjust<span class="br0">&#40;</span>s, <span class="nu0">7</span>, <span class="st0">&#8216;0&#8242;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// Value of s is now &quot;0001234&quot;</span></div>
<p>Now, let&#8217;s convert this to an Extended Method:</p>
<div class="dean_ch" style="white-space: nowrap;">public <span class="kw4">static</span> <span class="kw4">string</span> RightAdjust<span class="br0">&#40;</span>this <span class="kw4">string</span> s, <span class="kw4">int</span> Size, <span class="kw4">char</span> FillCharacter<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">string</span> ch = FillCharacter.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>s.<span class="me1">Length</span> &gt; Size<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException<span class="br0">&#40;</span><span class="st0">&quot;Size of Value larger than Requested Size.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>s.<span class="me1">Length</span> &lt; Size<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; s = ch + s;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> s;<br />
<span class="br0">&#125;</span></div>
<p>All we did was add &#8220;this&#8221; before the first parameter.  Now we can call it like so:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw4">string</span> s = <span class="st0">&quot;1234&quot;</span>;<br />
s = s.<span class="me1">RightAdjust</span><span class="br0">&#40;</span><span class="nu0">7</span>, <span class="st0">&#8216;0&#8242;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// Value of s is now &quot;0001234&quot;</span></div>
<p>Naturally, you will need to add a reference to the DLL that contains your Extension Methods in order to find and use them.</p>
<p><strong>Overloading Extension Methods</strong>:</p>
<p>Just like other methods, Extension Methods can be easily overloaded.  My approach for overloading has always been to put all the functionality in the method that requires the most parameters.  I then simply have my overloading method signatures call the primary method, sending it the appropriate parameter values.  Looking at the <em>RightAdjust</em> method above, I want to establish a method that will use a blank character as the default fill character if one is not supplied.  What&#8217;s different about overloading extension methods, is that I actually employ the primary extension method in my overloading methods:</p>
<div class="dean_ch" style="white-space: nowrap;">public <span class="kw4">static</span> <span class="kw4">string</span> RightAdjust<span class="br0">&#40;</span>this <span class="kw4">string</span> s, <span class="kw4">int</span> Size<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> s.<span class="me1">RightAdjust</span><span class="br0">&#40;</span>Size, <span class="st0">&#8216; &#8216;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>So now I can call this method passing it just the Size parameter, and that method then calls the primary method using the Extension Method mechanism.</p>
<p><strong>Conclusion:</strong></p>
<p>Extension Methods can be as simple or complex as you like.  They are a nice syntactical enhancement to the language that lets you enhance other objects and use them how you would like.  But beware: you could easily go overboard.  I mean, there is no reason to create a <em>MakeDirectory</em> method for a <em>TimeStamp</em> instance, but you could.  Just use common sense and make sure that the extensions you create apply to the object type and way that you would use them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/c-30/upgrade-your-c-skills-part-1-extension-methods.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Installing VS2008</title>
		<link>http://www.developingfor.net/visual-studio/installing-vs2008.html</link>
		<comments>http://www.developingfor.net/visual-studio/installing-vs2008.html#comments</comments>
		<pubDate>Wed, 21 Nov 2007 22:41:05 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/installing-vs2008.html</guid>
		<description><![CDATA[FINALLY!  After 5 hours and 45 minutes of downloading, the ISO file is here!  I also downloaded Microsoft&#8217;s VirtualCD software, mounted the ISO file, and began the Install process at 3:01pm EST.
First, after mindlessly bypassing the EULA, I saved 1.6 GB of install space on my machine by not installing C++ or Crystal [...]]]></description>
			<content:encoded><![CDATA[<p>FINALLY!  After 5 hours and 45 minutes of downloading, the ISO file is here!  I also downloaded Microsoft&#8217;s VirtualCD software, mounted the ISO file, and began the Install process at 3:01pm EST.</p>
<p>First, after mindlessly bypassing the EULA, I saved 1.6 GB of install space on my machine by not installing C++ or Crystal Reports.  I&#8217;ve had C++ since I first installed<!-- Traffic Statistics --> <!-- End Traffic Statistics --> VS2003 Pro and have never once used it, same for Crystal Reports, so why bother?  I could have saved more but not installing SQL Server, but I figured better safe than sorry.</p>
<p>A few things about this machine while the installer runs:</p>
<ul>
<li>IBM ThinkPad G41, about two years old, with some kind of Pentium IV Mobile processor.</li>
<li>Running Windows XP Pro SP2.</li>
<li>It had a little over 8 GB free when I started.</li>
<li>1 GB RAM.</li>
<li>Already installed are VS2005, Mobile 5.0 SDK, .NET 3.0 (and 2.0 and 1.1)</li>
</ul>
<p>4:18pm -</p>
<p>The install is finished.  I had to reboot, which meant remounting the Virtual CD image.  Also, I cannot install the MSDN Documentation: the installer will not recognize the ISO image as the correct disc.  Perhaps there is a separate download from MSDN for the docs.</p>
<p>At any rate, it seems I am up and running.  I opened the IDE for the first time and selected C# as my primary environment.  The software configured and opened a few minutes later.</p>
<p>So all in all, the installation was a breeze (except for the waiting for the download part).  Now on to Blend!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/visual-studio/installing-vs2008.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008 goes RTM!</title>
		<link>http://www.developingfor.net/net-35/visual-studio-2008-goes-rtm.html</link>
		<comments>http://www.developingfor.net/net-35/visual-studio-2008-goes-rtm.html#comments</comments>
		<pubDate>Tue, 20 Nov 2007 22:24:30 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.developingfor.net/visual-studio/visual-studio-2008-goes-rtm.html</guid>
		<description><![CDATA[OK, so I may have lied, but not by much!
Yesterday afternoon, while waiting for my MSDN number to be granted, I saw on MSDN that VS2008 went RTM!  I did finish the VS2008 Beta 2 download, but I never installed it. After waiting all day, I was finally able tog et my MSDN number from [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so I may have lied, but not by much!</p>
<p>Yesterday afternoon, while waiting for my MSDN number to be granted, I saw on MSDN that VS2008 went RTM!  I did finish the VS2008 Beta 2 download, but I never installed it. After waiting all day, I was finally able tog et my MSDN number from Microsoft and am currently downloading the VS2008 ISO file.  Tomorrow will be install day!</p>
<p>But since I am here, let me grouse a bit: why in the world does it take 24-72 hours for a couple of databases at Microsoft to share information?  I just can&#8217;t believe that there is this kind of delay.  One of the MSDN techs told me that it takes one day to get the data into the &#8220;Subscriber&#8221; database, and another to get it into the &#8220;Downloads&#8221; database. What gives? Microsoft of all companies shoul dbe able to make this sort of event automatic and practically instantaneous!  It just boggles the mind a little.</p>
<p>But hey, who cares!  I&#8217;ll soon be in .NET 3.5, and I hope you&#8217;ll join me there soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developingfor.net/net-35/visual-studio-2008-goes-rtm.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
