<?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>Meatball&#039;s Guide to .NET &#187; C#</title>
	<atom:link href="http://rogerpence.com/blog/index.php/archives/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://rogerpence.com/blog</link>
	<description>A dogma-free guide to making real-world sense of .NET</description>
	<lastBuildDate>Wed, 23 Jun 2010 16:15:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting to know Resharper: Generate a constructor</title>
		<link>http://rogerpence.com/blog/index.php/archives/230</link>
		<comments>http://rogerpence.com/blog/index.php/archives/230#comments</comments>
		<pubDate>Tue, 04 Aug 2009 20:03:43 +0000</pubDate>
		<dc:creator>rp</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Resharper]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/?p=230</guid>
		<description><![CDATA[Resharper, the terrific Visual Studio productivity plugin, reduces adding a constructor for class, with as many arguments as you need, to just a few mouse clicks.Here’s how to use this Resharper feature.
Figure 1a. Class without a constructor.

  1public class Artist
  2{
  3    public int ArtistId { get; set; }
 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jetbrains.com/resharper/index.html">Resharper</a>, the terrific Visual Studio productivity plugin, reduces adding a constructor for class, with as many arguments as you need, to just a few mouse clicks.Here’s how to use this Resharper feature.</p>
<div class="sourceCodeHeading">Figure 1a. Class without a constructor.</div>
<div id="Id1046440468SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">public class Artist</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">    public int ArtistId { get; set; }</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">    public string Name { get; set; }</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">    public int Rank { get; set; }</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">}</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1046440468SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1046440468SourceCode' );">Show copy-friendly code</a></div>
<p>Put the cursor somewhere in the body of a class that needs a constructor and press Alt/Ins (or use the Resharper&gt;Code&gt;Generate menu option). This displays Resharper’s <em>Generate</em> context menu (as shown below). Click “<em>Constructor</em>.”</p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/10/ContextMenu1.jpg"><img style="border-right-width: 0px; margin: 0px 20px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ContextMenu" border="0" alt="ContextMenu" src="http://rogerpence.com/blog/wp-content/uploads/2009/10/ContextMenu_thumb1.jpg" width="212" height="211" /></a> </p>
<h3>Coding with Resharper is almost like pair programming but minus the gum-chewing, Jonas Brother-listening, obnoxious coder next to you.</h3>
<p>This shows the <em>Generate constructors</em> dialog below. Select the members you want as arguments to the constructor with the checkboxes (selecting the class name selects all of the fields for use as constructor parameters). You can also select the access rights (public, private, et al) as well as whether you want an XML comment block added. Resharper is context sensitive&#8211;when using it to inject a constructor in an abstract class it’s smart enough to mark that constructor<em> protected</em>. Why? <a href="http://msdn.microsoft.com/en-us/library/ms229047.aspx">Read this.</a> For my money, providing this level of awareness is how Resharper elevates itself from being a simple code paster to being a true ally in helping you write better code. It’s almost like pair programming but minus the gum-chewing, Jonas Brother listening, obnoxious coder next to you.</p>
<p>The “Advanced” button lets you specify what arguments are passed on to a base constructor (if any are available).</p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/10/generate.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="generate" border="0" alt="generate" src="http://rogerpence.com/blog/wp-content/uploads/2009/10/generate_thumb.jpg" width="335" height="335" /></a> </p>
<p>Assuming that the ArtistId and Name fields were selected, when you click “Finish” the constructor shown below is immediately added to the class.</p>
<div class="sourceCodeHeading">Figure 1b. Class with constructor automatically added.</div>
<div id="Id1059101875SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">public class Artist</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">    public Artist( string name, int artistId )</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">    {</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">        Name = name;</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">        ArtistId = artistId;</span></pre>
<pre class="even"><span class="ln">  7</span><span class="code">    }</span></pre>
<pre class="odd"><span class="ln">  8</span><span class="code"></span></pre>
<pre class="even"><span class="ln">  9</span><span class="code">    public int ArtistId { get; set; }</span></pre>
<pre class="odd"><span class="ln"> 10</span><span class="code">    public string Name { get; set; }</span></pre>
<pre class="even"><span class="ln"> 11</span><span class="code">    private int Rank { get; set; }</span></pre>
<pre class="odd"><span class="ln"> 12</span><span class="code">}</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1059101875SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1059101875SourceCode' );">Show copy-friendly code</a></div>
<p>As you can see, Resharper makes it ridiculously easy to automate constructor creation, with optional parameters and value assignments. Although my examples show Resharper working with C#, there is a VB.NET version of Resharper available.</p>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/230/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Linq to create an ADO.NET connection string</title>
		<link>http://rogerpence.com/blog/index.php/archives/197</link>
		<comments>http://rogerpence.com/blog/index.php/archives/197#comments</comments>
		<pubDate>Wed, 22 Jul 2009 16:57:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[LinqPad]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/index.php/archives/197</guid>
		<description><![CDATA[I know I’m a little late to the party, but I am quickly falling in love with Linq. Once you start adopting its functional mindset, it is so expressive and direct. 
I once heard someone way that once you really get Linq, you’ll won’t ever need to write a For/Each loop again. Probably an overstatement, [...]]]></description>
			<content:encoded><![CDATA[<p>I know I’m a little late to the party, but I am quickly falling in love with Linq. Once you start adopting its functional mindset, it is so expressive and direct. </p>
<p>I once heard someone way that once you really get Linq, you’ll won’t ever need to write a For/Each loop again. Probably an overstatement, but still I was intrigued. I set out last night to see how Linq could negate the need for For/Each. </p>
<p>I set out to find a way to create ADO.NET connection strings. In the past, I would have probably used foreach to iterate over a collection using StringBuilder and String.Format to build up the connection string. I dispensed with all that old-school thought and created the connection string as shown below in Figure 1. As a bonus, I also learned (thanks to a little prompting from <a href="http://www.jetbrains.com/resharper/">Resharper</a>) a shortcut for initializing a collection.</p>
<p>The Linq code in lines 10 and 11 is where the cool stuff happens. Each element from the dictionary is selected to create a value pair and the Linq Aggregate() operator is used to concatenate all of the value pairs. All very sweet! </p>
<div class="sourceCodeHeading">Figure 1. Using Linq to create an ADO.NET connection string</div>
<div id="Id1106513593SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">var o = new Dictionary&lt; string, string &gt;()</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">	{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">		{ &quot;Data Source&quot;,           &quot;DUFFSQLSERVER2008&quot; },</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">		{ &quot;Initial Catalog&quot;,       &quot;Chinook&quot; },</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">		{ &quot;Persist Security Info&quot;, &quot;True&quot; },</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">		{ &quot;User ID&quot;,               &quot;xxxxx&quot; },</span></pre>
<pre class="even"><span class="ln">  7</span><span class="code">		{ &quot;Password&quot;,              &quot;xxxxx&quot; }</span></pre>
<pre class="odd"><span class="ln">  8</span><span class="code">	};    </span></pre>
<pre class="even"><span class="ln">  9</span><span class="code"></span></pre>
<pre class="odd"><span class="ln"> 10</span><span class="code">var ConnectionString = o.Select( i =&gt; i.Key + &quot;=&quot; + i.Value ).</span></pre>
<pre class="even"><span class="ln"> 11</span><span class="code">                         Aggregate( ( a, b ) =&gt; a + &quot;;&quot; + b );</span></pre>
<pre class="odd"><span class="ln"> 12</span><span class="code"></span></pre>
<pre class="even"><span class="ln"> 13</span><span class="code">Console.WriteLine( ConnectionString );</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1106513593SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1106513593SourceCode' );">Show copy-friendly code</a></div>
<p>I used <a href="http://www.linqpad.net/">LinqPad</a> to create and test this code. I just started using LinqPad and can’t believe I ever coded without it. Beyond being a great test bed for Linq, it is a superb snippet compiler. LinqPad is a free download and should be in every coder’s toolbox. </p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/07/linqpad1.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="linqpad" border="0" alt="linqpad" src="http://rogerpence.com/blog/wp-content/uploads/2009/07/linqpad-thumb1.jpg" width="775" height="489" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/197/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting a list of installed Windows printers</title>
		<link>http://rogerpence.com/blog/index.php/archives/172</link>
		<comments>http://rogerpence.com/blog/index.php/archives/172#comments</comments>
		<pubDate>Wed, 03 Jun 2009 16:57:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[printing]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/index.php/archives/172</guid>
		<description><![CDATA[For printing tasks, it’s common to need to fetch the list of currently installed printers on a given PC. The Windows’ PrintDialog does display available printers—but it’s generally intended to be used in the general workflow of printing a document. The PrintDialog is also no help whatsoever for browser-hosted intranet apps where a user needs [...]]]></description>
			<content:encoded><![CDATA[<p>For printing tasks, it’s common to need to fetch the list of currently installed printers on a given PC. The Windows’ PrintDialog does display available printers—but it’s generally intended to be used in the general workflow of printing a document. The PrintDialog is also no help whatsoever for browser-hosted intranet apps where a user needs to select, and then print, to a given network printer. What I wanted was an easy way to show users available printers; and I wanted this technique to work in either Windows or browser clients.</p>
<p>For example, the other day I worked on a task where the requirement was to be able to batch print documents (Word and Excel mostly, but also images). In this case, the PrintDialog wasn’t much help. I wanted to provide an easy way for users to select the target printer without all of the ceremony of the PrintDialog. </p>
<p>To perform this task, I wrote the code shown in Figure 1. It provides two classes: </p>
<ul>
<li><strong>PrinterCommon</strong>. This class provides a GetInstalledPrintersList() method that returns an instance of a class that provides a string array of printer names and an integer value indicating which of the printers in the list is the default printer. (I named this class PrinterCommon because I intend later to add other common print tasks to it.) </li>
<li><strong>InstalledPrinters</strong>. This class is the result type returned from from GetInstalledPrinterList(). While I typically don’t like to put more than one class in a source member, given the tight coupling between PrinterCommon and InstalledPrinters, that seemed like an OK shortcut for this simple task. </li>
</ul>
<div class="sourceCodeHeading">Figure 1. Code to get a list of installed printers.</div>
<div id="Id1230308437SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">using System;</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">using System.Drawing.Printing;</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code"></span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">namespace rp</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">{</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">    /// &lt;summary&gt;</span></pre>
<pre class="even"><span class="ln">  7</span><span class="code">    /// Common print and printer tasks.</span></pre>
<pre class="odd"><span class="ln">  8</span><span class="code">    /// &lt;/summary&gt;</span></pre>
<pre class="even"><span class="ln">  9</span><span class="code">    internal class PrinterCommon</span></pre>
<pre class="odd"><span class="ln"> 10</span><span class="code">    {</span></pre>
<pre class="even"><span class="ln"> 11</span><span class="code">        /// &lt;summary&gt;</span></pre>
<pre class="odd"><span class="ln"> 12</span><span class="code">        /// Get the list of installed printers.</span></pre>
<pre class="even"><span class="ln"> 13</span><span class="code">        /// &lt;/summary&gt;</span></pre>
<pre class="odd"><span class="ln"> 14</span><span class="code">        /// &lt;returns&gt;An instance of InstalledPrinters.&lt;/returns&gt;</span></pre>
<pre class="even"><span class="ln"> 15</span><span class="code">        public static InstalledPrinters GetInstalledPrinterList()</span></pre>
<pre class="odd"><span class="ln"> 16</span><span class="code">        {</span></pre>
<pre class="even"><span class="ln"> 17</span><span class="code">            // Get default printer name.</span></pre>
<pre class="odd"><span class="ln"> 18</span><span class="code">            string currentPrinter = new PrinterSettings().PrinterName;</span></pre>
<pre class="even"><span class="ln"> 19</span><span class="code"></span></pre>
<pre class="odd"><span class="ln"> 20</span><span class="code">            // Declare a string array to store the list of installed printers.</span></pre>
<pre class="even"><span class="ln"> 21</span><span class="code">            var installedPrinters =</span></pre>
<pre class="odd"><span class="ln"> 22</span><span class="code">                new string[ PrinterSettings.InstalledPrinters.Count ];</span></pre>
<pre class="even"><span class="ln"> 23</span><span class="code"></span></pre>
<pre class="odd"><span class="ln"> 24</span><span class="code">            // Copy the list of installed printers into the installedPrinters</span></pre>
<pre class="even"><span class="ln"> 25</span><span class="code">            // array (starting at element zero).</span></pre>
<pre class="odd"><span class="ln"> 26</span><span class="code">            PrinterSettings.InstalledPrinters.CopyTo( installedPrinters, 0 );</span></pre>
<pre class="even"><span class="ln"> 27</span><span class="code"></span></pre>
<pre class="odd"><span class="ln"> 28</span><span class="code">            // Get index of default printer.</span></pre>
<pre class="even"><span class="ln"> 29</span><span class="code">            int i = Array.IndexOf( installedPrinters, currentPrinter );</span></pre>
<pre class="odd"><span class="ln"> 30</span><span class="code"></span></pre>
<pre class="even"><span class="ln"> 31</span><span class="code">            // Return with a new instance of InstalledPrinters.</span></pre>
<pre class="odd"><span class="ln"> 32</span><span class="code">            return new InstalledPrinters() { PrinterNames = installedPrinters, </span></pre>
<pre class="even"><span class="ln"> 33</span><span class="code">                                             SelectedPrinterIndex = i };</span></pre>
<pre class="odd"><span class="ln"> 34</span><span class="code">        }</span></pre>
<pre class="even"><span class="ln"> 35</span><span class="code">    }</span></pre>
<pre class="odd"><span class="ln"> 36</span><span class="code"></span></pre>
<pre class="even"><span class="ln"> 37</span><span class="code">    /// &lt;summary&gt;</span></pre>
<pre class="odd"><span class="ln"> 38</span><span class="code">    /// A simple class used to list the installed printers</span></pre>
<pre class="even"><span class="ln"> 39</span><span class="code">    /// and the index in that list of the default printer.</span></pre>
<pre class="odd"><span class="ln"> 40</span><span class="code">    /// &lt;/summary&gt;</span></pre>
<pre class="even"><span class="ln"> 41</span><span class="code">    class InstalledPrinters</span></pre>
<pre class="odd"><span class="ln"> 42</span><span class="code">    {</span></pre>
<pre class="even"><span class="ln"> 43</span><span class="code">        public string[] PrinterNames { get; set; }</span></pre>
<pre class="odd"><span class="ln"> 44</span><span class="code">        public int SelectedPrinterIndex { get; set; }</span></pre>
<pre class="even"><span class="ln"> 45</span><span class="code">    }</span></pre>
<pre class="odd"><span class="ln"> 46</span><span class="code">}</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1230308437SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1230308437SourceCode' );">Show copy-friendly code</a></div>
<p>The code to use the GetInstalledPrinterList() is very easy; it is shown below in Figure 2. Note that the Item’s AddRange() method is used to assign the array of printer names to the ComboBox (which also works with ListBox). While the code in Figure 2 is specific to Windows, a simple variation of it works just fine with browser-based applications. </p>
<div class="sourceCodeHeading">Figure 2. Using the GetInstalledPrinterList() method</div>
<div id="Id1248109843SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">rp.InstalledPrinters pl = rp.PrinterCommon.GetInstalledPrinterList();</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code"></span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">comboBox1.Items.AddRange( pl.PrinterNames );</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">comboBox1.SelectedIndex = pl.SelectedPrinterIndex;</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1248109843SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1248109843SourceCode' );">Show copy-friendly code</a></div>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/172/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
