<?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>esmithy.net &#187; Programming</title>
	<atom:link href="http://esmithy.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://esmithy.net</link>
	<description>Stuff Hammered Out by Eric Smith</description>
	<lastBuildDate>Tue, 17 Apr 2012 03:06:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Updating an Amazon Linux AMI to Python 2.7</title>
		<link>http://esmithy.net/2012/01/12/updating-an-amazon-linux-ami-to-python-2-7/</link>
		<comments>http://esmithy.net/2012/01/12/updating-an-amazon-linux-ami-to-python-2-7/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 17:22:43 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=509</guid>
		<description><![CDATA[I&#8217;ve been doing some work with Linux lately, a new thing for me, and feel a bit like I&#8217;ve been thrown into the deep end and told to swim. Today I updated Python to version 2.7 on an EC2 instance. Amazon Linux (Basic 64-bit Amazon Linux AMI 2011.09 AMI Id: ami-1b814f72) currently comes with Python [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing some work with Linux lately, a new thing for me, and feel a bit like I&#8217;ve been thrown into the deep end and told to swim. Today I updated Python to version 2.7 on an EC2 instance.</p>
<p><span id="more-509"></span></p>
<p>Amazon Linux (Basic 64-bit Amazon Linux AMI 2011.09 AMI Id: ami-1b814f72) currently comes with Python 2.6.7 on it. At first, I thought I&#8217;d just be able to update to 2.7 with yum, but as far as I can tell, there isn&#8217;t a package for that already. </p>
<p>Joshua Holmes <a href="http://joshualholmes.wordpress.com/category/amazon-ec2/">wrote</a> about updating Python, which was a great help. My experienced varied a little, so I thought I&#8217;d share in case it is useful to someone else. For example, I didn&#8217;t want sqllite, but I did want ssl support. Also, there were some cases where I needed to use <code>sudo</code> when logged in as ec2-user.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> yum <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">gcc</span> gcc-c++
<span style="color: #c20cb9; font-weight: bold;">sudo</span> yum <span style="color: #c20cb9; font-weight: bold;">install</span> openssl-devel.x86_64
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> ~
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>python.org<span style="color: #000000; font-weight: bold;">/</span>ftp<span style="color: #000000; font-weight: bold;">/</span>python<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>Python-<span style="color: #000000;">2.7</span>.tgz
<span style="color: #c20cb9; font-weight: bold;">tar</span> xfz Python-<span style="color: #000000;">2.7</span>.tgz
<span style="color: #7a0874; font-weight: bold;">cd</span> Python-<span style="color: #000000;">2.7</span>
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>python2.7 <span style="color: #660033;">--with-threads</span> <span style="color: #660033;">--with-ssl</span> <span style="color: #660033;">--enable-shared</span>
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> ~ 
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">''</span><span style="color: #ff0000;">'
 alias python='</span><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>python2.7<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python<span style="color: #ff0000;">'
 PATH=$PATH:/opt/python2.7/bin
 '</span><span style="color: #ff0000;">''</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> .bash_profile
<span style="color: #7a0874; font-weight: bold;">source</span> .bash_profile
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">''</span><span style="color: #ff0000;">'
 /opt/python2.7/lib
 '</span><span style="color: #ff0000;">''</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> opt-python2.7.conf
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> opt-python2.7.conf  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ld.so.conf.d<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> ldconfig
&nbsp;
python <span style="color: #660033;">-V</span>
Python <span style="color: #000000;">2.7</span></pre></td></tr></table></div>

<h4>Notes</h4>
<p>Line 26 is output from Python &#8212; not something you input (if that wasn&#8217;t obvious).</p>
<p>The <code>configure</code> script (line 8 ) specifies which optional libraries will be included in Python. I&#8217;m not sure what the typical list includes (for example, which are included in the official Python install for Windows?), but when you run <code>make</code> (line 9), it will report all the missing libraries for Python modules (&#8220;Python build finished, but the necessary bits to build these modules were not found&#8221;). If you use a module with a missing library in a Python program, you&#8217;ll get a runtime error.</p>
<p>The <code>echo</code> lines (13-16) are editing your user profile file for bash so that when you type &#8216;python&#8217; you&#8217;ll get the 2.7 version instead of the default 2.6.7 version. I had a situation where I needed to run python under <code>sudo</code>, but root doesn&#8217;t have the alias, so it kept running 2.6.7. After some unfruitful attempts to add the alias to /etc/profile (<code>sudo</code> doesn&#8217;t execute that), I just added a symlink and invoked &#8216;python2.7&#8242; explicitly (thanks to Chris Brinker):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>python2.7<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python2.7
<span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.7 <span style="color: #660033;">-V</span>
Python <span style="color: #000000;">2.7</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2012/01/12/updating-an-amazon-linux-ami-to-python-2-7/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Batch File Libraries</title>
		<link>http://esmithy.net/2011/07/12/batch-file-libraries/</link>
		<comments>http://esmithy.net/2011/07/12/batch-file-libraries/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 02:19:29 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=466</guid>
		<description><![CDATA[I&#8217;ve always considered batch files as kind of ghetto. Anything too complex devolves into a morass of gotos and labels. But I recently put together some install batch scripts that I was actually pleased with, including a clean mechanism for sharing common code. A while back, I used WiX to build an installer, which was [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always considered batch files as kind of ghetto. Anything too complex devolves into a morass of gotos and labels. But I recently put together some install batch scripts that I was actually pleased with, including a clean mechanism for sharing common code.</p>
<p><span id="more-466"></span>A while back, I used <a href="http://wix.sourceforge.net/">WiX</a> to build an installer, which was handed off to IT to deploy a web site. After a fair amount of anguish and frustration, I eventually realized that my installer was essentially a difficult to create and maintain batch file. Half the project was just custom command calls out to appcmd.exe anyway. If you&#8217;re so unfortunate as to absolutely have to create an MSI install, WiX isn&#8217;t a bad way to go, but it was a layer of massive unnecessary complexity in my case. Instead, I ended up creating a <a href="http://www.msfn.org/board/topic/39048-how-to-make-a-7-zip-switchless-installer/">7zip self-extractor</a> that ran an actual batch file when launched.</p>
<p>One challenge is that I actually had a few installers, and I wanted to share code between them, which I was able to do after discovering the <a href="http://technet.microsoft.com/en-us/library/cc772743(WS.10).aspx">CALL</a> command. Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #00b100; font-weight: bold;">call</span> lib.cmd CopyFiles &quot;C:\ServiceInstallDir&quot;</pre></div></div>

<p>This is essentially a function call, where &#8220;CopyFiles&#8221; is the name of the function, and &#8220;lib.cmd&#8221; is the file where it is implemented. Arguments follow the name of the function, the destination directory being the single argument in this case.</p>
<p>The lib.cmd file looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> off
<span style="color: #808080; font-style: italic;">REM Within the &quot;function&quot; the args will be shifted, so 3 here becomes 2, 2 becomes 1, etc.</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">%<span style="color: #448888;">1</span></span> <span style="color: #33cc33;">%</span><span style="color: #448888;">2</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">3</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">4</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">5</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">6</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">7</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">8</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">9</span>
<span style="color: #00b100; font-weight: bold;">goto</span> :<span style="color: #b100b1; font-weight: bold;">EOF</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">Heading</span>
<span style="color: #808080; font-style: italic;">REM %1 - Text for heading</span>
<span style="color: #b1b100; font-weight: bold;">echo</span> ------------------------------------------------------
<span style="color: #b1b100; font-weight: bold;">echo</span> <span style="color: #33cc33;">^|</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">~1</span>
<span style="color: #b1b100; font-weight: bold;">echo</span> ------------------------------------------------------
<span style="color: #00b100; font-weight: bold;">goto</span> :<span style="color: #b100b1; font-weight: bold;">EOF</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">CopyFiles</span>
<span style="color: #808080; font-style: italic;">REM %1 - Install directory</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">Heading</span> &quot;Copying files...&quot;
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">exist</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">~1</span>&quot; <span style="color: #b1b100; font-weight: bold;">rd</span> /s /q &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">~1</span>&quot;
<span style="color: #b1b100; font-weight: bold;">md</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">~1</span>&quot;
xcopy *.* &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">~1</span>&quot; /s 
<span style="color: #00b100; font-weight: bold;">goto</span> :<span style="color: #b100b1; font-weight: bold;">EOF</span></pre></div></div>

<p>Each label in this file acts as a function call. The CALL at the beginning passes control off to the label, including the additional arguments. The function ends by jumping to the special :EOF label (which exits the batch context). Note also that functions can call each other, as where the &#8220;CopyFiles&#8221; function calls the &#8220;Heading&#8221; function.</p>
<p>It&#8217;s a great way to encapsulate little bits of functionality in a common place.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2011/07/12/batch-file-libraries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Suppressing CA1062 with a Helper Function</title>
		<link>http://esmithy.net/2011/03/15/suppressing-ca1062/</link>
		<comments>http://esmithy.net/2011/03/15/suppressing-ca1062/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 04:32:36 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Visual Studio Tips]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=439</guid>
		<description><![CDATA[I think Visual Studio&#8217;s static analysis warning for checking parameters for null before they are used is generally a good idea, but the code to check the parameters can get really tedious. Before addressing the tedium, though, why does it even matter whether you check for null arguments and throw a ArgumentNullException? You&#8217;ll just get [...]]]></description>
			<content:encoded><![CDATA[<p>I think Visual Studio&#8217;s static analysis warning for checking parameters for null before they are used is generally a good idea, but the code to check the parameters can get really tedious.</p>
<p><span id="more-439"></span></p>
<p>Before addressing the tedium, though, why does it even matter whether you check for null arguments and throw a ArgumentNullException? You&#8217;ll just get a NullReferenceException if you don&#8217;t anyway. Is the ArgumentNullException somehow better?</p>
<p>The ArgumentNullException has a couple of benefits: It clearly indicates that the caller is to blame and by including the parameter name quickly narrows down the problem instead of requiring you to figure out which reference within the body of the method is null.</p>
<p>Although an ArgumentNullException is preferable, the null checking can overwhelm a short method. Here&#8217;s a contrived example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> PrintThings<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> things<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>things <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;things&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var thing <span style="color: #0600FF; font-weight: bold;">in</span> things<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>thing<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Half the method is dedicated to checking the parameter for null. Now imagine if a relatively short method had three parameters to check. It would be all null checks and hardly any code.</p>
<p>In the spirit of conciseness, I&#8217;ve frequently had some kind of helper function that does the null check:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> PrintThings<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> things<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Arg<span style="color: #008000;">.</span><span style="color: #0000FF;">NotNull</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;things&quot;</span>, things<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var thing <span style="color: #0600FF; font-weight: bold;">in</span> things<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>thing<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The trouble is, Visual Studio&#8217;s static analysis doesn&#8217;t recognize this as a parameter validation of &#8220;things&#8221;, so you&#8217;ll get the CA1062 warning. </p>
<p>The work-around is to create a ValidatedNotNullAttribute and include it in the helper method, which signals to static analysis that, trust me, I really am checking the parameter. Here&#8217;s a complete example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> CA1062Test
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            PrintThings<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;one&quot;</span>, <span style="color: #666666;">&quot;two&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> PrintThings<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> things<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Arg<span style="color: #008000;">.</span><span style="color: #0000FF;">NotNull</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;things&quot;</span>, things<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var thing <span style="color: #0600FF; font-weight: bold;">in</span> things<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>thing<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Arg
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> NotNull<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> name, <span style="color: #008000;">&#91;</span>ValidatedNotNull<span style="color: #008000;">&#93;</span> <span style="color: #6666cc; font-weight: bold;">object</span> value<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> ValidatedNotNullAttribute <span style="color: #008000;">:</span> Attribute
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Notice the trivial ValidatedNotNullAttribute implementation &#8212; it doesn&#8217;t matter what it does so long as it is named &#8220;ValidatedNotNullAttribute&#8221;. The attribute is applied to the parameter being checked in the NotNull function, so it only has to be specified once in the helper declaration, and <strong>not</strong> on each use of the helper.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2011/03/15/suppressing-ca1062/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cipher Suite for webOS</title>
		<link>http://esmithy.net/2010/09/30/cipher-suite-for-webos/</link>
		<comments>http://esmithy.net/2010/09/30/cipher-suite-for-webos/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 04:26:51 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=398</guid>
		<description><![CDATA[My son turns 14 this month, but we still have bedtime stories. Currently, the bedtime story is Simon Singh&#8217;s The Code Book. While reading the description of the Vigenére cipher, I started to think about how I&#8217;d code up an implementation, and that led to my first webOS application. Cipher Suite is a small collection [...]]]></description>
			<content:encoded><![CDATA[<p>My son turns 14 this month, but we still have bedtime stories. Currently, the bedtime story is Simon Singh&#8217;s  <em><a href="http://www.amazon.com/gp/product/0385495323?ie=UTF8&#038;tag=esmithy.net.sparks-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0385495323">The Code Book</a><img src="http://www.assoc-amazon.com/e/ir?t=esmithy.net.sparks-20&#038;l=as2&#038;o=1&#038;a=0385495323" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></em>. While reading the description of the Vigenére cipher, I started to think about how I&#8217;d code up an implementation, and that led to my first webOS application. Cipher Suite is a small collection of classic ciphers, and is currently in the Palm <a href="http://www.precentral.net/app-gallery/beta/cipher-suite">beta feed</a>. Hopefully a few people will give it a try and let me know if it sets their phone on fire or drowns some kittens or anything. Having gotten it that far, I thought I&#8217;d share a little bit about how the app was developed.<br />
<span id="more-398"></span></p>
<h3>Development Environment</h3>
<p>I don&#8217;t love Eclipse, so I was excited when Palm announced <a href="http://ares.palm.com/Ares/about.html">Ares</a>. Ares combines most of the tools you need for webOS development in a simple, browser-based IDE. Built-in JSLint support is awesome. Viewing logs while an app is running is simple. The editor and debugger are surprisingly capable. Source control integration is a great feature.</p>
<p>The visual designer, though possibly the most visible feature of Ares, wasn&#8217;t that helpful for me. There&#8217;s just not enough documentation to figure out how to accomplish what I want. The developer site has just barely enough documentation to build an app in the &#8220;traditional&#8221; Mojo way, let alone translating from Mojo to the Ares designer. The <a href="http://www.youtube.com/watch?v=cR5_iW71soA">Ares presentation from Developer Days</a> was hugely disappointing. So I just ended up creating a regular Mojo application instead, which worked just fine. It would be nice if you could choose up front which style of app to develop, and have the &#8220;New Scene&#8221; menu item either create an HTML scene or an Ares &#8220;chrome&#8221; scene as appropriate.</p>
<h3>Source Control</h3>
<p>Even though Ares keeps your code in the cloud, it&#8217;s still a good idea to use source control for change tracking. I looked at a bunch of online Subversion hosts, and ended up picking <a href="http://unfuddle.com/">Unfuddle</a>.  It&#8217;s free, which is a big factor for a hobby project, and has worked reasonably well. It took me a little while to realize that adding a new project file in Ares does not add it to source control automatically &#8212; you have to do that as a separate step.</p>
<h3>Unit Testing</h3>
<p>Another benefit of my Subversion repository was that I could work on my project outside of Ares and still keep everything organized. The main thing I found missing in Ares was a built-in way to do unit tests, so I took care of those on right on my PC. I used the <a href="http://pivotal.github.com/jasmine">Jasime JavaScript unit test framework</a>, running the tests from a local &#8220;runner&#8221; HTML file within Firefox so that I could step through any failing tests using the Firebug debugger.</p>
<h3>Summary</h3>
<p>Learning to develop for webOS definitely took some effort, but now as I&#8217;m going through the same learning process for iOS, I realize that writing code in JavaScript and building views in HTML provides for a gentler learning curve than Objective-C and Cocoa. I started my professional development career as a C programmer, but the merger with Small Talk makes Objective-C an odd beast. </p>
<p>Cipher Suite is kind of a niche application, but hopefully it will be fun or useful to a few people. It took a lot of spare-time hours over several months to put it together, but I wanted to do my small part to support the great webOS community.  </p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2010/09/30/cipher-suite-for-webos/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Transparent WinForms Label</title>
		<link>http://esmithy.net/2009/11/28/transparent-winforms-label/</link>
		<comments>http://esmithy.net/2009/11/28/transparent-winforms-label/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 18:40:41 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=315</guid>
		<description><![CDATA[Easy control transparency isn&#8217;t exactly a hallmark of Windows Forms. If you have a form with a background image or a gradient, the stock controls paint with a solid background to give a criminally egregious aesthetic. If you search the web for a solution, you might come up with a couple of proposed solutions: WinForms: [...]]]></description>
			<content:encoded><![CDATA[<p>Easy control transparency isn&#8217;t exactly a hallmark of Windows Forms. If you have a form with a background image or a gradient, the stock controls paint with a solid background to give a criminally egregious aesthetic.</p>
<p><span id="more-315"></span>If you search the web for a solution, you might come up with a couple of proposed solutions:</p>
<ul>
<li><a href="http://support.microsoft.com/kb/943454">WinForms: How to create a control transparent to other controls</a> &#8211; The official word from Microsoft, the solution is presented in several pages of poorly formatted VB code that makes you immediately want to look for something simpler, such as&#8230;</li>
<li><a href="http://www.doogal.co.uk/transparent.php">C# Transparent Label</a> (also <a href="http://www.west-wind.com/WebLog/posts/247977.aspx">referenced</a> from Rick Strahl&#8217;s blog) &#8211; This solution is much simpler; it&#8217;s just a Control subclass that doesn&#8217;t paint a background and draws the label text in OnPaint.</li>
</ul>
<p>After working with the second solution for a while, I discovered that it doesn&#8217;t handle updating the text very well. Just changing the Text property itself doesn&#8217;t do it, and if you invalidate the control, you get the new text painted over the old text. If you&#8217;re the kind of person who fusses about minutiae like <em>legibility</em> and such, this isn&#8217;t ideal.</p>
<p>So here&#8217;s a solution that has worked reasonably well for me, and that scores high marks in the simplicity category:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// </span>
<span style="color: #008080; font-style: italic;">/// A label that is transparent.</span>
<span style="color: #008080; font-style: italic;">/// </span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TransparentLabel <span style="color: #008000;">:</span> Label
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/// </span>
  <span style="color: #008080; font-style: italic;">/// Paints the background with the parent's background image.</span>
  <span style="color: #008080; font-style: italic;">/// </span>
  <span style="color: #008080; font-style: italic;">///</span>
e
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPaintBackground<span style="color: #008000;">&#40;</span>PaintEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Rectangle rect <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span>Location, Size<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawImage</span><span style="color: #008000;">&#40;</span>Parent<span style="color: #008000;">.</span><span style="color: #0000FF;">BackgroundImage</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, rect, GraphicsUnit<span style="color: #008000;">.</span><span style="color: #0000FF;">Pixel</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>It basically scoops up the correct part of the parent&#8217;s background image and uses it as the background for the control. Yes, it still isn&#8217;t transparency, just a much better job of &#8220;camouflage&#8221; than the default behavior. If you&#8217;re painting the parent&#8217;s background, with a gradient brush for example, you can paint that into a bitmap instead and set the parent&#8217;s BackgroundImage to the bitmap for this solution to still work.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/11/28/transparent-winforms-label/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rounded Rectangluar Regions</title>
		<link>http://esmithy.net/2009/11/19/rounded-rectangluar-regions/</link>
		<comments>http://esmithy.net/2009/11/19/rounded-rectangluar-regions/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 04:27:27 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=302</guid>
		<description><![CDATA[There are lots of examples that demonstrate how to draw a rectangle with rounded corners using GDI+ in .NET. Converting such a rectangle to a Region so that it can be filled or be used for the geometry of a window can have less than perfect results, though. For example, here is one of the [...]]]></description>
			<content:encoded><![CDATA[<p>There are lots of examples that demonstrate how to draw a rectangle with rounded corners using GDI+ in .NET. Converting such a rectangle to a Region so that it can be filled or be used for the geometry of a window can have less than perfect results, though.<br />
<span id="more-302"></span><br />
For example, here is one of the more concise methods for getting a rounded Rectangle:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> GraphicsPath GetRoundedRectangle<span style="color: #008000;">&#40;</span>Rectangle rect, <span style="color: #6666cc; font-weight: bold;">int</span> rad<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">int</span> d <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> rad<span style="color: #008000;">;</span>
	<span style="color: #000000;">System.<span style="color: #0000FF;">Drawing</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Drawing2D</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">GraphicsPath</span> gp <span style="color: #008000;">=</span>
			<span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Drawing</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Drawing2D</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">GraphicsPath</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	gp<span style="color: #008000;">.</span><span style="color: #0000FF;">AddArc</span><span style="color: #008000;">&#40;</span>rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, d, d, <span style="color: #FF0000;">180</span>, <span style="color: #FF0000;">90</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	gp<span style="color: #008000;">.</span><span style="color: #0000FF;">AddArc</span><span style="color: #008000;">&#40;</span>rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">-</span> d, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, d, d, <span style="color: #FF0000;">270</span>, <span style="color: #FF0000;">90</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	gp<span style="color: #008000;">.</span><span style="color: #0000FF;">AddArc</span><span style="color: #008000;">&#40;</span>rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">-</span> d, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span> <span style="color: #008000;">-</span> d, d, d, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">90</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	gp<span style="color: #008000;">.</span><span style="color: #0000FF;">AddArc</span><span style="color: #008000;">&#40;</span>rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span> <span style="color: #008000;">-</span> d, d, d, <span style="color: #FF0000;">90</span>, <span style="color: #FF0000;">90</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	gp<span style="color: #008000;">.</span><span style="color: #0000FF;">AddLine</span><span style="color: #008000;">&#40;</span>rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span> <span style="color: #008000;">-</span> d, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> d <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> gp<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>It&#8217;s trivial to convert that GraphicsPath to a Region &#8212; just call the appropriate Region constructor. You can then fill the region:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var path <span style="color: #008000;">=</span> GetRoundedRectangle<span style="color: #008000;">&#40;</span>rect, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FillRegion</span><span style="color: #008000;">&#40;</span>Brushes<span style="color: #008000;">.</span><span style="color: #0000FF;">Orange</span>, <span style="color: #008000;">new</span> Region<span style="color: #008000;">&#40;</span>path<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If your corner radius is sufficiently large, you might not notice anything wrong with the result. With a value of 3, it is pretty obvious:</p>
<p style="text-align: center;"><img class="size-full wp-image-303 aligncenter" title="Not-so-rounded rectangle" src="http://esmithy.net/content/rect-bad.png" alt="Not-so-rounded rectangle" width="321" height="191" /></p>
<p>The top left corner looks perfect:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-308" title="Top-left" src="http://esmithy.net/content/rect-topleft.png" alt="Top-left" width="105" height="105" /></p>
<p>The bottom right corner is decidedly un-round:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-309" title="Bottom-right" src="http://esmithy.net/content/rect-bottomright1.png" alt="Bottom-right" width="120" height="126" /></p>
<p>I started to wonder if all those round rectangle functions were wrong, but drawing the GraphicsPath itself shows that the code is correct:</p>
<p style="text-align: center;"><img class="size-full wp-image-307 aligncenter" title="Graphics Path" src="http://esmithy.net/content/rect-path.png" alt="Graphics Path" width="329" height="200" /></p>
<p>The problem is that when converting the GraphicsPath to a Region, it uses the <em>inside </em>of the GraphicsPath, so you lose the outside pixels on the sides, which you can see when drawing the path and then filling the region:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-305" title="Missing pixels in the region" src="http://esmithy.net/content/rect-detail.png" alt="rect-detail" width="310" height="181" /></p>
<p>It turns out to be a whole lot easier and effective to do a little p/invoke here:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;gdi32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> IntPtr CreateRoundRectRgn<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> x1, <span style="color: #6666cc; font-weight: bold;">int</span> y1, <span style="color: #6666cc; font-weight: bold;">int</span> x2, <span style="color: #6666cc; font-weight: bold;">int</span> y2, <span style="color: #6666cc; font-weight: bold;">int</span> cx, <span style="color: #6666cc; font-weight: bold;">int</span> cy<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// ...</span>
var region <span style="color: #008000;">=</span> Region<span style="color: #008000;">.</span><span style="color: #0000FF;">FromHrgn</span><span style="color: #008000;">&#40;</span>CreateRoundRectRgn<span style="color: #008000;">&#40;</span>rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> rect<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FillRegion</span><span style="color: #008000;">&#40;</span>Brushes<span style="color: #008000;">.</span><span style="color: #0000FF;">Orange</span>, region<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Here&#8217;s the output from that code:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-306" title="Good Region" src="http://esmithy.net/content/rect-good.png" alt="Good Region" width="310" height="182" /></p>
<p>It looks like a little larger corner radius is needed to get the same rounding, but otherwise it is perfect.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/11/19/rounded-rectangluar-regions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight Localization</title>
		<link>http://esmithy.net/2009/07/08/silverlight-localization/</link>
		<comments>http://esmithy.net/2009/07/08/silverlight-localization/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 02:00:57 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=252</guid>
		<description><![CDATA[The official Silverlight documentation about localization gives lots of information about working with resource files, but it is kind of scant on how to actually get localized strings to show up in the UI in a reasonable way. For example, an unreasonable way would be to set the text/content properties of all the controls in [...]]]></description>
			<content:encoded><![CDATA[<p>The official <a href="http://msdn.microsoft.com/en-us/library/cc838238(VS.95).aspx">Silverlight documentation about localization</a> gives lots of information about working with resource files, but it is kind of scant on how to actually get localized strings to show up in the UI in a reasonable way. For example, an unreasonable way would be to set the text/content properties of all the controls in a page&#8217;s code-behind file. A reasonable way is to use data binding.</p>
<p><span id="more-252"></span></p>
<p>I found a good example of this in Chapter 5 of <em>Professional Silverlight 2 for ASP.NET Developers</em> by Jonathan Swift, Salvador Alvarez Patuel, Chris Barker and Dan Wahlin. As I describe it, and continue the discussion through this post, you&#8217;ll notice that the term &#8220;resource&#8221; is overloaded in Silverlight: There are resources in the normal .NET sense, backed by .resx files, and there are resources that are simply a dictionary of objects instantiated in XAML within a Resources element. Hopefully the usage will not be overly confusing.</p>
<p>In the localization example from the book, they created a resource file called LocalizedStrings.resx. Then they instantiated the automatically generated strongly typed class by including it in the Resources associated with a user control. Finally, they could then bind the Text property of a couple of TextBlock controls to a particular string from the resources:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;SilverlightLocalizationExample.Page&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:Localized</span>=<span style="color: #ff0000;">&quot;clr-namespace:LocalizationExample.Resources&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Localized:LocalizedStrings</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LocalizedStrings&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding TextBlock1,</span>
<span style="color: #009900;">            Source={StaticResource LocalizedStrings}}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding TextBlock2,</span>
<span style="color: #009900;">            Source={StaticResource LocalizedStrings}}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You&#8217;ll want to be sure to use the PublicResXFileCodeGenerator custom tool to generate the code from the .resx file, otherwise the binding doesn&#8217;t work.</p>
<p>So what if you want to dynamically change languages, such as with a language drop-down?</p>
<p>The solution I came up with incorporates string resources into the view model. I was hesitant to do this at first because it felt funny having resources in the view model. Aren&#8217;t the strings purely a view component? Ultimately I decided that supplying the correct strings to the view based on language choice is a reasonable thing for the view model to do, and the resulting implementation and usage felt pretty good.</p>
<p>The base class for all view models simply has a Resources member:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">object</span> Resources
<span style="color: #008000;">&#123;</span>
  get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Each subclass then returns the appropriate object containing the resources:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyViewModel <span style="color: #008000;">:</span> ViewModel
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> MyViewModelResources _resources<span style="color: #008000;">;</span>
&nbsp;
  MyViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _resources <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyViewModelResorces<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> Resources
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _resources<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The class MyViewModelResources is the class generated from MyViewModelResources.resx. You might be saying, &#8220;Wait a minute. That class is basically <em>static</em>. What are you doing creating an instance of it?&#8221; Well, I don&#8217;t think there&#8217;s a way to bind to statics, so you need to have an actual instance around. Of course, the view will actually end up calling the static property (such as SubmitButtonText) when you include one in the binding path. Also, this isn&#8217;t any different from instantiating the resource class in XAML as the first example does.</p>
<p>Modifying the first example to use the view model (and setting the view model as the DataContext) makes the XAML look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;SilverlightLocalizationExample.Page&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:Localized</span>=<span style="color: #ff0000;">&quot;clr-namespace:LocalizationExample&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Localized:MyViewModel</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ViewModel&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">DataContext</span>=<span style="color: #ff0000;">&quot;{StaticResource ViewModel}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Resources.TextBlock1}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Resources.TextBlock2}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So what about the dynamic switching of languages at run time?</p>
<p>Since the &#8220;Resources&#8221; property is in the binding path, changing its value will update the bindings and the UI updates with the new strings. You just need to notify the view that the Resources property has changed. To make this easy, I created a helper method in the ViewModel base class, RaiseResourcesChanged, that effectively says, &#8220;Attention all view models, please update your views&#8217; resources.&#8221; The message gets propagated to each view model instance by having a static ResourcesChanged event in the ViewModel base class that each ViewModel instance subscribes to at construction. When the event fires, the base class in turn raises the INotifyPropertyChanged.PropertyChanged event for the &#8220;Resources&#8221; property.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler ResourcesChanged<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    ResourcesChanged <span style="color: #008000;">+=</span> ViewModel_ResourcesChanged<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> RaiseResourcesChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var handler <span style="color: #008000;">=</span> ResourcesChanged<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>handler <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ResourcesChanged<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, EventArgs<span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #6666cc; font-weight: bold;">void</span> ViewModel_ResourcesChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    RaisePropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Resources&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Any single view model can then change the CurrentUICulture, call the helper method, and the UI updates automatically for all views:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentThread</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUICulture</span> <span style="color: #008000;">=</span> newCulture<span style="color: #008000;">;</span>
RaiseResourcesChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/07/08/silverlight-localization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.NET Cold Startup Performance: More</title>
		<link>http://esmithy.net/2008/06/03/net-cold-startup-performance-more/</link>
		<comments>http://esmithy.net/2008/06/03/net-cold-startup-performance-more/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 23:36:13 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=132</guid>
		<description><![CDATA[A few months ago I wrote about an approach to improving .NET cold startup performance. Here&#8217;s a little more information that I&#8217;ve learned since then. Another useful tool for optimizing cold startup is Microsoft&#8217;s free CLR Profiler 2.0. While this tool has the aesthetics of a wadded-up paper sack, it does have the ability to graph each assembly that gets [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago <a href="2008/03/06/net-cold-startup-performance-an-example/">I wrote about</a> an approach to improving .NET cold startup performance. Here&#8217;s a little more information that I&#8217;ve learned since then.</p>
<p><span id="more-132"></span>Another useful tool for optimizing cold startup is Microsoft&#8217;s free <a href="http://www.microsoft.com/downloads/details.aspx?familyid=A362781C-3870-43BE-8926-862B40AA0CD0&#038;displaylang=en">CLR Profiler 2.0</a>. While this tool has the aesthetics of a wadded-up paper sack, it does have the ability to graph each assembly that gets loaded, and the method that caused it to be loaded. This makes it easier to figure out <em>why</em> a particular assembly got loaded, allowing you to consider whether there might be a way to avoid loading it or postpone loading it until some less critical moment.</p>
<p>To generate the assembly graph, click the <strong>Start Application</strong> button in the profiler. It lets you browse to the executable, which it will then launch. When you close the application, the profiler displays a summary screen with a bunch of information about the managed heap and garbage collection. You can close that window. Instead, go to the main menu and pick <strong>View</strong> &gt; <strong>Assembly Graph</strong>. You&#8217;ll see something like this:</p>
<p style="text-align: center;"> <a href="http://esmithy.net/content/assembly-graph.png"><img class="alignnone size-medium wp-image-133" title="Assembly Graph Window" src="http://esmithy.net/content/assembly-graph-300x181.png" alt="" width="300" height="181" /></a></p>
<p style="text-align: center;">Assembly Graph Window (click for larger image)</p>
<p style="text-align: left;">At the far left edge of the graph, you can see the total number of assemblies (30) that were loaded during the application run. The edges from there represent method calls that caused other assemblies to load. Underneath the method name is the total number assemblies loaded through the full call stack. As you move to the far right side of the graph, you eventually see each individual assembly that is loaded. If you click on a node in the graph it sort of highlights (is <em>plaid</em> technically a highlight color?) the edges in and out of the node. That can be really handy when the graph lines criss-cross over each other.</p>
<p style="text-align: left;">In my case, where I&#8217;m writing a Word add-in, I have some initialization that must happen on the main thread at startup. Other initialization is put on a low priority background thread, which allows Word to start up quickly while continuing to load up the add-in functionality for when it will be used. To get a better picture of startup, I commented out the background thread. Then I looked for call paths that loaded lots of assemblies to figure out if any of those could be deferred to the background thread.</p>
<p style="text-align: left;">I found that making an XML-RPC call was pretty expensive in terms of code loading. The RPC call itself was pretty fast &#8212; just talking to another process on the same machine, but it took loading classes from five additional assemblies just to make the call, which hurts cold startup performance. At this point we&#8217;re trying to figure out if the current performance is acceptable or if we need to tweak the add-in&#8217;s behavior a little to delay the RPC call to the background thread. </p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2008/06/03/net-cold-startup-performance-more/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are .NET Properties a Mistake?</title>
		<link>http://esmithy.net/2008/04/24/are-net-properties-a-mistake/</link>
		<comments>http://esmithy.net/2008/04/24/are-net-properties-a-mistake/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 03:28:02 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://esmithy.net/2008/04/24/are-net-properties-a-mistake/</guid>
		<description><![CDATA[As I mentioned the other day, I&#8217;m reading Jeffrey Richter&#8217;s book CLR via C# right now. I was kind of surprised to read this statement by the author: &#8220;If I had been involved in the design of the .NET Framework and compilers, I would not have offered properties at all&#8230;&#8221; (p. 218) Mr. Richter&#8217;s reason [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned the other day, I&#8217;m reading Jeffrey Richter&#8217;s book <em>CLR via C#</em> right now. I was kind of surprised to read this statement by the author: &#8220;If I had been involved in the design of the .NET Framework and compilers, I would not have offered properties at all&#8230;&#8221; (p. 218)<span id="more-131"></span></p>
<p>Mr. Richter&#8217;s reason for disliking properties is that they look like fields but don&#8217;t behave like them, thus confusing developers. He gives a good list of ways in which properties are different from fields &#8212; think of all the ways that a zero argument method is different from a field and you&#8217;ll about have it &#8212; saying that those differences are bound to trip people up.</p>
<p>I disagree for a couple of reasons:</p>
<ol>
<li>Properties are really pretty easy to grasp.</li>
<li>Most programmers adhere to the rule that all fields are private (which Mr. Richter also recommended), so there aren&#8217;t really fields to be confused by. Expect everything to be a method and you&#8217;ll generally be fine.</li>
</ol>
<p>I say &#8220;generally&#8221; because there are some cases where the opposite is true: a property can behave more like a field than a method. For example, you can use the <a href="http://msdn.microsoft.com/en-us/library/aa691363(VS.71).aspx">postfix increment operator</a> on a property (that has both get and set accessors) and it behaves as it would on a field. That is, the operator gets the property value and sets the incremented value back in. [Added June 26, 2008]</p>
<p>A benefit of properties that isn&#8217;t really mentioned in the book is a more explicit (for lack of a better word) &#8220;bean-ness&#8221;. Java has the bean concept, where an object has get and set methods to create a set of properties, but the C# syntax is more elegant and apparent. With the explicit properties, you can do things like XML serialization, property editors, etc. with reasonable default behavior.</p>
<p>It&#8217;s probably true that .NET properties get abused and overused (see the <a href="http://msdn2.microsoft.com/en-us/library/ms229006.aspx">official guidelines</a>), but I still kind of like them.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2008/04/24/are-net-properties-a-mistake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Guidelines for Method Parameter and Return Types</title>
		<link>http://esmithy.net/2008/04/22/guidelines-for-method-parameter-and-return-types/</link>
		<comments>http://esmithy.net/2008/04/22/guidelines-for-method-parameter-and-return-types/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 04:25:48 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://esmithy.net/2008/04/22/guidelines-for-method-parameter-and-return-types/</guid>
		<description><![CDATA[I&#8217;ve been reading Jeffrey Richter&#8217;s CLR via C# lately, and just read a section titled &#8220;Declaring a Method&#8217;s Parameter Types&#8221;. That title doesn&#8217;t exactly promise a riveting read, but it did a nice job of articulating some guidelines for method parameter and return types, independent of any particular programming language. The guidelines are these: A [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading Jeffrey Richter&#8217;s <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FCLR-via-Second-Pro-Developer%2Fdp%2F0735621632%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1208924595%26sr%3D8-1&amp;tag=sparksfromthesmi&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325"><em>CLR via C#</em></a><img style="margin: 0px; border: medium none" src="http://www.assoc-amazon.com/e/ir?t=sparksfromthesmi&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" /> lately, and just read a section titled &#8220;Declaring a Method&#8217;s Parameter Types&#8221;. That title doesn&#8217;t exactly promise a riveting read, but it did a nice job of articulating some guidelines for method parameter and return types, independent of any particular programming language.</p>
<p><span id="more-130"></span></p>
<p>The guidelines are these:</p>
<ol>
<li>A method&#8217;s parameters should be the most general type possible.</li>
<li>A method&#8217;s return value should be the most specific type possible.</li>
</ol>
<p>As an example of the first guideline, a method should accept <strong>IEnumerable</strong> rather than <strong>IList</strong> if <strong>IEnumerable</strong> is all that is needed. The user of a method can always pass in something more specific, but being general allows the greatest flexibility. Of course, the most general parameter type would be <strong>object</strong>, but I don&#8217;t think you have to worry about this guideline suddenly causing you to write methods like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">FileStream CreateFile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> fileName, <span style="color: #6666cc; font-weight: bold;">object</span> fileMode, <span style="color: #6666cc; font-weight: bold;">object</span> fileAccess<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Clearly you wouldn&#8217;t want the method implementation down-casting all the parameters in order to do something useful with them. If the method can actually do something useful with  plain <strong>object</strong> parameters, then that is probably the right type.</p>
<p>Conversely, following the second guideline suggest that you should return a <strong>FileStream</strong> rather than a <strong>Stream</strong> (assuming that what you actually have is a <strong>FileStream</strong>). The caller can always treat it as something more general, but never something more specific. In this case, the most specific type provides the most flexibility.</p>
<p>The author did provide a caveat to the second guideline, however. If you want to allow the implementation of a method to change, you might pick a return type that is slightly more general. The example he gives is that even if a method happens to use a <strong>List</strong> internally, it may be appropriate to return an <strong>IList</strong> so that the internal implementation could change to use an array without affecting the callers.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2008/04/22/guidelines-for-method-parameter-and-return-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

