<?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; java</title>
	<atom:link href="http://cdmckay.org/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Joshua Bloch&#8217;s Builder Pattern in C#</title>
		<link>http://cdmckay.org/blog/2009/07/03/joshua-blochs-builder-pattern-in-csharp/</link>
		<comments>http://cdmckay.org/blog/2009/07/03/joshua-blochs-builder-pattern-in-csharp/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 21:02:49 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[design-patterns]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=610</guid>
		<description><![CDATA[Having spent a lot of time programming in Java over the last two years, I&#8217;ve made heavy use of Joshua Bloch&#8216;s Java Builder pattern (also Effective Java Item&#160;2). Recently, I&#8217;ve started a fairly large project in C# 3.0. As it happens, there came a point where I wanted to use a pattern similar to Bloch&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Having spent a lot of time programming in Java over the last two years, I&#8217;ve made heavy use of <a href="http://en.wikipedia.org/wiki/Joshua_Bloch">Joshua Bloch</a>&#8216;s Java <a href="http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-2689.pdf">Builder pattern</a> (also <a href="http://java.sun.com/docs/books/effective/">Effective Java</a> Item&nbsp;2).</p>
<p>Recently, I&#8217;ve started a fairly large project in C# 3.0.  As it happens, there came a point where I wanted to use a pattern similar to Bloch&#8217;s Builder.  However, as anyone who tries to use this pattern in C# will quickly find out, Bloch&#8217;s Builder doesn&#8217;t translate perfectly to C#.  </p>
<p><span id="more-610"></span></p>
<p>Here is how Bloch&#8217;s Builder looks in C# (with changes made only for syntax):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> servingSize, <span style="color: #6666cc; font-weight: bold;">int</span> servings<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ServingSize <span style="color: #008000;">=</span> servingSize<span style="color: #008000;">;</span>
      Servings <span style="color: #008000;">=</span> servings<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder Calories<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> calories<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> Calories <span style="color: #008000;">=</span> calories<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NutritionFacts Build<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> NutritionFacts<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> NuitritionFacts<span style="color: #008000;">&#40;</span>Builder builder<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    ServingSize <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">ServingSize</span><span style="color: #008000;">;</span>
    Servings <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Servings</span><span style="color: #008000;">;</span>
    Calories <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Calories</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">...</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you try to compile that code, the C# compiler will complain, saying that it does not have permission to access the private members of the Builder class.</p>
<p>Then how come <a href="http://stackoverflow.com/questions/1080383/why-doesnt-blochs-builder-pattern-work-in-c">it worked in Java</a>?  The answer is that Java has a special rule that allows Java outer classes to access the private members of their inner classes.  In C#, this rule does not exist, and thus C# outer classes cannot access the private members of C# inner classes.</p>
<p>Since Bloch&#8217;s Builder pattern doesn&#8217;t work in C#, the natural next question is: <a href="http://stackoverflow.com/questions/313729/how-to-implement-and-extend-joshuas-builder-pattern-in-net">how do we make it work</a>?</p>
<h3>Approach 1: Make Builder Properties Public</h3>
<p>The C# compiler tells us that it can&#8217;t compile because it doesn&#8217;t have permission to access private members of the nested Builder class.  Thus, the most obvious solution to this is to make them public:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span> 
  <span style="color: #008000;">...</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>      
    <span style="color: #008000;">...</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This allows the code to compile, and we can use the Builder just as we used it in Java.</p>
<p>However there is one niggling concern: the C# Builder exposes getters in the Builder object.  In the Java Builder, when we were constructing an object, we were only able to use methods that contributed to building the object.  That is, the Java Builder doesn&#8217;t expose any getters.  However, in the C# version, our API is polluted by unneeded getters.  Let&#8217;s see if we can do better.</p>
<h3>Approach 2: Build the object in the Builder</h3>
<p>As we&#8217;ve already seen, C# does not permit outer classes to access the private members of inner classes.  However, C# <em>does</em> let us access outer classes from inner classes.  Since that&#8217;s the case, why don&#8217;t we invert the building procedure.  That is, let&#8217;s build the object in the Builder, instead of in the class being built:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>    
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> servingSize, <span style="color: #6666cc; font-weight: bold;">int</span> servings<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ServingSize <span style="color: #008000;">=</span> servingSize<span style="color: #008000;">;</span>
      Servings <span style="color: #008000;">=</span> servings<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder Calories<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> calories<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> Calories <span style="color: #008000;">=</span> calories<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>    
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NutritionFacts Build<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> NutritionFacts
      <span style="color: #008000;">&#123;</span>
        ServingSize <span style="color: #008000;">=</span> ServingSize,
        Servings <span style="color: #008000;">=</span> Servings,
        Calories <span style="color: #008000;">=</span> Calories,
        <span style="color: #008000;">...</span>       
      <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> NuitritionFacts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Intentionally empty.</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>That, to me, is the closest you can get to the Java pattern.  From an API consumer&#8217;s point of view, it&#8217;ll look identical to a Java Builder.</p>
<h3>Alternative Form</h3>
<p>There is another alternative to the two C# Builders mentioned earlier in this article.  As it stands, Bloch&#8217;s Builder can be used as a sort of prototype factory.  You can configure the builder and then pump out instances of the class that have that configuration.  However, if you&#8217;re using the Builder to make a single, immutable instance, a more efficient way would be to avoid copying the data first to the Builder and then to the new instance.</p>
<p>Here&#8217;s a <a href="http://stackoverflow.com/questions/313729/how-to-implement-and-extend-joshuas-builder-pattern-in-net/313747#313747">version of the Builder</a> (by Jon Skeet) that can be used only once, but avoids unnecessary data copying.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>       
    <span style="color: #0600FF; font-weight: bold;">private</span> NutritionFacts _facts <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NutritionFacts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> servingSize, <span style="color: #6666cc; font-weight: bold;">int</span> servings<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _facts<span style="color: #008000;">.</span><span style="color: #0000FF;">ServingSize</span> <span style="color: #008000;">=</span> servingSize<span style="color: #008000;">;</span>
      _facts<span style="color: #008000;">.</span><span style="color: #0000FF;">Servings</span> <span style="color: #008000;">=</span> servings<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder Calories<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> calories<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> _facts<span style="color: #008000;">.</span><span style="color: #0000FF;">Calories</span> <span style="color: #008000;">=</span> calories<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>    
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NutritionFacts Build<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      var ret <span style="color: #008000;">=</span> facts<span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// Invalidates builder to maintain mutability.</span>
      facts <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">return</span> ret<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> NuitritionFacts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Intentionally empty.</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Some of the advantages of this pattern are:</p>
<ul>
<li>No data copying is required at build time. In other words, while you&#8217;re setting the properties, you&#8217;re doing so on the real object &#8211; you just can&#8217;t see it yet. This is similar to what StringBuilder does.</li>
<li>The builder becomes invalid after calling Build() to guarantee immutability. This unfortunately means it can&#8217;t be used as a sort of &#8220;prototype&#8221; in the way that Bloch&#8217;s version can.</li>
</ul>
<h3>Conclusion</h3>
<p>In this article we&#8217;ve looked at 3 different working C# implementations of Bloch&#8217;s Builder pattern.  The first approach was a quick and dirty fix that got the job done.  The second approach applied a bit more finesse and managed to keep the same API as the Java Builder.  The final approach was more of a variation on the second approach, offering a more efficient implementation of the pattern if you&#8217;re not using the Builder as an object prototype factory.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/03/joshua-blochs-builder-pattern-in-csharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Consuming a J2EE Web Service with PHP SOAP</title>
		<link>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/</link>
		<comments>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 22:39:59 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[nusoap]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=303</guid>
		<description><![CDATA[I recently had to do some work for a company which involved using PHP to consume a J2EE-based WSDL web service. The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected. What is WSDL? Before we start, let&#8217;s talk a bit about what WSDL [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to do some work for a company which involved using <a href="http://www.php.net">PHP</a> to consume a <a href="http://java.sun.com/javaee/">J2EE</a>-based <a href="http://www.w3.org/TR/wsdl">WSDL</a> web service.  The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected.</p>
<p><span id="more-303"></span></p>
<h3>What is WSDL?</h3>
<p>Before we start, let&#8217;s talk a bit about what <a href="http://en.wikipedia.org/wiki/Web_Services_Description_Language">WSDL</a> is.  <em>(Warning: I am very new to WSDL, SOAP and web services in general, so feel free to correct me if I get any of these details wrong).</em></p>
<p>WSDL stands for Web Service Description Language.  As the name suggests, it is an XML language used for describing web services.  By describe, I mean a WSDL file contains information like the functions that a client can call, and data types it uses, and what exceptions it might throw.  This is useful for clients that might contact the web service.  Exactly why that information is useful will become evident in examples below.</p>
<h3>What is SOAP?</h3>
<p>At one point, a long time ago, in a web far far away, <a href="http://en.wikipedia.org/wiki/SOAP_(protocol)">SOAP</a> stood for Simple Object Access Protocol.  Now it stands for nothing.  SOAP is a protocol for exchanging structured information on the web.  Like WSDL, SOAP uses XML.  In fact, SOAP is a protocol often used to exchange WSDL information, although one can also use straight HTTP to do it as well.</p>
<p>All we really need to know (right now) is that a SOAP library permits us to communicate with a WSDL web service that has SOAP bindings (which would be almost any we would encounter in the wild).</p>
<h3>Which library/module to use?</h3>
<p>In order to determine which library to use, I did some googling.  What I found were two options: the <a href="http://sourceforge.net/projects/nusoap/">NuSOAP</a> library and the <a href="http://ca.php.net/manual/en/book.soap.php">PHP SOAP</a> module.</p>
<p>So which one should we use? I initially leaned towards NuSOAP due to the fact that it was an external library, and thus would probably work with almost any PHP installation.  Unfortunately, after perusing the NuSOAP Sourceforge website, I noticed the library hadn&#8217;t been updated since 2005 (a bad sign).  In fact, after further reading, I discovered that, as of PHP 5, the NuSOAP namespace also conflicted with the PHP SOAP module namespace (and hadn&#8217;t yet been fixed).  As a result, I decided to go with the PHP SOAP module.</p>
<h3>The Code</h3>
<p>The first thing anyone needs to do when consuming a WSDL service with PHP is to create a new client object.  By creating a client object, PHP basically reads the server&#8217;s WSDL file and determines which functions are available to us.  A SoapClient is created using the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// The path to the WSDL web service.</span>
  <span style="color: #000088;">$wsdl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://path/to/webservice.wsdl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The connection options.</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'features'</span> <span style="color: #339933;">=&gt;</span> 
    SOAP_USE_XSI_ARRAY_TYPE <span style="color: #339933;">+</span> SOAP_SINGLE_ELEMENT_ARRAYS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Create a new SoapClient.</span>
  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;SoapClient creation successful&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what&#8217;s going on at line 7?  Those two features are basically used to make the SoapClient act as we&#8217;d expect.  For example, if you don&#8217;t include the SOAP_USE_XSI_ARRAY_TYPE feature, then you&#8217;ll find you get a lot of casting exceptions when dealing with a Java-based webservice that uses any complex data structures.  If you don&#8217;t include the SOAP_USE_XSI_ARRAY_TYPE feature, then PHP will turn 1-element arrays return types into objects, which is not what you want 90% of the time and often gives confusing results.</p>
<p>Now let&#8217;s actually consume a web service.  Let&#8217;s pretend we&#8217;re using the J2EE service provided by this <a href="http://java.sun.com/developer/technicalArticles/J2EE/j2ee_ws/">article</a>.  Here are the contents of the WSDL file from that article:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definitions</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyFirstService&quot;</span> <span style="color: #000066;">targetNamespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span> <span style="color: #000066;">xmlns:tns</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/wsdl/&quot;</span> <span style="color: #000066;">xmlns:xsd</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:soap</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;types</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace_add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;int_1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;int_2&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace_addResponse&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;result&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;portType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">parameterOrder</span>=<span style="color: #ff0000;">&quot;int_1 int_2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:MathFace_add&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:MathFace_addResponse&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/portType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFaceBinding&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;tns:MathFace&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:binding</span> <span style="color: #000066;">transport</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/http&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;rpc&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:body</span> <span style="color: #000066;">encodingStyle</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span> </span>
<span style="color: #009900;">		    <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;encoded&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:body</span> <span style="color: #000066;">encodingStyle</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span> </span>
<span style="color: #009900;">		    <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;encoded&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/output<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyFirstService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;port</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFacePort&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;tns:MathFaceBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:address</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;http://localhost:8080/math-service/math&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/port<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>What can we tell about the web service by looking at this file?  Well the first thing you should notice is lines 12-14.  In those lines, the web service defines an operation (&#8220;add&#8221;) that takes two arguments (&#8220;int_1 int_2&#8243;).  As a result, we know that the web service exposes an add operation that takes two integer arguments.  Let&#8217;s use that knowledge to invoke the add operation in PHP using the SOAP module:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// The path to the WSDL web service.</span>
  <span style="color: #000088;">$wsdl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://localhost:8080/math-service/math?wsdl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The connection options.</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'features'</span> <span style="color: #339933;">=&gt;</span> 
    SOAP_USE_XSI_ARRAY_TYPE <span style="color: #339933;">+</span> SOAP_SINGLE_ELEMENT_ARRAYS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Create a new SoapClient.</span>
  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;SoapClient creation successful&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Call the add operation.</span>
  <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// This should print 4.</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$response</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>How&#8217;d the PHP object $client know to have an add method?  It simply read the WSDL file the same way we did, and realized that the web service exposed that operation.</p>
<p>As you can see, consuming a J2EE web service from a PHP client is not too complicated.  Once you understand the rudiments of a WSDL file and the PHP SOAP module, calling remote functions becomes a snap.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/feed/</wfw:commentRss>
		<slash:comments>5</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[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<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>
	</channel>
</rss>

