<?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>marthijn. &#187; Development</title>
	<atom:link href="http://www.marthijnvandenheuvel.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marthijnvandenheuvel.com</link>
	<description></description>
	<lastBuildDate>Fri, 30 Dec 2011 13:03:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to send an e-mail from your Android app</title>
		<link>http://www.marthijnvandenheuvel.com/2011/05/01/how-to-send-an-e-mail-from-your-android-app/</link>
		<comments>http://www.marthijnvandenheuvel.com/2011/05/01/how-to-send-an-e-mail-from-your-android-app/#comments</comments>
		<pubDate>Sun, 01 May 2011 09:43:19 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[E-Mail]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=334</guid>
		<description><![CDATA[I&#8217;m developing an Android application where the user must be able to send a file stored on the SD Card to a specific e-mail address. By using Android&#8217;s Intent class this is very easy. The following code snippet will bring up an e-mail application choose dialog. String filename = &#34;file://&#34; + Environment.getExternalStorageDirectory&#40;&#41; + &#34;/file.txt&#34;; String [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m developing an Android application where the user must be able to send a file stored on the SD Card to a specific e-mail address. By using Android&#8217;s <a href="http://developer.android.com/reference/android/content/Intent.html" target="_blank">Intent</a> class this is very easy. The following code snippet will bring up an e-mail application choose dialog.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> filename <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;file://&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">Environment</span>.<span style="color: #006633;">getExternalStorageDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/file.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> email<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;foo@bar.com&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Intent sendIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_SEND</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sendIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">EXTRA_SUBJECT</span>, <span style="color: #0000ff;">&quot;My file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sendIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">EXTRA_EMAIL</span>, email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sendIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">EXTRA_STREAM</span>, Uri.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>filename<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sendIntent.<span style="color: #006633;">setType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
startActivity<span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">createChooser</span><span style="color: #009900;">&#40;</span>sendIntent, <span style="color: #0000ff;">&quot;EMail file&quot;</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://www.marthijnvandenheuvel.com/2011/05/01/how-to-send-an-e-mail-from-your-android-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fix .NET 4 SocketPermission for MySQL driver in Medium Trust</title>
		<link>http://www.marthijnvandenheuvel.com/2011/04/14/fix-net-4-socketpermission-for-mysql-driver-in-medium-trust/</link>
		<comments>http://www.marthijnvandenheuvel.com/2011/04/14/fix-net-4-socketpermission-for-mysql-driver-in-medium-trust/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 18:57:38 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SocketPermission]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=328</guid>
		<description><![CDATA[Most .NET 4 shared hosting providers offering their customers a medium trust environment. This is not a problem for most web applications unless the applications uses MySQL in combination with the .NET MySQL driver (MySQL.Data.dll). The MySQL driver connects to the MySQL database using a socket. However, sockets are not allowed in medium trust. The [...]]]></description>
			<content:encoded><![CDATA[<p>Most .NET 4 shared hosting providers offering their customers a medium trust environment. This is not a problem for most web applications unless the applications uses MySQL in combination with the .NET MySQL driver (<a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySQL.Data.dll</a>). The MySQL driver connects to the MySQL database using a socket. However, sockets are not allowed in medium trust. The application will throw a SecurityException with the following message:</p>
<pre><code>Request for the permission of type 'System.Net.SocketPermission,  System, Version=4.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089' failed.</code></pre>
<p><span id="more-328"></span><br />
In order to fix this there are three solutions:</p>
<p>1. Instead of MySQL, use Microsoft SQL server.</p>
<p>2. Move to a hosting provider that supports a full trust environment or get your own server.</p>
<p>3. Ask your hosting provider to update the medium trust policy with SocketPermission. The steps will be explained here:</p>
<p>Open the following file in a text editor:</p>
<pre>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web_mediumtrust.config</pre>
<p>Or when you are running 64 bit:</p>
<pre>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web_mediumtrust.config</pre>
<p>Inside the SecurityClasses tag, add the following line:</p>
<pre><code>&lt;SecurityClass Name="SocketPermission" Description="System.Net.SocketPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/&gt;</code></pre>
<p>Scroll down and look for the following PermissionSet:</p>
<pre>&lt;PermissionSet version="1" Name="ASP.Net"&gt;</pre>
<p>Add the following inside this PermissionSet:</p>
<pre><code>&lt;IPermission class="SocketPermission" version="1" Unrestricted="true" /&gt;
</code></pre>
<p>That&#8217;s all, a restart of IIS is, as far as I know, not necessary.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2011/04/14/fix-net-4-socketpermission-for-mysql-driver-in-medium-trust/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Possible solution for NHibernate many-to-many criteria</title>
		<link>http://www.marthijnvandenheuvel.com/2011/01/07/possible-solution-for-nhibernate-many-to-many-criteria/</link>
		<comments>http://www.marthijnvandenheuvel.com/2011/01/07/possible-solution-for-nhibernate-many-to-many-criteria/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 10:44:12 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=299</guid>
		<description><![CDATA[How to query an NHibernate many-to-many relationship.]]></description>
			<content:encoded><![CDATA[<p>In one of my .NET projects I&#8217;m using the <a href="http://nhforge.org/" target="_blank">NHibernate</a> library for object-relational mapping. I&#8217;m mainly using the ICriteria interface to fetch data from the database. Unfortunately I ran into a function that got really complicated; how to query a many-to-many relationship. For example, I have a table containing posts and a table containing tags. The post datamodel contains a set with tags so in my mapping it&#8217;s a many-to-many relationship. I want my query to return all posts tagged with one or more specific tags. On <a href="http://forum.castleproject.org/viewtopic.php?p=15157" target="_blank">this forum</a> I found a solution. I&#8217;m not sure if this is the perfect solution, so feel free to suggest a better one.<br />
<span id="more-299"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> IList<span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span> GetPostsByTags<span style="color: #008000;">&#40;</span>IList<span style="color: #008000;">&lt;</span>Tag<span style="color: #008000;">&gt;</span> tags<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
 ICriteria posts <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Session</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CreateCriteria</span><span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Tag t <span style="color: #0600FF; font-weight: bold;">in</span> tags<span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  posts<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>Subqueries<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>DetachedCriteria<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">For</span><span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;p2&quot;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">.</span><span style="color: #0000FF;">SetProjection</span><span style="color: #008000;">&#40;</span>Projections<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">.</span><span style="color: #0000FF;">CreateAlias</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;p2.Tags&quot;</span>, <span style="color: #666666;">&quot;t&quot;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>Restrictions<span style="color: #008000;">.</span><span style="color: #0000FF;">Eq</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;t.Id&quot;</span>, t<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>Restrictions<span style="color: #008000;">.</span><span style="color: #0000FF;">EqProperty</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;p2.Id&quot;</span>, <span style="color: #666666;">&quot;this.Id&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
 posts<span style="color: #008000;">.</span><span style="color: #0000FF;">SetResultTransformer</span><span style="color: #008000;">&#40;</span>NHibernate<span style="color: #008000;">.</span><span style="color: #0000FF;">Transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Transformers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DistinctRootEntity</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #0600FF; font-weight: bold;">return</span> posts<span style="color: #008000;">.</span><span style="color: #0000FF;">List</span><span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2011/01/07/possible-solution-for-nhibernate-many-to-many-criteria/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using log4net to show NHibernate SQL in Visual Studio</title>
		<link>http://www.marthijnvandenheuvel.com/2010/12/02/using-log4net-to-show-nhibernate-sql-in-visual-studio/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/12/02/using-log4net-to-show-nhibernate-sql-in-visual-studio/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 10:15:28 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=292</guid>
		<description><![CDATA[Configure log4net in order to show NHibernate SQL queries in Visual Studio's console.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently developing a web application in <a href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC</a> and <a href="http://nhforge.org/" target="_blank">NHibernate</a>. Since the application was very slow at some points I wanted to know which SQL queries NHibernate was executing. Since the <a href="http://logging.apache.org/log4net/index.html" target="_blank">log4net</a> library was already included in the application I searched for a solution to output the SQL queries to the console in Visual Studio 2010 using log4net. I found the solution on <a href="http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate.aspx" target="_blank">this wiki</a> and <a href="http://www.davesquared.net/2008/01/viewing-sql-generated-by-nhibernate.html" target="_blank">this blog post</a>.</p>
<p><span id="more-292"></span>First thing you need to do is add a log4net section in your web.config if you don&#8217;t have one yet.</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;configSections<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 ...
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;log4net&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configSections<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then add the log4net section, and configure an appender and logger.</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;log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;DebugSQL&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Appender.TraceAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Layout.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;conversionPattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;%date [%thread] %-5level %logger [%property{NDC}] - %message%newline&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;logger</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NHibernate.SQL&quot;</span> <span style="color: #000066;">additivity</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;DEBUG&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender-ref</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;DebugSQL&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/logger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That&#8217;s all. Don&#8217;t forget to configure log4net in the Global.asax.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
 log4net<span style="color: #008000;">.</span><span style="color: #0000FF;">Config</span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlConfigurator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Configure</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/12/02/using-log4net-to-show-nhibernate-sql-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CodeIgniter simple CRUD improved</title>
		<link>http://www.marthijnvandenheuvel.com/2010/06/27/codeigniter-simple-crud-improved/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/06/27/codeigniter-simple-crud-improved/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 12:38:56 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[CRUD]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Table]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=208</guid>
		<description><![CDATA[Add sorting in a simple CRUD with CodeIgniter.]]></description>
			<content:encoded><![CDATA[<p>Currently I&#8217;m developing a web application in PHP using the <a href="http://codeigniter.com/" target="_blank">CodeIgniter</a> framework. Since I needed some admin functionality such as create, read, update and delete records (CRUD) I searched for CodeIgniter libraries. I came across <a href="http://henrihnr.wordpress.com/2009/04/26/simple-crud-application/" target="_blank">this post</a> by <a href="http://henrihnr.wordpress.com/" target="_blank">Henri</a>. His solution is very simple and easy to understand and implement. In this post I&#8217;ll describe how to improve his solution so it&#8217;s possible to sort columns. I start with the files Henri provided in his post.</p>
<p>In order to enable sorting I first modify the get_paged_list function of the PersonModel class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_paged_list<span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_column</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_column</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order_by</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order_by</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_column</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tbl_person</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-208"></span></p>
<p>The index function of the person controller must be modified as well. I changed the following parts:</p>
<ul>
<li>Function parameters</li>
<li>Get ordering from URI</li>
<li>Call to model</li>
<li>Table header</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_column</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// checks</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_column</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$order_column</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$order_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'asc'</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//TODO: check for valid column</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// load data</span>
	<span style="color: #000088;">$persons</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">personModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_paged_list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_column</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order_type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// generate pagination</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pagination'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'base_url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> site_url<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/index/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total_rows'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">personModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count_all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'per_page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uri_segment'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pagination</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagination'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pagination</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create_links</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// generate table data</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_empty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;nbsp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$new_order</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'asc'</span> ? <span style="color: #0000ff;">'desc'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_heading</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'No'</span><span style="color: #339933;">,</span>
		anchor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/index/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$offset</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/name/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$new_order</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		anchor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/index/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$offset</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/gender/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$new_order</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Gender'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		anchor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/index/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$offset</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/dob/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$new_order</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Date of Birth (dd-mm-yyyy)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Actions'</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$persons</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$person</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_row</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gender</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'M'</span>? <span style="color: #0000ff;">'Male'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'Female'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d-m-Y'</span><span style="color: #339933;">,</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dob</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			anchor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/view/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span>
			anchor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/update/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'update'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'update'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span>
			anchor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'person/delete/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'onclick'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;return confirm('Are you sure want to delete this person?')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'table'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// load view</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'personList'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><i>Updated June 29th 2010 thanks to Stephen.</i><br/><br />
The only problem now is pagination doesn&#8217;t work any more. As far as I know there is no solution to pass the ordering parameter to CodeIgniter&#8217;s pagination class. I think the best solution is to implement your own pagination class. Maybe I&#8217;ll discuss that in another post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/06/27/codeigniter-simple-crud-improved/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NUnit in Visual Studio 2010</title>
		<link>http://www.marthijnvandenheuvel.com/2010/06/09/using-nunit-in-visual-studio-2010/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/06/09/using-nunit-in-visual-studio-2010/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 12:13:10 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=196</guid>
		<description><![CDATA[How to use NUnit in Visual Studio 2010.]]></description>
			<content:encoded><![CDATA[<p>Like in older versions of <a href="http://www.microsoft.com/visualstudio" target="_blank">Visual Studio</a> it is possible to use <a href="http://www.nunit.org" target="_blank">NUnit</a> as an external tool in the new 2010 version. By creating a toolbar as well it is very easy to run your test suite. The usual way to add an external command is by clicking the menu Tools -&gt; External Tools. However, by default this menu item is not visible. To enable this menu item go to Tools -&gt; Settings -&gt; Expert Settings.</p>
<p><a href="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010.png" rel="lightbox[196]"><img class="aligncenter size-full wp-image-197" title="Visual Studio 2010" src="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010.png" alt="Visual Studio 2010" width="214" height="32" /></a></p>
<p><span id="more-196"></span></p>
<p>The first step is download and install the latest version of NUnit since older versions are not compatible with .NET 4. NUnit version 2.5.5 can be downloaded <a href="http://www.nunit.org/?p=download" target="_blank">here</a>.</p>
<p>Now go to Tools -&gt; External Tools:</p>
<p><a href="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010_exttools.png" rel="lightbox[196]"><img class="aligncenter size-full wp-image-198" title="Visual Studio 2010 External Tools" src="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010_exttools.png" alt="Visual Studio 2010 External Tools" width="493" height="481" /></a></p>
<p>I think this screen shot explains itself very well. Now its possible to add this external command to a tool bar. To do so go to View -&gt; Toolbars -&gt; Customize:</p>
<p><a href="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010_customize.png" rel="lightbox[196]"><img class="aligncenter size-full wp-image-200" title="Visual Studio 2010 Customize Toolbars" src="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010_customize.png" alt="Visual Studio 2010 Customize Toolbars" width="465" height="509" /></a></p>
<p>Press the New button and enter a name. Now open the Commands tab and add the external command (usually External Command 1 when you don&#8217;t have any other external commands) to the toolbar:</p>
<p style="text-align: center;"><a href="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010_customize2.png" rel="lightbox[196]"><img class="aligncenter size-full wp-image-202" title="Visual Studio 2010 Commands" src="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/06/vs2010_customize2.png" alt="Visual Studio 2010 Toolbars" width="801" height="529" /></a></p>
<p style="text-align: left;">When clicking the NUnit button in the new toolbar the NUnit GUI starts and loads your test suite.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/06/09/using-nunit-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Unit testing in Watir</title>
		<link>http://www.marthijnvandenheuvel.com/2010/04/14/unit-testing-in-watir/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/04/14/unit-testing-in-watir/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 15:17:12 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Watir]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=149</guid>
		<description><![CDATA[Write a simple unit test in Watir]]></description>
			<content:encoded><![CDATA[<p><a href="http://watir.com/" target="_blank">Watir</a> is an open-source Ruby library for automating web browsers. Combined with unit testing for Ruby it is a really powerful tool to test websites and webapplications. In this post I explain how to write a test for a simple website.</p>
<div id="attachment_150" class="wp-caption aligncenter" style="width: 120px"><a href="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/04/watir.png" rel="lightbox[149]"><img class="size-full wp-image-150" title="Watir" src="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/04/watir.png" alt="Watir" width="110" height="103" /></a><p class="wp-caption-text">Watir</p></div>
<p><span id="more-149"></span></p>
<p>First download and install <a href="http://www.ruby-lang.org/en/downloads/" target="_blank">Ruby</a>. If you are familiar with <a href="http://www.eclipse.org/" target="_blank">Eclipse</a>, <a href="http://www.aptana.com/" target="_blank">Aptana</a> is a nice web development plugin (see <a href="http://osdir.com/ml/RubyonRailsTalk/2009-08/msg02136.html" target="_blank">this simple installation tutorial</a>). If you don&#8217;t know how to use Ruby, <a href="http://www.basvandijk.eu/" target="_blank">Bas</a> has a <a href="http://www.basvandijk.eu/2010/03/28/getting-started-with-ruby/" target="_blank">nice article</a> about getting started with Ruby.</p>
<p>The second step is <a href="http://watir.com/installation/" target="_blank">install Watir</a>. You can try <a href="http://watir.com/examples/" target="_blank">these examples</a> to see what Watir does.</p>
<h2>Testing a simple website</h2>
<p>For one of my school projects I had to write a simple test for one of the function of this <a href="http://www.calcenstein.com/" target="_blank">online calculator</a>. In this tutorial we will test the <a href="http://www.calcenstein.com/calc/0416.php" target="_blank">GCF and LCM function</a>. Start with creating a new Ruby script containing the right requirements and a test case class:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'watir'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test/unit'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> TC_LCM_GCD <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></span>
 <span style="color:#9966CC; font-weight:bold;">def</span> test_lcm_gcd
  <span style="color:#008000; font-style:italic;"># ...</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In order to let Watir start a new browser add the following to the test_lcm_gcd function:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Watir::Browser</span>.<span style="color:#9900CC;">default</span> = <span style="color:#996600;">&quot;ie&quot;</span>
b = <span style="color:#6666ff; font-weight:bold;">Watir::Browser</span>.<span style="color:#9900CC;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://www.calcenstein.com/calc/0416.php&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># GCF and LCM</span></pre></div></div>

<p>If you want to use Firefox for testing, change &#8220;ie&#8221; to &#8220;firefox&#8221; and don&#8217;t forget to install the <a href="http://watir.com/installation/" target="_blank">plugin</a>. The second line starts a new browser window.</p>
<p>Our goal is to let Watir insert two values (the &#8216;First Value&#8217; and &#8216;Second Value&#8217; input field), and then press the &#8216;Solve&#8217; button. In order to do so we need to know the name or id (or value in some cases) of the input fields and the &#8216;Solve&#8217; button. Instead of looking at the source I can recommend to use the <a href="https://addons.mozilla.org/nl/firefox/addon/1843" target="_blank">Firebug</a> add-on for Firefox, and use the inspect function. The &#8216;First Value&#8217; input is named &#8216;a&#8217;, and the &#8216;Second Value&#8217; input is named &#8216;b&#8217;. We want to set &#8216;a&#8217; to 30 and &#8216;b&#8217; to 45:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">set</span> <span style="color:#996600;">&quot;30&quot;</span>
b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;b&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">set</span> <span style="color:#996600;">&quot;45&quot;</span></pre></div></div>

<p>When these values are set, Watir must press the &#8216;Solve&#8217; button. It appears this button doesn&#8217;t have a name or id, so get it by value and execute the click action:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">b.<span style="color:#9900CC;">button</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:value</span>, <span style="color:#996600;">&quot;Solve&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">click</span></pre></div></div>

<p>Now the click action is executed and the result is printed in the other two input fields (named &#8216;lcmA&#8217; and &#8216;gcdA&#8217;). Like in all other test cases, the result must be verified. The correct result is LCM = 90 and GCF = 15, so use assertions to check the values:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">assert<span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;lcmA&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">value</span> == <span style="color:#996600;">&quot;90&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
assert<span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;gcdA&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">value</span> == <span style="color:#996600;">&quot;15&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This simple test case is now complete. The test output should be something like this:</p>
<pre>Loaded suite My_test_suite
Started
.
Finished in 5.579 seconds.

1 tests, 2 assertions, 0 failures, 0 errors</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/04/14/unit-testing-in-watir/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Type conversion in C#</title>
		<link>http://www.marthijnvandenheuvel.com/2010/03/30/type-conversion-in-csharp/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/03/30/type-conversion-in-csharp/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 11:17:17 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Type conversion]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=122</guid>
		<description><![CDATA[Type conversion in C#]]></description>
			<content:encoded><![CDATA[<p>Type conversion (or typecasting) is the technique to change an entity from one data type into another. In general there are two types of type conversion; implicit and explicit conversion. This post tells about type conversion in C#.NET and the performance issues.</p>
<h2>Implicit conversion</h2>
<p>Implicit conversion (or coercion) is automatic type conversion by the compiler. In C# it does not require a special syntax because the type conversion is safe and no data will be lost. In the following example the value of an integer is stored in a long. This is safe because an integer is 4 bytes (on a 32 bit computer), and a long is 8 bytes. So every value that fits in an integer fits in a long. For a complete conversion table see <a href="http://msdn.microsoft.com/en-us/library/y5b434w4.aspx" target="_blank">this page</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">12345678</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">long</span> l <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span></pre></div></div>

<p>It is also possible to cast a derived class to a base class without using a special syntax.<br />
<span id="more-122"></span></p>
<h2>Explicit conversion</h2>
<p>Instead of implicit conversion explicit conversion (or casting) is not done by the compiler. It is explicitly defined in the application (i.e. it requires a cast operator). There is a possible risk of losing data when the source and destination have a different size or when the source is converted to a base class. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">double</span> d <span style="color: #008000;">=</span> <span style="color: #FF0000;">10.345</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>d<span style="color: #008000;">;</span></pre></div></div>

<p>This example will not compile without the explicit cast operator since a double fits not in an integer. Also the value will lose its precision since an integer does not store decimal values. On <a href="http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx" target="_blank">this page</a> you can find an explicit number conversion table.</p>
<h2>User-defined conversion</h2>
<p>In C# it is possible to create your own methods for implicit and explicit conversion. For more information see <a href="http://msdn.microsoft.com/en-us/library/09479473.aspx" target="_blank">this article</a>.</p>
<h2>Helper functions</h2>
<p>The .NET framework has various helper functions for conversion. For example <a href="http://msdn.microsoft.com/en-us/library/system.convert.aspx" target="_blank">System.Convert</a> and the Parse method of various types (e.g. <a href="http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx" target="_blank">Int32.Parse</a>).</p>
<h2>Boxing and unboxing</h2>
<p>C# provides a unified type system. All types are derived from the type &#8220;object&#8221;. It is possible to convert for example an integer to the type object (boxing) and back (unboxing). The <a href="http://msdn.microsoft.com/en-us/library/scekt9xw.aspx" target="_blank">is keyword</a> checks if an object is compatible with a specific type. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
 <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span> o <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// boxing</span>
  <span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span> o<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// unboxing</span>
  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>j<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>o <span style="color: #008000;">is</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;o is an int&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>&#8220;Cast&#8221; versus &#8220;as&#8221;</h2>
<p>The <a href="http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx" target="_blank">as operator</a> is often used for type conversion, and it works more or less the same as the cast prefix:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ClassA <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  ClassA a <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ClassA<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span> o <span style="color: #008000;">=</span> a<span style="color: #008000;">;</span>
&nbsp;
  ClassA a2 <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ClassA<span style="color: #008000;">&#41;</span>o<span style="color: #008000;">;</span>
  ClassA a3 <span style="color: #008000;">=</span> o <span style="color: #0600FF; font-weight: bold;">as</span> ClassA<span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The difference between these two types of conversion is that the first will throw an exception and the second will return null when the conversion is incorrect:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ClassA <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span> o <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">try</span>
  <span style="color: #008000;">&#123;</span>
   ClassA a2 <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ClassA<span style="color: #008000;">&#41;</span>o<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>InvalidCastException ex<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ClassA a3 <span style="color: #008000;">=</span> o <span style="color: #0600FF; font-weight: bold;">as</span> ClassA<span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>a3 <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Incorrect conversion&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>However, this is not the only difference (see <a href="http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx" target="_blank">this post</a> by Eric Lippert). When you have a user-defined conversion from class A to B, the cast operator (B)A will run the user-defined conversion while the &#8220;as&#8221; operator will not. The &#8220;as&#8221; operator will only consider reference, boxing and unboxing conversions. Like Eric says:</p>
<ul>
<li>As: &#8220;I don&#8217;t know if this conversion is legal or not; we&#8217;re going to give it a  try and see how it goes.&#8221;</li>
<li>Cast: &#8220;I am certain that this conversion is legal and I am willing to take a  runtime exception if I&#8217;m wrong.&#8221;</li>
<li>As: &#8220;I want to know what this object *really is*, not what it is convertible  to by some representation-changing specially-defined conversion rule.&#8221;</li>
<li>Cast: &#8220;Convert this thing using whatever crazy mechanism you need to do to make  it work.&#8221;</li>
</ul>
<h3>Performance</h3>
<p><a href="http://www.codeproject.com/KB/cs/csharpcasts.aspx" target="_blank">This article</a> by Emilio Guijarro gives a good explanation about performance of the cast and &#8220;as&#8221; operator. He concludes the &#8220;as&#8221; operator is about 5 times faster than the cast operator. The performance is only going to matter in the million number of casts, and then the performance problem is probably why you need so many cast operations.</p>
<h3>Why (not) to use &#8220;as&#8221;</h3>
<p>As said before the &#8220;as&#8221; operator returns null when the conversion is incorrect. When using this piece of code you can not check if the original object was null:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ClassA <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  ClassA a <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span> o <span style="color: #008000;">=</span> a<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">try</span>
  <span style="color: #008000;">&#123;</span>
   ClassA a2 <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ClassA<span style="color: #008000;">&#41;</span>o<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>InvalidCastException ex<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ClassA a3 <span style="color: #008000;">=</span> o <span style="color: #0600FF; font-weight: bold;">as</span> ClassA<span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>a3 <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Incorrect conversion&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This program will write &#8220;Incorrect conversion&#8221; while the cast conversion didn&#8217;t throw an exception. Also the &#8220;is&#8221; operator does not help in this case since it returns false when the object is null. A correct pattern could be:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>o <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
 Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;object is null&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">else</span>
<span style="color: #008000;">&#123;</span>
 ClassA a3 <span style="color: #008000;">=</span> o <span style="color: #0600FF; font-weight: bold;">as</span> ClassA<span style="color: #008000;">;</span>
 <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>a3 <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Incorrect conversion&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>References</h2>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/ms173105.aspx" target="_blank">Casting and type conversion &#8211; MSDN</a></li>
<li><a href="http://www.c-sharpcorner.com/UploadFile/ggaganesh/BoxNUnBox11082005073720AM/BoxNUnBox.aspx" target="_blank">Boxing and unboxing in C# &#8211; C-Sharpcorner</a></li>
<li><a href="http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx" target="_blank">Difference between &#8220;as&#8221; and &#8220;cast&#8221; operators &#8211; Eric Lippert</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/03/30/type-conversion-in-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Rhino Mocks to mock a void function</title>
		<link>http://www.marthijnvandenheuvel.com/2010/03/15/using-rhino-mocks-to-mock-a-void-function/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/03/15/using-rhino-mocks-to-mock-a-void-function/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 23:01:57 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Rhino Mocks]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=95</guid>
		<description><![CDATA[Using NUnit and Rhino Mocks to create an expectation for a void function.]]></description>
			<content:encoded><![CDATA[<p>In some of my web applications I use <a href="http://www.ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino Mocks</a> to create mock objects. Usually the code has a record section where the expected calls are recorded, and a playback section where the testing is done. In the following code snippet the DoSomething() function calls a void function which is defined in the interface of _myMock. The problem is this void function is not expected and the <a href="http://www.nunit.org/index.php" target="_blank">NUnit</a> test run will fail:</p>
<pre>Rhino.Mocks.Exceptions.ExpectationViolationException : IMyInterface.MyVoidFunction; Expected #0, Actual #1.</pre>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>_mock<span style="color: #008000;">.</span><span style="color: #0000FF;">Record</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Expect<span style="color: #008000;">.</span><span style="color: #0000FF;">Call</span><span style="color: #008000;">&#40;</span>_myMock<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;a&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Return</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;b&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008080; font-style: italic;">/* insert expectation for void function here */</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>_mock<span style="color: #008000;">.</span><span style="color: #0000FF;">Playback</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// DoSomething calls the mocked object's GetValue function, which returns 'b' when the parameter is 'a'</span>
  <span style="color: #008080; font-style: italic;">// DoSomething also calls a void function which is located in the interface of the mocked object, so this</span>
  <span style="color: #008080; font-style: italic;">// void function must be mocked too, else the test run will fail</span>
  Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;b&quot;</span>, _myObject<span style="color: #008000;">.</span><span style="color: #0000FF;">DoSomething</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;a&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><span id="more-95"></span></p>
<p>When trying to create an expectation for this void function Visual Studio will give a syntax error:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Expect<span style="color: #008000;">.</span><span style="color: #0000FF;">Call</span><span style="color: #008000;">&#40;</span>_myMock<span style="color: #008000;">.</span><span style="color: #0000FF;">MyVoidFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Argument '1': cannot convert from 'void' to 'Rhino.Mocks.Expect.Action'</span></pre></div></div>

<p>On the internet I found two solutions to fix this problem. The <a href="http://ayende.com/Blog/archive/2007/10/17/Rhino-Mocks-Void-methods-using-Expect.Call.aspx" target="_blank">first solution</a> uses a delegate:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Expect<span style="color: #008000;">.</span><span style="color: #0000FF;">Call</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span> _myMock<span style="color: #008000;">.</span><span style="color: #0000FF;">MyVoidFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The <a href="http://stackoverflow.com/questions/359831/rhinomock-test-mock-interface-void-functions" target="_blank">second solution</a> uses a <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Expect<span style="color: #008000;">.</span><span style="color: #0000FF;">Call</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> _myMock<span style="color: #008000;">.</span><span style="color: #0000FF;">MyVoidFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now the test will succeed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/03/15/using-rhino-mocks-to-mock-a-void-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

