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

<channel>
	<title>Cameron McKay &#187; .net</title>
	<atom:link href="http://cdmckay.org/blog/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Tue, 11 Oct 2011 05:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Anders Hejlsberg talks about C# 4.0</title>
		<link>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/</link>
		<comments>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 01:46:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 4.0]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1365</guid>
		<description><![CDATA[Channel 9 has posted a great talk by Anders Hejlsberg, the original author of Turbo Pascal, the chief architect of Delphi, and the lead architect of C#. History lesson: Anders was lured away from Borland by Microsoft with a hefty offer of a $1.5 million signing bonus, a base salary of up to $200,000, and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://channel9.msdn.com/posts/matthijs/C-40-and-beyond-by-Anders-Hejlsberg/">Channel 9 has posted a great talk</a> by <a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg">Anders Hejlsberg</a>, the original author of Turbo Pascal, the chief architect of Delphi, and the lead architect of C#.</p>
<p>History lesson: Anders was <a href="http://news.cnet.com/2009-1023-229218.html">lured away from Borland by Microsoft</a> with a hefty offer of a $1.5 million signing bonus, a base salary of up to $200,000, and options to buy 75,000 shares of Microsoft stock.</p></blockquote>
<p>Looks like it was worth it, as each C# release has added many interesting and powerful features, while maintaining a fine balance between complexity and simplicity.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inconsistencies in the .NET Enum class</title>
		<link>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/</link>
		<comments>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 05:59:44 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Dnum]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[enum]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=948</guid>
		<description><![CDATA[During a recent project of mine, I had to do a lot of enum manipulation using the .NET Enum class and I have to say I wasn&#8217;t impressed. Besides the shortcoming of it not being a generic class (and thus not being especially type-safe), Enum also has some strange inconsistencies in terms of how it [...]]]></description>
			<content:encoded><![CDATA[<p>During a recent project of mine, I had to do a lot of enum manipulation using the .NET Enum class and I have to say I wasn&#8217;t impressed.  Besides the shortcoming of it not being a generic class (and thus not being especially type-safe), Enum also has some strange inconsistencies in terms of how it handles matched integral types.</p>
<p><span id="more-948"></span></p>
<h3>GetName</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx">GetName</a></code> return null for any integral type you pass it except those that correspond to a constant?</p>
<p>For example, this is completely valid:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">byte</span> <span style="color: #008000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var fruit <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetName</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// fruit now equals null</span></pre></div></div>

<p>In every other case in the Enum class where you pass an integral value outside the range of the underlying type, you get an exception.  Except here.</p>
<h3>IsDefined</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx">IsDefined</a></code> throw an exception when you pass it an integral value that can be safely narrowed to the enumeration&#8217;s underlying type?</p>
<p>For example, this will throw an exception:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">byte</span> <span style="color: #008000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var defined <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsDefined</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Why?  Because it&#8217;s a integer literal (and therefore type <code>int</code>) and the underlying type of <code>Fruit</code> is <code>byte</code>.  This would be alright if this is how the Enum class consistently treated this case, but it&#8217;s not.  In every other method, the Enum class is OK with you passing in an <code>int</code> value for a <code>byte</code> enumeration (as long as it&#8217;s within the range of the underlying type).</p>
<h3>ToObject</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.toobject.aspx">ToObject</a></code> silently overflow for values outside the range of the enumeration&#8217;s underlying type?</p>
<p>For example, this code will give the following non-intuitive result:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">byte</span> <span style="color: #008000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var apple <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToObject</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var elppa <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToObject</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">256</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>apple, elppa<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Assuming that we ignore the fact that silently overflowing is typically a bad idea, this is again completely inconsistent with the rest of the library.  Why overflow here, but not in <code>GetName</code>?</p>
<h3>Conclusion</h3>
<p>The .NET Enum class is not lacking in it&#8217;s ability to extract any information you need from an enumeration.  It is, however, a little inconsistent in terms of how it treats underlying type mismatches, overflows and narrowing conversions.  Barring a new, generified Enum class from Microsoft, the best way to deal with these inconsistencies is to either be aware of them, or use a third-party library to hide them.</p>
<p><em>(Edit: Some people have noted that my conclusion was a bit weak and preachy towards Dnum, which I have to admit is not without truth.  I&#8217;ve seen edited it.  For the record, the old conclusion was: &#8220;Fortunately, these and other problems have been fixed in my <a href="http://cdmckay.org/blog/dnum/">Dnum</a> library, which should be released in the next couple days.  Be sure to check it out.&#8221;).</em></p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Fep Collections</title>
		<link>http://cdmckay.org/blog/2009/07/29/fep-collections/</link>
		<comments>http://cdmckay.org/blog/2009/07/29/fep-collections/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 20:47:57 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Fep]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[dictionaries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=863</guid>
		<description><![CDATA[As I was working on the PHP classes that Fep was to map to, I realized that it was rather silly for me to re-invent an API for lists since there were already many well-designed ones in existence. In particular, I&#8217;ve found that the .NET 3.5 collections API to be particularly nice to use. Moreover, [...]]]></description>
			<content:encoded><![CDATA[<p>As I was working on the PHP classes that Fep was to map to, I realized that it was rather silly for me to re-invent an API for lists since there were already many well-designed ones in existence.  In particular, I&#8217;ve found that the .NET 3.5 collections API to be particularly nice to use.  Moreover, since everyone and their dog and using .NET now, it would also be familiar to most programmers.  Thus, I&#8217;ve decided to use a subset of the .NET collections API for Fep collections.</p>
<p><span id="more-863"></span></p>
<p>Furthermore, after some thought, I&#8217;ve decided to split up Fep arrays.  In PHP arrays aren&#8217;t really arrays in the traditional sense, but more like array/associate array hybrids (JavaScript uses a similar concept).  This can be confusing, error-prone and I don&#8217;t think that the benefits of having an hybrid array system outweighs the complexity.</p>
<p>Thus, in Fep, there will be two main collection types: lists and dictionaries.  Here&#8217;s how a Fep list will look like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Fep</span>
fep <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bar&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
fep<span style="color: #339933;">.</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == 4</span>
fep<span style="color: #339933;">.</span>add<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;baz&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fep<span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span> x <span style="color: #339933;">=&gt;</span> x <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == 2</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will compile into this PHP</span>
<span style="color: #000088;">$fep</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FepList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;baz&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$x</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> x <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And here&#8217;s how a Fep dictionary will look:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Fep</span>
fep <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> blah<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #339933;">,</span> foo<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;zero&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
fep<span style="color: #339933;">.</span>keys<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == [ &quot;blah&quot;, &quot;foo&quot;, 0 ]</span>
fep<span style="color: #339933;">.</span>containsKey<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == true</span>
fep<span style="color: #339933;">.</span>remove<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == true</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// PHP</span>
<span style="color: #000088;">$fep</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FepDict<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;blah&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span> 
         <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;zero&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">keys</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">containsKey</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/29/fep-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring Java and VB.Net Syntax, Part 1</title>
		<link>http://cdmckay.org/blog/2009/02/04/exploring-java-and-vbnet-syntax-part-1/</link>
		<comments>http://cdmckay.org/blog/2009/02/04/exploring-java-and-vbnet-syntax-part-1/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 07:48:10 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 1.1]]></category>
		<category><![CDATA[arraylist]]></category>
		<category><![CDATA[generic programming]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vb6]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=93</guid>
		<description><![CDATA[The syntax of a programming language is a large part of how a programmer interacts with a language. Being somewhat of a programming language enthusiast, I&#8217;m always curious about how different programming languages stack up syntactically. Today, I&#8217;m going to take a look at an variety of programming tasks and show how they are done [...]]]></description>
			<content:encoded><![CDATA[<p>The syntax of a programming language is a large part of how a programmer interacts with a language.  Being somewhat of a programming language enthusiast, I&#8217;m always curious about how different programming languages stack up syntactically.  </p>
<p>Today, I&#8217;m going to take a look at an variety of programming tasks and show how they are done in both Java and VB.NET (the .NET 1.1 version).  Since both these languages have differing feature sets, I&#8217;ll try to only use features that both the languages have, in order to compare them fairly.  I know this might misrepresent the complexity of the languages, and I&#8217;m cool with that.  The purpose of this post is merely to explore and discuss the syntax of each language.</p>
<p><span id="more-93"></span></p>
<h3>Collections</h3>
<p>Creating and manipulating collections is a common task in <a href="http://en.wikipedia.org/wiki/Procedural_programming">procedural programming</a> languages, so let&#8217;s look at that first.  Consider arrays.  To make an array of 10 String objects in Java, I would do this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ARRAY_LENGTH <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span>ARRAY_LENGTH<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In VB.NET, things get a bit more complicated, but for the most part, you&#8217;d do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Const</span> ARRAY_LENGTH <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span>
<span style="color: #0600FF;">Dim</span> arr<span style="color: #000000;">&#40;</span>ARRAY_LENGTH <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></pre></div></div>

<p>Woah, what?  Why is it ARRAY_LENGTH &#8211; 1?  That&#8217;s because VB.Net is an evolution of VB6, and in VB6, for reasons unknown, the value in the parenthesis is not the number of elements but instead the maximum index.  There&#8217;s a second way to create arrays in VB.NET using anonymous arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> arr<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #808080;">&quot;foo&quot;</span>, <span style="color: #808080;">&quot;bar&quot;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>You may also create arrays using anonymous arrays in Java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;foo&quot;</span>, <span style="color: #0000ff;">&quot;bar&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now let&#8217;s look at something a bit more powerful: the ArrayList.  Both Java and VB have a version of this class with more or less the same syntax.  Let&#8217;s use them in a real world situation.  Consider this task: We have an ArrayList with a bunch of Strings and we want to join them together using a comma.  Here&#8217;s the code to do that in VB without using generics:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' Option Strict is On (as it should be).</span>
<span style="color: #0600FF;">Dim</span> list <span style="color: #FF8000;">As</span> ArrayList <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> ArrayList
list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;a&quot;</span><span style="color: #000000;">&#41;</span>
list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;b&quot;</span><span style="color: #000000;">&#41;</span>
list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;c&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the comma-joined string.</span>
<span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">str</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">String</span>.<span style="color: #0600FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;,&quot;</span>, _ 
    DirectCast<span style="color: #000000;">&#40;</span>list.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #804040;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF8000;">String</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>That&#8217;s quite a jumble at the end there.  Let&#8217;s see we would do it in Java (again, without generics):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the comma-joined string.  Let's assume that we've </span>
<span style="color: #666666; font-style: italic;">// created a method called join(String str, String[] strList) </span>
<span style="color: #666666; font-style: italic;">// that acts the same as .NET's String.Join().</span>
<span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> join<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span>, <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> list.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>(Aside: Anyone familiar with generics is probably beating their head against their desk.  This example is just screaming for it.  I&#8217;ll include the generified version of the Java example at the end for the curious.)</em></p>
<p>Same problem in Java.  Converting from an ArrayList (or any Collection for that matter) does not look pretty (and is somewhat complicated for a novice programmer), even though it doesn&#8217;t seem like it&#8217;s a lot of ask.  We have a collection of things, and we want to put them into an array, which is a very basic type of a collection.  What&#8217;s the problem?</p>
<p>Basically, it has to do with the programming language not knowing what&#8217;s in the ArrayList.  If we just wanted to convert to an array of objects, that&#8217;s no problem:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Imagine we still have the &quot;list&quot; object </span>
<span style="color: #666666; font-style: italic;">// from the previous example.</span>
<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> objArray <span style="color: #339933;">=</span> list.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This works because the programming language can be sure that all of the items in the ArrayList are at least Objects.  Unfortunately, in most cases, that&#8217;s not especially helpful.  Fortunately, both these languages have a solution to the problem and that&#8217;s telling the programming language that the ArrayList is an ArrayList of Strings.  The technology that does that is called <a href="http://en.wikipedia.org/wiki/Generic_programming">generic programming</a>.</p>
<p>That&#8217;s it for today.  In part 2, we&#8217;ll delve deeper into the wordiness of VB.NET and how it drives you nuts if you program with it on a regular basis.  Here&#8217;s the generified version of the Java program for the keeners:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the comma-joined string.  Let's assume that we've </span>
<span style="color: #666666; font-style: italic;">// created a method called join(String str, String[] strList) </span>
<span style="color: #666666; font-style: italic;">// that acts the same as .NET's String.Join().</span>
<span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> join<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span>, list.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/04/exploring-java-and-vbnet-syntax-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating an HTML document with .NET</title>
		<link>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/</link>
		<comments>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 08:15:09 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[visual basic]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=43</guid>
		<description><![CDATA[Sometimes when you&#8217;re programming, you&#8217;ll want to generate an HTML document. This may be because you&#8217;re writing some sort of web app, or maybe you want your program to be able to output in a web-friendly format. The String Concatenation Approach Whatever your reason is for generating HTML, the best bet is your first prototype [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when you&#8217;re programming, you&#8217;ll want to generate an HTML document.  This may be because you&#8217;re writing some sort of web app, or maybe you want your program to be able to output in a web-friendly format.</p>
<p><span id="more-43"></span></p>
<h3>The String Concatenation Approach</h3>
<p>Whatever your reason is for generating HTML, the best bet is your first prototype looked something like this:</p>
<p><em>(Aside: All this code is in VB.NET&#8230; the original VB.NET (i.e. not 2005 or later).  It also assumes ASP.NET 1.1.  This is because the client I work for still uses this technology, so all my experience is with it.  The code should still be fine though, as all versions of .NET after 1.1 are more or less backwards compatible.)</em></p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #008000;">' Run some query on the database.</span>
<span style="color: #990099; font-weight: bold;">Dim</span> dataTable <span style="color: #330066;">As</span> DataTable <span style="color: #006600; font-weight: bold;">=</span> Query<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' Dump it into an HTML document.</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;html&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;head&gt;&lt;title&gt;Database Table&lt;/title&gt;&lt;/head&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span>Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;body&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;table&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' Print the rows.</span>
<span style="color: #990099; font-weight: bold;">For</span> <span style="color: #990099; font-weight: bold;">Each</span> row <span style="color: #330066;">As</span> DataRow <span style="color: #990099; font-weight: bold;">In</span> dataTable
  <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;tr&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #008000;">' Print the cells.</span>
  <span style="color: #990099; font-weight: bold;">For</span> <span style="color: #990099; font-weight: bold;">Each</span> item <span style="color: #330066;">As</span> Object <span style="color: #990099; font-weight: bold;">In</span> row
    <span style="color: #990099; font-weight: bold;">If</span> IsDBNull<span style="color: #006600; font-weight:bold;">&#40;</span>item<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">False</span> <span style="color: #990099; font-weight: bold;">Then</span>
      <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;td&gt;&quot;</span> + <span style="color: #990099; font-weight: bold;">CStr</span><span style="color: #006600; font-weight:bold;">&#40;</span>item<span style="color: #006600; font-weight:bold;">&#41;</span> + <span style="color: #cc0000;">&quot;&lt;/td&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">Else</span>
      <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">If</span>
  <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">For</span>
  <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">For</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/table&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/body&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/html&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now don&#8217;t get me wrong, this code will do the trick.  But as you write it, you start to feel a bit dirty.  HTML is a language for representing hierarchies, yet you&#8217;re concatenating strings.  You start to feel like there&#8217;s a more elegant way to do it, a way that takes advantage of the structure of HTML.</p>
<p>And there is.</p>
<h3>The Programmatic Approach</h3>
<p>Ok, so we want to take an approach that has some knowledge of the HTML we&#8217;re writing.  Enter XML.  .NET uses XML for, among other things, it&#8217;s <a href="http://en.wikipedia.org/wiki/Javadoc">Javadoc</a>-like documentation generator and it&#8217;s ASP.NET config files.  With XML so prevalent in .NET, it&#8217;s not surprising to discover that it&#8217;s part of the standard library, in the namespace <code>System.Xml</code>.</p>
<p>So you&#8217;re thinking &#8220;Cameron, who cares about XML, I want to make an HTML document!&#8221;  Gear down, sugar plum, XML can still help!  As you may or may not know, HTML (at least the XHTML dialects) is a reformulation of XML.  That is, an XHTML document is an XML document.  As a result, we can use .NET&#8217;s built-in XML support to generate an HTML document.</p>
<p>Thus, if we want to write the same code in previous example in .NET XML, we would do the following.  First, we would need to make a new XML document.  That&#8217;s easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Xml</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create a new XML document.</span>
<span style="color: #0600FF;">Dim</span> xmlDoc <span style="color: #FF8000;">As</span> XmlDocument <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> XmlDocument</pre></div></div>

<p>Now we want to start writing some HTML.  So let&#8217;s create our <code>html</code> tag and add some headers.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' Create the html tag.</span>
<span style="color: #0600FF;">Dim</span> xmlRoot <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;html&quot;</span><span style="color: #000000;">&#41;</span>
xmlDoc.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlRoot<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the head tag and append it under the html element.</span>
<span style="color: #0600FF;">Dim</span> xmlHead <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;head&quot;</span><span style="color: #000000;">&#41;</span>
xmlRoot.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlHead<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the title tag, set it's text to &quot;Database Table&quot;</span>
<span style="color: #008080; font-style: italic;">' and append it under the head element.</span>
<span style="color: #0600FF;">Dim</span> xmlTitle <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;title&quot;</span><span style="color: #000000;">&#41;</span>
xmlTitle.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">CreateTextNode</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Database Table&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
xmlHead.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlTitle<span style="color: #000000;">&#41;</span></pre></div></div>

<p>Wow, that&#8217;s a lot of code.  What&#8217;s going on there?  First, let&#8217;s look at the <code>XmlDocument</code> class.  This is important when we&#8217;re generating XML documents.  It&#8217;s what we use to create new elements.  However, even though it creates new elements, it doesn&#8217;t add them to the document.  Thus, we need to use the <code>AppendChild</code> method of the appropriate element to add it to the document.  As you can see above, we created a root element (<code>html</code>) and added that to the XML document (<code>xmlDoc</code>) first.  Then we created a <code>head</code> element, and added that to the root element, not the <code>xmlDoc</code>.  This was done in order to form a hierarchy, which is, in essence, what a XML (and by extension HTML) document is.</p>
<p>Here is what the resulting XML/XHTML document would look like so far:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Database Table<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So far so good, let&#8217;s finish it off.  Here&#8217;s the rest of the code we&#8217;d need to make our simple HTML document:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' Create the body element and append it to the root.</span>
<span style="color: #0600FF;">Dim</span> xmlBody <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;body&quot;</span><span style="color: #000000;">&#41;</span>
xmlRoot.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlBody<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the table and append it.</span>
<span style="color: #0600FF;">Dim</span> xmlTable <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;table&quot;</span><span style="color: #000000;">&#41;</span>
xmlBody.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlTable<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the rows.</span>
<span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> row <span style="color: #FF8000;">As</span> DataRow In dataTable
  <span style="color: #0600FF;">Dim</span> xmlRow <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;tr&quot;</span><span style="color: #000000;">&#41;</span>
  xmlTable.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlRow<span style="color: #000000;">&#41;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">' Create the cells.</span>
  <span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> item <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span> In row
    <span style="color: #0600FF;">Dim</span> content <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
    <span style="color: #0600FF;">If</span> IsDBNull<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span> <span style="color: #FF8000;">Then</span> content <span style="color: #008000;">=</span> <span style="color: #0600FF;">CStr</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> xmlCell <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;td&quot;</span><span style="color: #000000;">&#41;</span>
    xmlCell.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">createTextNode</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    xmlRow.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlCell<span style="color: #000000;">&#41;</span>
  <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">For</span>
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">For</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Write the XML/HTML document.</span>
Response.<span style="color: #0600FF;">Write</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">OuterXml</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>And there you have it: a programmatic way to generate an HTML document.  So why bother?  Here&#8217;s a few reasons:</p>
<p><strong>Safety</strong>.  If you generate your HTML document by concatenating strings, there&#8217;s a chance you might forget to close a tag.  If you generate the document programmatically, you can ensure that all your tags will always be closed.</p>
<p><strong>Maintainability and readability</strong>.  I&#8217;ve had to deal with code where the HTML is generated by concatenation.  It&#8217;s confusing and frustrating.  If you generate your HTML documents programmatically, it&#8217;s a lot easier for a future programmer to follow what&#8217;s going on (especially if you, y&#8217;know, document it).</p>
<p><strong>Reusability and modifiability</strong>.  If you create your HTML document as a flat string, it makes it really hard to modify and reuse elsewhere in your code.  For example, if you wanted to add a tag in the middle of your HTML document using the string concatenation method, you will have to do a lot of painful string manipulations.  With the programmatic approach, it&#8217;s as simple as just calling <code>AppendChild</code> on the appropriate <code>XmlElement</code>.</p>
<p>Using an <code>XmlDocument</code> to represent an XML document just makes sense.  It&#8217;s less crude and more versatile.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

