<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The Art of Code</title>
	
	<link>http://theartofcode.com</link>
	<description>Process Modeling, Simulation, Workforce Management &amp; Software Development</description>
	<pubDate>Wed, 16 Dec 2009 16:01:28 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheArtOfCode" /><feedburner:info uri="theartofcode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>HTTP Web Requests in .NET</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/NIXGjsUN19o/</link>
		<comments>http://theartofcode.com/2009/09/14/http-web-requests-in-net/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 13:10:00 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Coding Techniques]]></category>

		<category><![CDATA[http]]></category>

		<category><![CDATA[httpwebrequest]]></category>

		<category><![CDATA[httpwebresponse]]></category>

		<category><![CDATA[static methods]]></category>

		<category><![CDATA[webrequest]]></category>

		<category><![CDATA[webresponse]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/09/14/http-web-requests-in-net/</guid>
		<description><![CDATA[For whatever reason, when Microsoft developed the HttpWebRequest.GetResponse() method, if the status code of the response is 4xx or 5xx (for example a 404 not found response), it actually throws a WebException and you have to retrieve the Response object from the exception to test the status code of the response:


   1: try [...]]]></description>
			<content:encoded><![CDATA[<p>For whatever reason, when Microsoft developed the HttpWebRequest.GetResponse() method, if the status code of the response is 4xx or 5xx (for example a <em>404 not found</em> response), it actually throws a WebException and you have to retrieve the Response object from the exception to test the status code of the response:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">try</span> {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     HttpWebResponse response = (HttpWebResponse)request.GetResponse();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     ...</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> } <span style="color: #0000ff">catch</span> (WebException e) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     HttpWebResponse response = (HttpWebResponse)e.Response;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     HttpStatusCode status = response.StatusCode;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     ...</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> }</pre>
</div>
</div>
<p>In creating a custom client, I have found occasion where I would rather threat a <em>404 not found</em> response, or a <em>server error</em> response like any other response. For example, it might be common in my application to get a 404 not found response returned by the web server, and I may be simply passing the response to the user. In this case, I would always have to catch the exception.</p>
<p>Ideally, I would like to derive a class from the HttpWebRequest and override the GetResponse() method to automatically handle WebExceptions associated with 4xx and 5xx responses. It appears that this is not possible. HttpWebRequest derives from the WebRequest class and provides additional properties and methods that enable interaction with servers using the Hypertext Transfer Protocol (HTTP). Instances of the HttpWebRequest class are automatically created by the WebRequest class. For example, an instance is created when the WebRequest.Create(uri) method is called and a Uniform Resource Identifier (URI) beginning with <em>http://</em> is specified. Yet again, <a title="static methods" href="http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/">static methods are evil</a>.</p>
<p>Plan B is to create a lightweight wrapper like so:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Net;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.Runtime.Serialization;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span> <span style="color: #0000ff">namespace</span> WebRequestTest {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     <span style="color: #008000">// Class that wraps a HttpWebRequest object</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> MyHttpWebRequest {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>         <span style="color: #008000">// The underlying HttpWebRequest object</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>         <span style="color: #0000ff">private</span> HttpWebRequest _request;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>         <span style="color: #008000">// The constructor takes a HttpWebRequest object</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>         <span style="color: #0000ff">public</span> MyHttpWebRequest(HttpWebRequest request) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>             <span style="color: #0000ff">this</span>._request = request;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>         }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>         <span style="color: #008000">// This method returns a HttpWebResponse object</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span>         <span style="color: #0000ff">public</span> HttpWebResponse GetResponse() {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span>             <span style="color: #0000ff">try</span> {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  20:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  21:</span>                 <span style="color: #008000">// If the response can be retrieved from the</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  22:</span>                 <span style="color: #008000">// underlying request, return it</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  23:</span>                 <span style="color: #0000ff">return</span> (HttpWebResponse)<span style="color: #0000ff">this</span>._request.GetResponse();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  24:</span>             } <span style="color: #0000ff">catch</span> (WebException e) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  25:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  26:</span>                 <span style="color: #008000">// Get the response from the WebException object</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  27:</span>                 HttpWebResponse response =</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  28:</span>                     (HttpWebResponse)e.Response;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  29:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  30:</span>                 <span style="color: #008000">// If the exception is because of the status being</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  31:</span>                 <span style="color: #008000">// 4xx or 5xx, return the exceptions response object</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  32:</span>                 <span style="color: #0000ff">if</span> ((<span style="color: #0000ff">int</span>)response.StatusCode &gt;= 400) <span style="color: #0000ff">return</span> response;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  33:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  34:</span>                 <span style="color: #008000">// otherwise rethrow the exception</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  35:</span>                 <span style="color: #0000ff">else</span> <span style="color: #0000ff">throw</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  36:</span>             }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  37:</span>         }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  38:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  39:</span> }</pre>
</div>
</div>
<p>And now we can use the wrapper’s GetResponse() method to get an HttpWebResponse object regardless of the status code:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Net;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span> <span style="color: #0000ff">namespace</span> WebRequestTest {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">class</span> Program {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>         <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>             <span style="color: #008000">// Request a valid web resource</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>             HttpWebRequest request =</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>                 (HttpWebRequest)HttpWebRequest.Create(<span style="color: #006080">"http://www.yahoo.com"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>             <span style="color: #008000">// Create instance of wrapper for the</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>             <span style="color: #008000">// purpose of getting the response</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span>             MyHttpWebRequest myrequest =</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>                 <span style="color: #0000ff">new</span> MyHttpWebRequest(request);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span>             <span style="color: #008000">// Get the response object and display the </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  20:</span>             <span style="color: #008000">// status description</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  21:</span>             HttpWebResponse response = myrequest.GetResponse();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  22:</span>             <span style="color: #008000">// This will display "OK"</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  23:</span>             Console.WriteLine(response.StatusDescription);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  24:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  25:</span>             <span style="color: #008000">// Request a non-existent web resource</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  26:</span>             request =</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  27:</span>                 (HttpWebRequest)HttpWebRequest.Create(<span style="color: #006080">"http://www.yahoo.com/thgy.html"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  28:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  29:</span>             <span style="color: #008000">// Create instance of wrapper for the</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  30:</span>             <span style="color: #008000">// purpose of getting the response</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  31:</span>             myrequest = <span style="color: #0000ff">new</span> MyHttpWebRequest(request);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  32:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  33:</span>             <span style="color: #008000">// Get the response object and display the </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  34:</span>             <span style="color: #008000">// status description </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  35:</span>             response = myrequest.GetResponse();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  36:</span>             <span style="color: #008000">// This will display "not found"</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  37:</span>             Console.WriteLine(response.StatusDescription);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  38:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  39:</span>             Console.ReadLine();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  40:</span>         }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  41:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  42:</span> }</pre>
</div>
</div>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/NIXGjsUN19o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/09/14/http-web-requests-in-net/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/09/14/http-web-requests-in-net/</feedburner:origLink></item>
		<item>
		<title>Testing Regex</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/kglS1H5QINM/</link>
		<comments>http://theartofcode.com/2009/09/13/testing-regex/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 18:11:00 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Coding Techniques]]></category>

		<category><![CDATA[addin]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[parser]]></category>

		<category><![CDATA[regex]]></category>

		<category><![CDATA[regular expressions]]></category>

		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/09/13/testing-regex/</guid>
		<description><![CDATA[Going through an entire compile / debug cycle only to find that a Regex is not behaving as intended is not a good use of a developer’s time (well, it’s not a good use of my time anyway). There is a better way.
The Problems with Regex
Regex, or regular expressions, represents a formal language that provides [...]]]></description>
			<content:encoded><![CDATA[<p>Going through an entire compile / debug cycle only to find that a Regex is not behaving as intended is not a good use of a developer’s time (well, it’s not a good use of my time anyway). There is a better way.</p>
<h3>The Problems with Regex</h3>
<p><a title="regex" href="http://en.wikipedia.org/wiki/Regular_expression">Regex</a>, or regular expressions, represents a formal language that provides an extremely concise way to parse strings of text. To be honest, there are no problems with Regex itself. The problem is you need a Ph.D. to understand it. Ok, maybe it’s not quite that difficult, but the syntax can be annoying to say the least. It may appeal to some, but I imagine these as diehard coders who would prefer to write novels in assembler. I would, however, hazard to guess that the conciseness of a Regex is <em>very</em> appealing to almost anyone whose alternative is to write lines and lines of string parsing code.</p>
<h3>Test Regex Outside of Debug Cycle</h3>
<p>Since Regex can be interpreted by any regular expression processor,&#160; regular expressions in your code can be tested outside of your code, and <em>before</em> you ever compile. It’s ideal, in my opinion, to test and validate specific regular expressions as part of the development process, as opposed to relying on the (unit) testing process. Don’t get me wrong, my regular expressions would still be tested indirectly as past of my unit tests, but I don’t want to endure multiple compile cycles as I debug the regular expressions themselves. Here are a couple of alternatives:</p>
<ol>
<li>Testing Online: JavaScript 1.2 and later has built-in support for regular expressions. There are a number of excellent expression processors that take advantage of this support. For example, this <a title="regular expression tester" href="http://www.regular-expressions.info/javascriptexample.html">Regular Expression Tester</a>.</li>
<li>Testing within the IDE: If online is not your sort of thing, maybe something built into your IDE would work better. For Visual Studio, you can use the <a title="Regular Expression Explorer" href="http://visualstudiogallery.msdn.microsoft.com/en-us/f63786bf-a137-4433-9d2c-6eea56d39927">Regular Expression Explorer</a> addin. </li>
</ol>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/kglS1H5QINM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/09/13/testing-regex/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/09/13/testing-regex/</feedburner:origLink></item>
		<item>
		<title>Ideas That Spread Win</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/fZ-ppJH3DKY/</link>
		<comments>http://theartofcode.com/2009/08/12/ideas-that-spread-win/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 20:29:19 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Business]]></category>

		<category><![CDATA[ideas]]></category>

		<category><![CDATA[marketing]]></category>

		<category><![CDATA[Seth Godin]]></category>

		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/08/12/ideas-that-spread-win/</guid>
		<description><![CDATA[Great talk by Seth Godin at a Business of Software conference:



He&#8217;s recycled some material from earlier talks, but of course it remains entertaining. His message: even in the software industry, we are in the idea business as much, or more than we are in the business of building function. To a great extent, software markets [...]]]></description>
			<content:encoded><![CDATA[<p>Great talk by <a title="Seth Godin&#39;s blog" href="http://sethgodin.typepad.com/">Seth Godin</a> at a <a title="Business of Software" href="http://www.businessofsoftware.org/">Business of Software</a> conference:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:3a4b86b9-b7c7-4e7b-ba4f-37eaa84abb59" class="wlWriterSmartContent">
<div><embed src="http://blip.tv/play/Ad6xPAI" type="application/x-shockwave-flash" width="480" height="390" allowscriptaccess="always" allowfullscreen="true"></embed></div>
</div>
<p>He&#8217;s recycled some material from earlier talks, but of course it remains entertaining. His message: even in the software industry, we are in the idea business as much, or more than we are in the business of building function. To a great extent, software markets itself and therefore one should build marketing into software (he uses the analogy of BMW spending money on engineering where Ford spends it on advertising).</p>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/fZ-ppJH3DKY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/08/12/ideas-that-spread-win/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/08/12/ideas-that-spread-win/</feedburner:origLink></item>
		<item>
		<title>A Better Way To Call A .NET Assembly From COM</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/CddFdqdrNp0/</link>
		<comments>http://theartofcode.com/2009/08/11/a-better-way-to-call-a-net-assembly-from-com/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 20:55:50 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Coding Techniques]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[COM]]></category>

		<category><![CDATA[early binding]]></category>

		<category><![CDATA[GUID]]></category>

		<category><![CDATA[interface]]></category>

		<category><![CDATA[interop]]></category>

		<category><![CDATA[late binding]]></category>

		<category><![CDATA[versioning]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/08/11/a-better-way-to-call-a-net-assembly-from-com/</guid>
		<description><![CDATA[In my previous post Call a C# Assembly From VB6 I mentioned that there was a better way to pull off the interop required for using a .NET assembly in VB6. You can think of the technique used in that article as the &#8220;quick and dirty&#8221; approach. It works, but leans heavily on .NET&#8217;s ability [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post <a title="Call a c# Assembly From VB6" href="http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/">Call a C# Assembly From VB6</a> I mentioned that there was a better way to pull off the interop required for using a .NET assembly in VB6. You can think of the technique used in that article as the &#8220;quick and dirty&#8221; approach. It works, but leans heavily on .NET&#8217;s ability to automatically specify the settings in the generated COM interface. That&#8217;s ok, but there is a cost: you have to recompile your COM clients every time you recompile your .NET component, and you cannot use late binding which, for example, is required for use by scripting clients. For just a little extra effort, you can exercise a greater level of control, and not have to pay these costs.</p>
<p>The technical background for what follows is covered very nicely in many places. I&#8217;m just going to provide a &#8220;how to&#8221; in what follows. For background on <em>why</em> it is done this way, you can check out <a title="COM Interop Exposed" href="http://www.15seconds.com/issue/040721.htm">COM Interop Exposed</a>, or another similar resource on the web.</p>
<p>I&#8217;ll reengineer the Reverser class from the <a title="Call a c# Assembly From VB6" href="http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/">Call a C# Assembly From VB6</a> article to use this better approach.</p>
<h2>Create an Interface</h2>
<p>Create an interface for the methods you want to expose in COM. The interface for the Reverser class would be defined like so:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Runtime.InteropServices;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span> <span style="color: #0000ff">namespace</span> String.Utilities {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     [Guid(<span style="color: #006080">"F60865B6-D2D7-4435-8B0B-D2A09D88548C"</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     [ComVisible(<span style="color: #0000ff">true</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IReverser {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>         <span style="color: #0000ff">string</span> Reverse(<span style="color: #0000ff">string</span> str);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>         <span style="color: #0000ff">string</span> BetterReverse(<span style="color: #0000ff">string</span> str);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span> }</pre>
</div>
</div>
<p>Notice that we have added a GUID. We have done this to control versioning. In the prior <a href="http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/">article</a> we noted that  you should recompile your COM clients every time the .NET component is changed. By specifying a GUID for our interface and classes, we will be able to change the implementation of our .NET component without requiring a recompile of our COM clients. See <a title="COM Interop Exposed" href="http://www.15seconds.com/issue/040721.htm">COM Interop Exposed</a> for more details.</p>
<h2>Implement the Interface</h2>
<p>Our new Reverser class will implement the new IReverser interface:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Runtime.InteropServices;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span> <span style="color: #0000ff">namespace</span> String.Utilities {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     [Guid(<span style="color: #006080">"80705AFF-708F-46f9-BA50-D69A96753A57"</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>     [ComVisible(<span style="color: #0000ff">true</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>     [ClassInterface(ClassInterfaceType.None)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Reverser : IReverser {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>     <span style="color: #008000">// Types must have a public default constructor   </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>     <span style="color: #008000">// to be instantiated through COM. Managed public  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>     <span style="color: #008000">// types are visible to COM, but without a public  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span>     <span style="color: #008000">// default constructor (a constructor without   </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>     <span style="color: #008000">// arguments), COM clients cannot create an   </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span>     <span style="color: #008000">// instance of the type.  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">public</span> Reverser() {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  20:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  21:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  22:</span>     <span style="color: #cc6633">#region</span> IReverser Members</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  23:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  24:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Reverse(<span style="color: #0000ff">string</span> str) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  25:</span>         <span style="color: #008000">// This method is from Mladen Prajdić's   </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  26:</span>         <span style="color: #008000">// I want some Moore blog:  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  27:</span>         <span style="color: #008000">// http://weblogs.sqlteam.com/mladenp/</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  28:</span>         <span style="color: #008000">// archive/2006/03/19/9350.aspx  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  29:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  30:</span>         <span style="color: #0000ff">char</span>[] charArray = str.ToCharArray();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  31:</span>         <span style="color: #0000ff">int</span> len = str.Length - 1;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  32:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  33:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; len; i++, len--) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  34:</span>             charArray[i] ^= charArray[len];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  35:</span>             charArray[len] ^= charArray[i];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  36:</span>             charArray[i] ^= charArray[len];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  37:</span>         }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  38:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>(charArray);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  39:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  40:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  41:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> BetterReverse(<span style="color: #0000ff">string</span> str) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  42:</span>         <span style="color: #008000">// This method is from Mladen Prajdić's   </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  43:</span>         <span style="color: #008000">// I want some Moore blog:  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  44:</span>         <span style="color: #008000">// http://weblogs.sqlteam.com/mladenp/</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  45:</span>         <span style="color: #008000">// archive/2006/03/19/9350.aspx  </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  46:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  47:</span>         <span style="color: #0000ff">char</span>[] charArray = <span style="color: #0000ff">new</span> <span style="color: #0000ff">char</span>[x.Length];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  48:</span>         <span style="color: #0000ff">int</span> len = x.Length - 1;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  49:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt;= len; i++)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  50:</span>             charArray[i] = x[len - i];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  51:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>(charArray);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  52:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  53:</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  54:</span>     <span style="color: #cc6633">#endregion</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  55:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  56:</span> }</pre>
</div>
</div>
<p>Notice that this class also defines a GUID. As noted for the IReverser interface, this is also done for the actual class to control versioning. Also, the ClassInterfaceAttribute specifies that no automatic interface is to be generated. In this case, the first interface implemented by the class will be the default interface in COM. This allows us to support late binding as well as early binding in COM clients (previously, only early binding was supported).</p>
<h2>Notes</h2>
<p>You will still have to set the project to register for COM interop. Note however that there is no need to change the ComVisiible setting in the AssemblyInfo.cs file:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> [assembly: ComVisible(<span style="color: #0000ff">false</span>)]</pre>
</div>
</div>
<p>In fact, by leaving it false, we can control exactly which interface methods are visible by overriding this setting with the [ComVisible(true)] attribute on an interface by interface, or method by method basis.</p>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/CddFdqdrNp0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/08/11/a-better-way-to-call-a-net-assembly-from-com/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/08/11/a-better-way-to-call-a-net-assembly-from-com/</feedburner:origLink></item>
		<item>
		<title>Using .NET To Access Google Data</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/sEUtb_wWEAY/</link>
		<comments>http://theartofcode.com/2009/06/10/using-net-to-access-google-data/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 20:10:22 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Design]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[client library]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[google data]]></category>

		<category><![CDATA[google data api]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/06/10/using-net-to-access-google-data/</guid>
		<description><![CDATA[Google Data includes the data used in Google services, data which is stored on the web. This includes Google Analytics data, Google Apps data (such as spreadsheets and documents), Blogger posts, Picasa Web Albums, Google Calendar data and much more. Google provides an API for accessing this data. Lately I&#8217;ve been working on how to [...]]]></description>
			<content:encoded><![CDATA[<p>Google Data includes the data used in Google services, data which is stored on the web. This includes Google Analytics data, Google Apps data (such as spreadsheets and documents), Blogger posts, Picasa Web Albums, Google Calendar data and much more. Google provides an <a title="The Google Data API" href="http://code.google.com/apis/gdata/">API</a> for accessing this data. Lately I&#8217;ve been working on how to access and modify Google Data using .NET. The obvious place to start is  the <a title="Google Data .NET Client Library" href="http://code.google.com/p/google-gdata/">Google Data .NET client library</a>. There is a nice <a title="Getting Started with the .NET Client Library" href="http://code.google.com/apis/gdata/articles/dotnet_client_lib.html">guide</a> for setting yourself up and getting started with this library, as well as developer guides for accessing Google data, such as <a title=".NET Developer's Guide" href="http://code.google.com/apis/spreadsheets/docs/2.0/developers_guide_dotnet.html">spreadsheet data</a>, using the .NET client library.</p>
<p>While the .NET client library provides an object oriented, programmatic interface to data stored in Google, the basis for the object model is based on Atom and the Atom Publishing Protocol. For example, the object model for accessing Google Spreadsheets is similar in many ways to the Excel object model, but in certain respects very different. While I don&#8217;t think one necessarily has to have intimate knowledge of the low level internals of Atom to use the .NET client library, I have found it helpful to understand them. What follows is the material I used to educate myself.</p>
<h2>Lesson 1: The Atom Publishing Protocol</h2>
<p>The Google Data API is based on the <a title="Atom Publishing Protocol" href="http://www.rfc-editor.org/rfc/rfc5023.txt">Atom Publishing Protocol</a> (&#8221;AtomPub&#8221;), which in turn is based on the <a title="Atom syndication format" href="http://en.wikipedia.org/wiki/Atom_(standard)">Atom</a> syndication format. The following video provides a nice introduction:</p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:a7864868-dda5-4866-b9d8-2c95c6384a6d" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="61ce9191-8d3c-4006-bcd1-9b6b1c8363e5" style="margin: 0px; padding: 0px; display: inline;">
<div><object width="425" height="355" data="http://www.youtube.com/v/T04fKsD56LU" type="application/x-shockwave-flash"><param name="wmode" value="transparent" /><param name="src" value="http://www.youtube.com/v/T04fKsD56LU" /></object></div>
</div>
</div>
<p><strong>Further Reading</strong>:</p>
<ul>
<li>Atom: <a href="http://www.ietf.org/rfc/rfc4287.txt">http://www.ietf.org/rfc/rfc4287.txt</a></li>
<li>Atom Publishing Protocol: <a href="http://www.rfc-editor.org/rfc/rfc5023.txt">http://www.rfc-editor.org/rfc/rfc5023.txt</a></li>
</ul>
<p>Since AtomPub is a <a title="REST" href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a>ful protocol, you may also enjoy an introduction to REST, although I don&#8217;t think this is strictly necessary for understanding the Google Data API:</p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:cd71a2b5-47a9-49aa-95e4-e4af9b002113" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="ad37aba1-8ad6-4865-b501-93b0be95ba08" style="margin: 0px; padding: 0px; display: inline;">
<div><object width="425" height="355" data="http://www.youtube.com/v/YCcAE2SCQ6k" type="application/x-shockwave-flash"><param name="wmode" value="transparent" /><param name="src" value="http://www.youtube.com/v/YCcAE2SCQ6k" /></object></div>
</div>
</div>
<h2>Lesson 2: How The Google Data API Uses AtomPub</h2>
<p>The following explains how the Google Data API is implemented using AtomPub, providing some good examples at the AtomPub level, including the concepts of a service, a query and a feed:</p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:ab1aa7dd-ee8a-4b2f-b9fc-56b40ab66ee2" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="b8f6aeb3-d877-449b-8061-61f992476731" style="margin: 0px; padding: 0px; display: inline;">
<div><object width="425" height="355" data="http://www.youtube.com/v/ADos_xW4_J0" type="application/x-shockwave-flash"><param name="wmode" value="transparent" /><param name="src" value="http://www.youtube.com/v/ADos_xW4_J0" /></object></div>
</div>
</div>
<h3>Lesson 3: Getting Starting With The .NET Client Library</h3>
<p>At this point, I find that it is now easier to understand the concepts behind the object model in the .NET client library. This final video gives an introduction to the .NET client library where you will see the concepts of authentication, services, queries and feeds being used:</p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:dd39df54-776b-4472-a7f1-67944c11594a" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="335e0cad-c4d7-440d-b4d9-1fadb58a93fd" style="margin: 0px; padding: 0px; display: inline;">
<div><object width="425" height="355" data="http://www.youtube.com/v/8ErOVHiu5K8" type="application/x-shockwave-flash"><param name="wmode" value="transparent" /><param name="src" value="http://www.youtube.com/v/8ErOVHiu5K8" /></object></div>
</div>
</div>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/sEUtb_wWEAY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/06/10/using-net-to-access-google-data/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/06/10/using-net-to-access-google-data/</feedburner:origLink></item>
		<item>
		<title>Call A C# Assembly From VB6</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/v_cmDLXuQoY/</link>
		<comments>http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 10:45:51 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Coding Techniques]]></category>

		<category><![CDATA[COM]]></category>

		<category><![CDATA[interop]]></category>

		<category><![CDATA[VB6]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/</guid>
		<description><![CDATA[I have had to figure this out twice now, so I will write it down this time: how to call and debug a c# assembly from VB6 in a development environment. The first thing you have to do is set up your c# assembly so that it is exposed as a COM object. This is [...]]]></description>
			<content:encoded><![CDATA[<p>I have had to figure this out twice now, so I will write it down this time: how to call and debug a c# assembly from VB6 in a development environment. The first thing you have to do is set up your c# assembly so that it is exposed as a COM object. This is more or less covered <a href="http://support.microsoft.com/kb/817248">here</a>, and I am sure elsewhere, but by way of an example, let&#8217;s create a .NET assembly that reverses strings (For this purpose, we will use some techniques outlined by Mladen Prajdić at his <a href="http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx">I want some Moore</a> blog).</p>
<p>I will create a String.Utilities project with a single public Reverser class. This class is implemented as follows:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.Text;  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Runtime.InteropServices;  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   5:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span> <span style="color: #0000ff">namespace</span> String.Utilities {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   7:</span>       </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>     <span style="color: #008000">// ClassInterfaceType.AutoDual allows early-bound  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   9:</span>     <span style="color: #008000">// COM clients and compile-time checking. Note that  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>     <span style="color: #008000">// it completely ignores COM versioning, and that   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  11:</span>     <span style="color: #008000">// you should recompile your COM clients every time  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>     <span style="color: #008000">// this .NET component is changed.  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  13:</span>     [ClassInterface(ClassInterfaceType.AutoDual)]  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Reverser {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  15:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>         <span style="color: #008000">// Types must have a public default constructor   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  17:</span>         <span style="color: #008000">// to be instantiated through COM. Managed public  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>         <span style="color: #008000">// types are visible to COM, but without a public  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  19:</span>         <span style="color: #008000">// default constructor (a constructor without   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>         <span style="color: #008000">// arguments), COM clients cannot create an   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  21:</span>         <span style="color: #008000">// instance of the type.  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>         <span style="color: #0000ff">public</span> Reverser() {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  23:</span>         }  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  25:</span>         <span style="color: #008000">// We use this attribute to specify which public  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  26:</span>         <span style="color: #008000">// methods will be visible to COM.  The default   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  27:</span>         <span style="color: #008000">// value is true for public methods, so this is  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  28:</span>         <span style="color: #008000">// not really necessary!  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  29:</span>         [ComVisible(<span style="color: #0000ff">true</span>)]  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  30:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Reverse(<span style="color: #0000ff">string</span> str) {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  31:</span>             <span style="color: #008000">// This method is from Mladen Prajdić's   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  32:</span>             <span style="color: #008000">// I want some Moore blog:  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  33:</span>             <span style="color: #008000">// http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  34:</span>               </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  35:</span>             <span style="color: #0000ff">char</span>[] charArray = str.ToCharArray();  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  36:</span>             <span style="color: #0000ff">int</span> len = str.Length - 1;  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  37:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  38:</span>             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; len; i++, len--) {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  39:</span>                 charArray[i] ^= charArray[len];  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  40:</span>                 charArray[len] ^= charArray[i];  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  41:</span>                 charArray[i] ^= charArray[len];  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  42:</span>             }  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  43:</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>(charArray);  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  44:</span>         }  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  45:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  46:</span>         [ComVisible(<span style="color: #0000ff">true</span>)]  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  47:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> BetterReverse(<span style="color: #0000ff">string</span> x) {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  48:</span>             <span style="color: #008000">// This method is from Mladen Prajdić's   </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  49:</span>             <span style="color: #008000">// I want some Moore blog:  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  50:</span>             <span style="color: #008000">// http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  51:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  52:</span>             <span style="color: #0000ff">char</span>[] charArray = <span style="color: #0000ff">new</span> <span style="color: #0000ff">char</span>[x.Length];  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  53:</span>             <span style="color: #0000ff">int</span> len = x.Length - 1;  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  54:</span>             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt;= len; i++)  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  55:</span>                 charArray[i] = x[len - i];  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  56:</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>(charArray);  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  57:</span>         }  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  58:</span>     }  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  59:</span> }  </pre>
</p></div>
</div>
<p>Code is interop ready, now we just need to set up some project settings. First, we have to update (or add) the ComVisible setting in the c# project&#8217;s AssemblyInfo.cs file (this is the thing I always forget to do):</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #008000">// Setting ComVisible to false makes the types in this assembly not visible  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #008000">// to COM components.  If you need to access a type in this assembly from  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #008000">// COM, set the ComVisible attribute to true on that type.  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> [assembly: ComVisible(<span style="color: #0000ff">true</span>)] </pre>
</p></div>
</div>
<p>Next, open the project&#8217;s properties and click on the Build tab. Ensure the <strong>Register for COM interop</strong> option is selected:</p>
<p><a href="http://theartofcode.com/wp-content/uploads/2009/04/buildoption.jpg"><img height="103" alt="buildoption" src="http://theartofcode.com/wp-content/uploads/2009/04/buildoption-thumb.jpg" width="400" /></a> </p>
<p>In order to be able to debug this while running the VB6 client from the development environment, you want to go to the Debug tab and select the <strong>Start external program</strong> option specifying the path to the VB6.exe.</p>
<p>Now we can build the assembly. When we debug, as specified in the debug options, VB6 will start. You can then open an existing project (or create a new one) that you want to use as a client for the .NET service. Click on the (VB6&#8217;s) project references and select the .NET assembly (which is now exposed as a COM object):</p>
<p><a href="http://theartofcode.com/wp-content/uploads/2009/04/vb6ref.jpg"><img height="249" alt="vb6ref" src="http://theartofcode.com/wp-content/uploads/2009/04/vb6ref-thumb.jpg" width="400" /></a> </p>
<p>Here is my VB6 client code for calling the .NET assembly:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">Private</span> <span style="color: #0000ff">Sub</span> cmdReverse_Click()  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #008000">' Notice how the dot (&quot;.&quot;) came across as  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> <span style="color: #008000">' an underscore!  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   5:</span> <span style="color: #0000ff">Dim</span> aReverser <span style="color: #0000ff">As</span> String_Utilities.Reverser  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span> <span style="color: #0000ff">Dim</span> reversed <span style="color: #0000ff">As</span> <span style="color: #0000ff">String</span>  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   7:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span> <span style="color: #008000">' Instantiate the .NET Reverser object  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   9:</span> <span style="color: #0000ff">Set</span> aReverser = <span style="color: #0000ff">New</span> String_Utilities.Reverser  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  11:</span> <span style="color: #008000">' Call a method in the assembly  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span> reversed = aReverser.Reverse(txtString.Text)  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  13:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span> <span style="color: #008000">' Show the user the text in reverse  </span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  15:</span> lblReverse.Caption = reversed  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>   </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  17:</span> <span style="color: #0000ff">End</span> Sub</pre>
</p></div>
</div>
<p>Running the VB6 client and clicking the button gives the following:</p>
<p><a href="http://theartofcode.com/wp-content/uploads/2009/04/revsample.jpg"><img height="240" alt="revsample" src="http://theartofcode.com/wp-content/uploads/2009/04/revsample-thumb.jpg" width="320" /></a> </p>
<p>Of course, within the VB6 environment you can fully debug the client (set breakpoints, add watches, etc.). Now, run the VB6 project again, but this time set a breakpoint at <strong>public string Reverse(string str)</strong> in the Reverser class in VS2005. When you click the button, you will break at the Reverse method in the .NET debugging environment:</p>
<p><a href="http://theartofcode.com/wp-content/uploads/2009/04/debugsample.jpg"><img height="139" alt="debugsample" src="http://theartofcode.com/wp-content/uploads/2009/04/debugsample-thumb.jpg" width="400" /></a> </p>
<p>Therefore we can call an assembly from a VB6 application and debug both sets of code at the same time. A couple things: (1) the assembly is only registered as a COM object for the lifetime of the debugging session initiated from VS2005. So you cannot open VB6 and debug on its own without building and registering the assembly as a COM object (you use the <a href="http://msdn2.microsoft.com/en-us/library/tzat5yw6(VS.71).aspx">regasm</a> utility). (2) there is a better way than using <strong>ClassInterfaceType.AutoDual</strong> to expose the class in COM which I will cover another day.</p>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/v_cmDLXuQoY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/04/27/call-a-c-assembly-from-vb6/</feedburner:origLink></item>
		<item>
		<title>Testability Guide</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/6l-ycUHchmY/</link>
		<comments>http://theartofcode.com/2009/03/11/testability-guide/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 14:38:25 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Coding Techniques]]></category>

		<category><![CDATA[automated testing]]></category>

		<category><![CDATA[clean code talks]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[misko hevery]]></category>

		<category><![CDATA[testable code]]></category>

		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/03/11/testability-guide/</guid>
		<description><![CDATA[ Mi&#353;ko Hevery works at Google and is responsible for maintaining a culture of automated testing. He has recently published a Guide to Writing Testable Code. It is available on his site and as a pdf download. Besides making code more testable, I believe that the guidelines also represent coding best practices in general and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://theartofcode.com/wp-content/uploads/2009/03/testing.jpg"><img style="margin: 10px" height="201" alt="testing" src="http://theartofcode.com/wp-content/uploads/2009/03/testing-thumb.jpg" width="240" align="right" /></a> Mi&#353;ko Hevery works at Google and is responsible for maintaining a culture of automated testing. He has recently published a <a title="Guide: Writing Testbale Code" href="http://misko.hevery.com/2009/03/09/guide-to-testability-is-now-downloadable/">Guide to Writing Testable Code</a>. It is available on his <a title="code reviewers guide" href="http://misko.hevery.com/code-reviewers-guide/">site</a> and as a pdf download. Besides making code more testable, I believe that the guidelines also represent coding best practices in general and I urge you to get a copy of the guide.</p>
<p>I first discovered Mi&#353;ko through Google&#8217;s <a title="Clean Code talks" href="http://www.youtube.com/results?search_type=&amp;search_query=clean+code+talks&amp;aq=f">Clean Code talks</a>. My favourite talk is the one on Inheritance, Polymorphism &amp; Testing:</p>
<p>
<div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:502999ec-fbe3-4027-bbd4-b351e3fbd071" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="28b7cd5a-cc25-4c32-bac7-026187d3153b" style="margin: 0px; padding: 0px; display: inline;">
<div><a href="http://www.youtube.com/watch?v=4F72VULWFvc" target="_new"><img src="http://theartofcode.com/wp-content/uploads/2009/03/videoffdf6adf74bd.jpg" galleryimg="no" onload="var downlevelDiv = document.getElementById('28b7cd5a-cc25-4c32-bac7-026187d3153b'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/4F72VULWFvc\&quot;&gt;&lt;\/param&gt;&lt;param name=\&quot;wmode\&quot; value=\&quot;transparent\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;http://www.youtube.com/v/4F72VULWFvc\&quot; type=\&quot;application/x-shockwave-flash\&quot; wmode=\&quot;transparent\&quot; width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div>
</div>
</div>
<p>&#160;</p>
<p>Here are the others&#8230;</p>
<p>Unit Testing:</p>
<div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:332689a8-c5be-4612-8fba-4a6ebaeb219f" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="164e7646-0edf-4b12-bba6-12178ce4afb5" style="margin: 0px; padding: 0px; display: inline;">
<div><a href="http://www.youtube.com/watch?v=wEhu57pih5w" target="_new"><img src="http://theartofcode.com/wp-content/uploads/2009/03/video519e588cc3c7.jpg" galleryimg="no" onload="var downlevelDiv = document.getElementById('164e7646-0edf-4b12-bba6-12178ce4afb5'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/wEhu57pih5w\&quot;&gt;&lt;\/param&gt;&lt;param name=\&quot;wmode\&quot; value=\&quot;transparent\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;http://www.youtube.com/v/wEhu57pih5w\&quot; type=\&quot;application/x-shockwave-flash\&quot; wmode=\&quot;transparent\&quot; width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div>
</div>
</div>
<p>&#160;</p>
<p>Don&#8217;t Look For Things:</p>
<div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:fe445d02-8f28-4533-8fff-600eda4c55e6" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="3eb8f70f-c482-4cb0-b1b7-bbf552cdb8fa" style="margin: 0px; padding: 0px; display: inline;">
<div><a href="http://www.youtube.com/watch?v=RlfLCWKxHJ0" target="_new"><img src="http://theartofcode.com/wp-content/uploads/2009/03/videofbff249e5d5d.jpg" galleryimg="no" onload="var downlevelDiv = document.getElementById('3eb8f70f-c482-4cb0-b1b7-bbf552cdb8fa'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/RlfLCWKxHJ0\&quot;&gt;&lt;\/param&gt;&lt;param name=\&quot;wmode\&quot; value=\&quot;transparent\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;http://www.youtube.com/v/RlfLCWKxHJ0\&quot; type=\&quot;application/x-shockwave-flash\&quot; wmode=\&quot;transparent\&quot; width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div>
</div>
</div>
<p>&#160;</p>
<p>Global State and Singletons:</p>
<div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:052e255c-0baf-4e3c-9d58-eb005dbc67ef" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div id="efcf7f3c-f5ed-4288-ac3a-ca577b25020d" style="margin: 0px; padding: 0px; display: inline;">
<div><a href="http://www.youtube.com/watch?v=-FRm3VPhseI" target="_new"><img src="http://theartofcode.com/wp-content/uploads/2009/03/video1401e4cb666b.jpg" galleryimg="no" onload="var downlevelDiv = document.getElementById('efcf7f3c-f5ed-4288-ac3a-ca577b25020d'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/-FRm3VPhseI\&quot;&gt;&lt;\/param&gt;&lt;param name=\&quot;wmode\&quot; value=\&quot;transparent\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;http://www.youtube.com/v/-FRm3VPhseI\&quot; type=\&quot;application/x-shockwave-flash\&quot; wmode=\&quot;transparent\&quot; width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div>
</div>
</div>
<p>&#160;</p>
<p>&#160;</p>
<p>Photo Credit: <a title="Matthew Venn" href="http://www.flickr.com/people/matthewvenn/">Matthew Venn</a></p>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/6l-ycUHchmY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/03/11/testability-guide/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/03/11/testability-guide/</feedburner:origLink></item>
		<item>
		<title>Abstract Factory Or Factory Method</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/kYVBscCRD3I/</link>
		<comments>http://theartofcode.com/2009/02/18/abstract-factory-or-factory-method/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 04:18:45 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Coding Techniques]]></category>

		<category><![CDATA[abstract factory]]></category>

		<category><![CDATA[creational patterns]]></category>

		<category><![CDATA[design patterns]]></category>

		<category><![CDATA[factory method]]></category>

		<category><![CDATA[gang of four]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/02/18/abstract-factory-or-factory-method/</guid>
		<description><![CDATA[ The Gang of Four, in their seminal work Design Patterns: Elements of Reusable Object-Oriented Software, describe several creational design patterns. First off, you should be convinced that it is a best practice to separate the classes that actually do stuff from the classes that create the objects that do stuff. If you are, then [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://theartofcode.com/wp-content/uploads/2009/02/hatfactory.jpg"><img style="margin: 10px 0px 10px 10px" height="180" alt="hatFactory" src="http://theartofcode.com/wp-content/uploads/2009/02/hatfactory-thumb.jpg" width="240" align="right" /></a> The Gang of Four, in their seminal work <a title="Design Patterns" href="http://www.amazon.com/gp/product/0201633612?ie=UTF8&amp;tag=4houworweejou-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0201633612">Design Patterns: Elements of Reusable Object-Oriented Software</a>, describe several <em>creational</em> design patterns. First off, you should be convinced that it is a best practice to separate the classes that actually do stuff from the classes that create the objects that do stuff. If you are, then these patterns are important. These patterns include the factory method and abstract factory patterns. It has often confused me as to when you should use either. I found a rather good overview in some computer science <a title="creational design patterns" href="http://www.cs.toronto.edu/~wl/teach/407/w6handout.pdf">course notes</a>. Let me quote the relevant section:</p>
<blockquote><p>Sometimes it is difficult to differentiate between abstract factory and factory method. Solution?</p>
<ul>
<li>In the factory method, your class looks down (inheritance) for help in creating the object of specific type.</li>
<li>In abstract factory, it looks to the left or right (association) for that help.</li>
</ul>
<p>So use factory method if:</p>
<ol>
<li>your class depends on a subclass to decide what object to create.</li>
<li>inheritance plays a vital role.</li>
</ol>
<p>However, if your class depends on another class, which may be abstract and you are associated with it, then use abstract factory.</p>
</blockquote>
<p>Alright, that&#8217;s kind of helpful. Here is how I think about it. You use the abstract factory when you have different <em>families</em> of similar things you need to create. You use the factory method when you have one family of things you need to create. </p>
<p>For example, you have one store that creates several different kinds of pizza (cheese, pepperoni, vegetable): use the factory method. You have two pizza store chains that each produce different kinds of pizza (cheese, pepperoni, vegetable): use the abstract factory.</p>
<p>&#160;</p>
<p>Photo Credit: <a title="Sara Richards" href="http://www.flickr.com/photos/sararichards/">Sara Richards</a></p>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/kYVBscCRD3I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/02/18/abstract-factory-or-factory-method/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/02/18/abstract-factory-or-factory-method/</feedburner:origLink></item>
		<item>
		<title>BugzReport v0.2.5</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/N6YemOO7LaE/</link>
		<comments>http://theartofcode.com/2009/01/28/bugzreport-v025/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 00:36:26 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Products]]></category>

		<category><![CDATA[bug tracking]]></category>

		<category><![CDATA[bugzreport]]></category>

		<category><![CDATA[fogbugz]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/01/28/bugzreport-v025/</guid>
		<description><![CDATA[ Hot on the heels of v0.2.0, we&#8217;ve released version 0.2.5, an update which is now available for download. You can read more about what BugzReport is here, here and even here!
This update includes a few new features:

Added support for exporting cases to tab delimited files.
HTML code in the case fields will be decoded nicely [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://theartofcode.com/wp-content/uploads/2009/01/bug.jpg"><img height="160" alt="bug" src="http://theartofcode.com/wp-content/uploads/2009/01/bug-thumb.jpg" width="240" align="right" /></a> Hot on the heels of v0.2.0, we&#8217;ve released version 0.2.5, an update which is now available for <a title="BugzReport Download" href="http://code.google.com/p/bugzreport/">download</a>. You can read more about what BugzReport is <a title="export a filter to csv" href="http://www.fogcreek.com/FogBugz/blog/post/Export-a-Filter-to-CSV.aspx">here</a>, <a title="bugzreport" href="http://code.google.com/p/bugzreport/">here</a> and even <a title="bugzreport v0.2.0" href="http://theartofcode.com/2009/01/27/bugzreport-v020/">here</a>!</p>
<p>This update includes a few new features:</p>
<ul>
<li>Added support for exporting cases to tab delimited files.</li>
<li>HTML code in the case fields will be decoded nicely in the client and the export.</li>
<li>Made it easier to select (click on) fields to include in the queries.</li>
</ul>
<p>There were also a bunch of <a href="http://code.google.com/p/bugzreport/source/detail?r=8">bug fixes</a>. I&#8217;d also like to thank Nick and Christian for their valuable feedback. Enjoy!</p>
<p>Photo Credit: <a href="http://www.flickr.com/photos/big_mouth/">_big_mouth_</a></p>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/N6YemOO7LaE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/01/28/bugzreport-v025/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/01/28/bugzreport-v025/</feedburner:origLink></item>
		<item>
		<title>BugzReport v0.2.0</title>
		<link>http://feedproxy.google.com/~r/TheArtOfCode/~3/1wcYKwlUtSk/</link>
		<comments>http://theartofcode.com/2009/01/27/bugzreport-v020/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 09:27:34 +0000</pubDate>
		<dc:creator>John Januszczak</dc:creator>
		
		<category><![CDATA[Products]]></category>

		<category><![CDATA[bugzreport]]></category>

		<category><![CDATA[fog creek]]></category>

		<category><![CDATA[fogbugz]]></category>

		<guid isPermaLink="false">http://theartofcode.com/2009/01/27/bugzreport-v020/</guid>
		<description><![CDATA[BugzReport v0.2.0 is now available for download.&#160; 
 BugzReport is a Windows desktop client application that can be used to generate reports on cases logged in a FogBugz installation or a FogBugz On Demand hosted installation. BugReport allows you to export these reports to files that can be used by other applications such as Microsoft [...]]]></description>
			<content:encoded><![CDATA[<p>BugzReport v0.2.0 is now available for <a title="BugzReport Download" href="http://code.google.com/p/bugzreport/">download</a>.&#160; <a href="http://theartofcode.com/wp-content/uploads/2009/01/bugzreportquery.jpg"><img style="margin: 10px" height="240" alt="bugzReportQuery" src="http://theartofcode.com/wp-content/uploads/2009/01/bugzreportquery-thumb.jpg" width="225" align="right" /></a></p>
<p> BugzReport is a Windows desktop client application that can be used to generate reports on cases logged in a FogBugz installation or a FogBugz On Demand hosted installation. BugReport allows you to export these reports to files that can be used by other applications such as Microsoft Excel.</p>
<p><a title="FogBugz" href="http://www.fogcreek.com/">FogBugz</a>, a product of Fog Creek Software, is a web based solution that helps organizations make better software. It allows organizations to track feature requests, customer email, bugs, and other artifacts. It&#8217;s a pretty great application that I use in my work.</p>
<p>v0.2.0 includes the following updates:</p>
<ul>
<li>added the area field, as well as the &quot;opened by&quot;, &quot;resolved by&quot; and &quot;closed by&quot; fields to the query options. </li>
<li>changed the query options dialog so that all the fields are checked by default (I found     <br />I was checking most of them every time I ran a query).</li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheArtOfCode/~4/1wcYKwlUtSk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theartofcode.com/2009/01/27/bugzreport-v020/feed/</wfw:commentRss>
		<feedburner:origLink>http://theartofcode.com/2009/01/27/bugzreport-v020/</feedburner:origLink></item>
	</channel>
</rss>
