<?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>haslo.ch - Guido's Blog &#187; code</title>
	<atom:link href="http://www.haslo.ch/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haslo.ch/blog</link>
	<description>We believe that people with passion can change the world for the better.</description>
	<lastBuildDate>Wed, 16 Nov 2011 11:23:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>SetProperty and GetProperty with C# Reflection</title>
		<link>http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/</link>
		<comments>http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 12:32:29 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2863</guid>
		<description><![CDATA[I&#8217;ll make a post soon, comparing Ruby on Rails to C# with ASP.NET MVC, but this here is atomic and might be of help for one or two readers. If you want to set and get properties in C# in a type-agnostic way, these are the functions you want to put into a library class [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll make a post soon, comparing Ruby on Rails to C# with ASP.NET MVC, but this here is atomic and might be of help for one or two readers.</p>
<p>If you want to set and get properties in C# in a type-agnostic way, these are the functions you want to put into a library class somewhere (or, as I did, into the class that uses them):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">object</span> getProperty<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> containingObject, <span style="color: #FF0000;">string</span> propertyName<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> containingObject.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span>propertyName, BindingFlags.<span style="color: #0000FF;">GetProperty</span>, <span style="color: #0600FF;">null</span>, containingObject, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> setProperty<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> containingObject, <span style="color: #FF0000;">string</span> propertyName, <span style="color: #FF0000;">object</span> newValue<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    containingObject.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span>propertyName, BindingFlags.<span style="color: #0000FF;">SetProperty</span>, <span style="color: #0600FF;">null</span>, containingObject, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> newValue <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>That&#8217;s all I will say on this subject.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating my own little Portal Site</title>
		<link>http://www.haslo.ch/blog/creating-my-own-little-portal-site/</link>
		<comments>http://www.haslo.ch/blog/creating-my-own-little-portal-site/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 11:43:15 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[haslo]]></category>
		<category><![CDATA[politblog]]></category>
		<category><![CDATA[portal]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2810</guid>
		<description><![CDATA[As you may or may not have noticed, I now have a second blog: haslos Politblog. I decided I&#8217;d want to blog about Swiss politics from time to time as well, and with this blog being English only and having a global rather than Swiss target audience, these subjects didn&#8217;t really fit in here. So [...]]]></description>
			<content:encoded><![CDATA[<p>As you may or may not have noticed, I now have a second blog: <a href="http://haslo.ch/politblog/">haslos Politblog</a>. I decided I&#8217;d want to blog about Swiss politics from time to time as well, and with this blog being English only and having a global rather than Swiss target audience, these subjects didn&#8217;t really fit in here.</p>
<div id="attachment_2831" class="wp-caption alignright" style="width: 160px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2009/07/politblog.jpg"><img class="size-thumbnail wp-image-2831" title="Politblog" src="http://www.haslo.ch/blog/wp-content/uploads/2009/07/politblog-150x114.jpg" alt="Politblog" width="150" height="114" /></a><p class="wp-caption-text">Politblog</p></div>
<p>So I went and started a second blog. Finding a theme and installing WordPress was one thing, but how do you tie those things together? And while we&#8217;re at it, why not add stuff like the <a href="http://twitter.com/haslo">Twitter</a> feed or <a href="http://delicious.com/haslo">Delicious</a> bookmarks? And why not make a mini-tutorial out of it? (That idea actually wasn&#8217;t mine, <a href="http://twitter.com/knowLED/status/2775913174">thanks @knowLED</a>!)</p>
<p>When looking at easy ways to create such a mashup for a <a href="http://haslo.ch/">portal site</a>, I stumbled upon the <a href="http://code.google.com/apis/ajaxfeeds/">Google AJAX Feed API</a>, and was amazed. Essentially, it does everything we need, in a format that&#8217;s incredibly easy to style with CSS. The <a href="http://code.google.com/apis/ajaxfeeds/documentation/#FEEDCONTROL">higher-level FeedControl class</a> is easy to use, and does exactly what I need: display a collection of feeds in &lt;div&gt; elements.<br />
<span id="more-2810"></span><br />
If you want to use it as well, I suggest you <a href="http://code.google.com/apis/ajaxfeeds/key.html">sign up for an API key</a> (although it&#8217;s not mandatory). The code that actually fetches the feeds then is simple. Add this to your HTML &lt;head&gt; element&#8230;</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;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://www.google.com/jsapi&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8230;and then include bits of JavaScript that load the feeds. Since I wanted to be able to style the blog feeds a bit differently from the other feeds, I created <em>two</em> empty &lt;div&gt; elements that they&#8217;ll go into in the HTML body, with the ids &#8220;blogfeeds&#8221; and &#8220;otherfeeds&#8221;. Empty because they&#8217;ll be populated with the AJAX JavaScript goodness.</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;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;blogfeeds&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;otherfeeds&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This is the JavaScript code, populating two FeedControl objects in an array and then rendering them to their respective &lt;div&gt;s. The feed URLs don&#8217;t have to be escaped, the escaped bits you see are what the Google API puts in as titles; I wanted those to be links to the respective feed source. Make sure you put it between &lt;script&gt;&lt;/script&gt; tags, best in the page header:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;feeds&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> feedControl <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">feeds</span>.<span style="color: #660066;">FeedControl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">feeds</span>.<span style="color: #660066;">FeedControl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addFeed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.haslo.ch/blog/feed/&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.haslo.ch/blog/<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;Blog&amp;lt;/a&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addFeed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.haslo.ch/politblog/feed/&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.haslo.ch/politblog/<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;Politblog&amp;lt;/a&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addFeed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://feeds.backtype.com/haslo&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.backtype.com/haslo/<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;BackType&amp;lt;/a&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addFeed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://twitter.com/statuses/user_timeline/14274896.rss&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://twitter.com/haslo/<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;Twitter&amp;lt;/a&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addFeed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://feeds.delicious.com/v2/rss/haslo?count=15&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://delicious.com/haslo/<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;Delicious&amp;lt;/a&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">setNumEntries</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">setNumEntries</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;blogfeeds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feedControl<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;otherfeeds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
google.<span style="color: #660066;">setOnLoadCallback</span><span style="color: #009900;">&#40;</span>initialize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s it, that will populate the &lt;div&gt; elements with the feed data. Unstyled, this does look a bit ugly though, a mere list. So I added some CSS, and used <a href="http://ryanfait.com/resources/footer-stick-to-bottom-of-page/">this nice tutorial here for making the footer stick to the bottom</a>. Go and have a look, it&#8217;s all explained nicely there. Inside that footer, I styled the elements (with Google&#8217;s classes, found easily with <a href="http://getfirebug.com/">Firebug</a>) like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.gfc-results</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">460px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.gfc-resultsRoot</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#blogfeeds</span> <span style="color: #00AA00;">*</span> <span style="color: #6666ff;">.gfc-resultsRoot</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">23</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#otherfeeds</span> <span style="color: #00AA00;">*</span> <span style="color: #6666ff;">.gfc-resultsRoot</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">18</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Finally, for the title, I made an absolutely positioned &lt;div&gt; at the top, et voilà.</p>
<p>The last step was making Facebook aware of the fact that I now have two blogs. As you may know, you can only add one source of the type Feed/RSS for automatic updates (nice <a href="http://www.tech-recipes.com/rx/2885/facebook_import_blogs_rss_feeds_without_third_party_applications/">tutorial for adding RSS feeds to Facebook</a>). Which is silly.</p>
<p>But anyway, the way around this involved <a href="http://pipes.yahoo.com/pipes/">Yahoo Pipes</a>, or rather, just one rather simple pipe that involves a union of the two feeds, and then a descending sort by date. <a href="http://pipes.yahoo.com/pipes/pipe.edit?_id=755f7ea0e13a135cfb3cb74f89ed9c8b">I made a copy of it public, have a look</a>. They&#8217;re nice like that, you can just drag-and-drop elements that you want to use, and connect their outlets (lower end) with other element&#8217;s inputs (upper end) until you reach the &#8220;pipe output&#8221; element, creating a flow diagram that works out of the box.</p>
<div id="attachment_2833" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2009/07/portal.jpg"><img class="size-medium wp-image-2833" title="Portal" src="http://www.haslo.ch/blog/wp-content/uploads/2009/07/portal-300x229.jpg" alt="Portal" width="300" height="229" /></a><p class="wp-caption-text">Portal</p></div>
<p>That&#8217;s it already, and <a href="http://haslo.ch/">here&#8217;s the result</a>. I hope you can use an element or two out of this post for your own projects <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Creating my own little Portal Site" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/creating-my-own-little-portal-site/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting MySQL 5.0 to work in Ruby 1.9.1 on Windows 7</title>
		<link>http://www.haslo.ch/blog/getting-mysql-5-0-to-work-in-ruby-1-9-1-on-windows-7/</link>
		<comments>http://www.haslo.ch/blog/getting-mysql-5-0-to-work-in-ruby-1-9-1-on-windows-7/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 00:15:59 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2768</guid>
		<description><![CDATA[So, I got MySQL to work for my Ruby on Rails development environment. Let&#8217;s be clear about what I actually wanted: Ruby 1.9.1 Rails 2.3.2 Any version of MySQL whatsoever Any way whatsoever to make my up-to-date Ruby on Rails talk to that MySQL I don&#8217;t care what version of MySQL. I don&#8217;t care if [...]]]></description>
			<content:encoded><![CDATA[<p>So, I got MySQL to work for my Ruby on Rails development environment. Let&#8217;s be clear about what I actually wanted:</p>
<ul>
<li>Ruby 1.9.1</li>
<li>Rails 2.3.2</li>
<li>Any version of MySQL whatsoever</li>
<li>Any way whatsoever to make my up-to-date Ruby on Rails talk to that MySQL</li>
</ul>
<p>I don&#8217;t care what version of MySQL. I don&#8217;t care if I have to force platforms for Gems or recompile or whatever. I never did care, and I&#8217;ll happily even dig through code to make manual changes before compiling or what-have-you. What I do care about is actually getting this to work, so I can finally stop tinkering and start doing something productive.</p>
<p>Well, I learned a lot on my way there. I had a glimpse at <a href="http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Extending_Ruby_Creating_a_Makefile_with_extconf.rb.html">how extconf.rb works</a> (because I had to figure out correct parameters), I dug through rails sources to find errors that you don&#8217;t even want to know about, had another good look at <a href="http://www.gnu.org/software/make/">makefiles</a>, and generally tried to understand what exactly didn&#8217;t work.</p>
<p>Also, there is a more efficient way to do this, as <a href="http://www.haslo.ch/blog/getting-mysql-5-0-to-work-in-ruby-1-9-1-on-windows-7/#comment-1134">pointed out by Luis in the comments</a>. Thanks!</p>
<h2>So, in the end, it now works</h2>
<p>This <a href="http://blog.xajler.net/?p=14">tutorial blog post on Code Elite</a> was very, very helpful. So were Luis comments <a href="http://www.haslo.ch/blog/actually-installing-ruby-on-rails-on-windows-7/#comments">here</a> and <a href="http://blog.mmediasys.com/2009/06/23/rubyinstaller-get-ready-to-vote/#comments">else</a>w<a href="http://blog.mmediasys.com/2008/08/10/rubygems-with-power-comes-responsibility/#comments">here</a> (although me being quite the newbie when it comes to gcc and the likes, they went a bit over my head).</p>
<p>The Code Elite post is not the best at being copy-paste-able though, the formatting kinda ruins that. It does point out the correct steps to getting MySQL to work in Ruby (and consequently, Rails).</p>
<p>Long story short, these are the steps that lead to a working MySQL integration:<br />
<span id="more-2768"></span></p>
<ol>
<li>Download Ruby 1.9.1 MinGW32 and the corresponding developer kit from the <a href="http://rubyinstaller.org/downloads/">RubyInstaller download page</a>. I prefer the zipped version to the one with installer, to be honest, but that&#8217;s just me.</li>
<li>Get the 32-bit version (not 64-bit, or linking won&#8217;t work) of MySQL 5.0 (not 5.1, or nothing will work) from <a href="http://dev.mysql.com/downloads/mysql/5.0.html#win32">the MySQL download page</a>. Again, I prefer the zipped version.</li>
<li>Get the current mysql-ruby source (2.8.1, currently) <a href="http://tmtm.org/downloads/mysql/ruby/">from its FTP</a>.</li>
<li>Put Ruby into <strong>F:\Ruby</strong> or your path of preference. Add Ruby&#8217;s <strong>\bin</strong> path to <a href="http://www.computerhope.com/issues/ch000549.htm">your path variable</a>. Close and re-open the command window to update the path.</li>
<li>Install Rails &#8211; this is easy.

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> rails</pre></div></div>

</li>
<li>Put MySQL into <strong>F:\mysql</strong> (or your path, again). No adding to the path yet here.</li>
<li>Put the contents of the mysql-ruby package into your Ruby path (I didn&#8217;t try other paths) and point your command line there, then run this:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby extconf.rb <span style="color: #660033;">--with-mysql-lib</span>=f:<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>opt <span style="color: #660033;">--with-mysql-include</span>=f:<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>include</pre></div></div>

</li>
<li>Run <strong>make</strong>, then <strong>make install</strong>. Here is where 64-bit MySQL fails.</li>
<li>Add the following to your path variable (substituting your MySQL location): <strong>F:\mysql\lib\opt;F:\mysql\bin</strong>. Again, close and re-open the command window to update the path.</li>
<li>Run this to let Ruby <a href="http://www.youtube.com/watch?v=EmEPXXJ4sKw">know MySQL</a>:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'require &quot;mysql&quot;'</span></pre></div></div>

</li>
<li>Get into a command prompt as admin (you might have to right-click a command prompt and &#8220;run as admin&#8221; to get to this) to install and start the MySQL service. To do that, run these in the mysql\bin directory:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqld-nt <span style="color: #660033;">--install</span>
net start mysql</pre></div></div>

</li>
<li>Now that the MySQL server is actually running, you can connect to it to change your root password:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqladmin <span style="color: #660033;">-u</span> root password topsecretpassword</pre></div></div>

</li>
</ol>
<p>Steps 1-3 are just downloads. Steps 4 and 5 already installs Ruby on Rails completely. Steps 6-12 then are for installing mysql (which is necessary first for the DLLs to be available to the compiler later) and then compiling the mysql-ruby package (driver?) from scratch into Ruby.</p>
<p>Interestingly, step 7 builds the makefile through Ruby (mostly, with some automated C mixed in), while step 8 actually compiles the C source through make and step 10 plugs this into Ruby. Steps 11 and 12 merely configure the MySQL server.</p>
<p>This should be it. And this time, I hope I did no forbidden or ugly things <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Getting MySQL 5.0 to work in Ruby 1.9.1 on Windows 7" />  <span>For they know not what they do and stuff.<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/getting-mysql-5-0-to-work-in-ruby-1-9-1-on-windows-7/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Actually Installing Ruby on Rails, on Windows 7</title>
		<link>http://www.haslo.ch/blog/actually-installing-ruby-on-rails-on-windows-7/</link>
		<comments>http://www.haslo.ch/blog/actually-installing-ruby-on-rails-on-windows-7/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 13:32:10 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2703</guid>
		<description><![CDATA[OK, so I found out what it is that I have to install for my wonderful Ruby on Rails environment. It&#8217;s time to start installing. Keep in mind that much of what I&#8217;m doing here is an experiment (and thanks Luis for being so patient with me). I don&#8217;t claim to know what works and [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so<a href="http://www.haslo.ch/blog/assessing-installing-ruby-on-rails-on-windows-7/"> I found out what it is that I have to install</a> for my wonderful Ruby on Rails environment. It&#8217;s time to start installing.</p>
<p>Keep in mind that much of what I&#8217;m doing here is an experiment (and thanks Luis for being so patient with me). I don&#8217;t claim to know what works and what doesn&#8217;t, I&#8217;m just trying to find my way around here. Follow my steps at your own risk &#8211; they seem to lead to a working environment for now.</p>
<p><strong>Update 2009-07-03 02:16:</strong> <a href="http://www.haslo.ch/blog/getting-mysql-5-0-to-work-in-ruby-1-9-1-on-windows-7/">There&#8217;s a followup post now</a> that does less evil and wrong things, hopefully. It also happens to result in a working Ruby on Rails plus MySQL environment, something this post doesn&#8217;t quite achieve.</p>
<h2>Getting Ruby to run</h2>
<p>Admittedly, I was a little surprised by how easy this was. <a href="http://rubyinstaller.org/downloads/">Get Ruby and the Development kit from here</a>. Extract them (using <a href="http://www.7-zip.org/">7-Zip</a>, which you should use by default anyway), and put all the contained directories where you want your Ruby. Mine resides in <strong>F:\Ruby</strong>.</p>
<p>Next, add the newly installed bin directory to your path. Quickest way: Open the control panel, search for &#8220;environment&#8221;, then click &#8220;edit the system environment variables&#8221;, and add the bin after a semicolon to the &#8220;Path&#8221; variable. Then, change the path in <em>devkit/msys/1.0.11/etc/fstab</em> to where you put your stuff &#8211; you won&#8217;t have to do this if you put Ruby into <strong>C:\Ruby</strong>.</p>
<p>This is what we get:</p>
<div id="attachment_2704" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2009/06/Ruby_Installed.jpg"><img class="size-medium wp-image-2704 " title="Ruby Installed" src="http://www.haslo.ch/blog/wp-content/uploads/2009/06/Ruby_Installed-300x166.jpg" alt="Ruby Installed" width="300" height="166" /></a><p class="wp-caption-text">Ruby Installed</p></div>
<p>Yay. That was easy. And the greatest thing is that the One-Click installer (even if it&#8217;s not entirely one-click, currently) also already adds support for Ruby Gems.<br />
<span id="more-2703"></span></p>
<h2>Getting Rails</h2>
<p>The next step is to install some gems. There is <a href="http://akitaonrails.com/2009/1/13/the-best-environment-for-rails-on-windows">a long list on Akita on Rails</a>, but after realizing how little stuff works with Ruby 1.9.1 and Windows 7, I chose to (nearly) only install the things I need:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> rails</pre></div></div>

<p>This installs Rails and the gems it depends on.</p>
<h2>ImageMagick, and Some More Stuff</h2>
<p>Next, since I planned to do so and not out of some technical necessity, we install ImageMagick for Windows &#8211; get the newest <a href="http://rubyforge.org/frs/?group_id=12&amp;release_id=35951">Win32 Binary Gem version from RubyForge</a> and extract it somewhere. Install the gem by going into the directory it&#8217;s extracted into and then executing this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> rmagick-2.10.0-x86-mswin32.gem</pre></div></div>

<p>Substitute the gem name with the version you&#8217;re actually using. Then install ImageMagick itself, with the .exe that you just extracted as well.</p>
<p>If you&#8217;re like me, you won&#8217;t be able to resist attempting to install some more gems. <a href="http://www.railsenvy.com/2009/5/6/railsconf-2009-tuesday">Here&#8217;s a nice video with some suggestions</a> from Railsconf 2009. Personally, I installed googlecharts, twitter, cucumber and pdf-writer, but haven&#8217;t gotten to try them out properly yet.</p>
<h2>Testing What We Have</h2>
<p>We have Ruby on Rails now. Yep, that&#8217;s it. Rather painless so far, eh? Test it by going to the directory you want your projects in and typing this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rails testproject</pre></div></div>

<p>You&#8217;ll see Rails creating plenty of directories and files now. After it&#8217;s done, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> testproject
ruby script<span style="color: #000000; font-weight: bold;">/</span>server</pre></div></div>

<p>Point your web browser to <em>http://127.0.0.1:3000/</em>, and you should see Rails, in all its glory:</p>
<div id="attachment_2720" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2009/06/Rails_Installed.jpg"><img class="size-medium wp-image-2720 " title="Rails Installed" src="http://www.haslo.ch/blog/wp-content/uploads/2009/06/Rails_Installed-300x233.jpg" alt="Rails Installed" width="300" height="233" /></a><p class="wp-caption-text">Rails Installed</p></div>
<h2>Not Installing MySQL</h2>
<p>I started out by attempting to install <a href="http://www.mysql.com/">MySQL 5.1</a>, thinking it was stable and all. However, as it turns out, there are plenty of problems with that, and none of them stem from MySQL:</p>
<ul>
<li>The mysql gem relies on libmySQL.dll, which has to be in the path.</li>
<li>The mysql gem is precompiled, there is no 64-bit Windows version, and the 32-bit version relies on msvcrt-ruby18.dll, rather than the newer msvcrt-ruby191.dll.</li>
<li>There is a segmentation fault that I haven&#8217;t found the cause of.</li>
</ul>
<p>I was able to fix the first two problems by adding the mysql/bin directory to the path, and installing a Ruby 1.8.6 alongside the Ruby 1.9.1 to get the 1.8 DLL &#8211; but nevertheless, I never got rid of that segmentation fault, despite multiple reinstalls and even trying MySQL 5.0 instead of 5.1 (<a href="http://www.google.ch/search?q=mysql-2.7.3-x86-mswin32%2Fext%2Fmysql.so%3A+[BUG]+Segmentation+fault">Google search for the exact cause</a>, none of the proposed solutions helped).</p>
<p>(Do not install the 1.8 DLL, that&#8217;s apparently very evil.)</p>
<p>Nothing worked until I gave it a try with Ruby 1.8.6 (after all, I had that installed from attempting to get that DLL, and only needed to change the path variable and install the rails gem to have a go), and lo and behold everything worked fine.</p>
<p>Which convinced me <strong><em>not</em></strong> that I should use Ruby 1.8.6, there&#8217;s too many shiny new features in 1.9.1. Instead, it convinced me that I should forget about MySQL.</p>
<h2>Installing PostgreSQL</h2>
<p>Since <a href="http://www.oracle.com/technology/pub/articles/kern-rails-migrations.html">Rails migrations</a> make database dependencies a thing of the past (or at least, to a very, very large extent), I decided to install PostgreSQL instead. Their <a href="http://www.postgresql.org/download/windows">one-click Windows installer for PostgreSQL 8.3</a> worked a treat and installed the service and everything just fine. Next step, have Rails be able to talk to it &#8211; there are two PostgreSQL gems, only one of which (postgres-pr, <strong><em>not</em></strong> ruby-postgres) currently seems to work in Windows. So let&#8217;s get it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> postgres-pr <span style="color: #660033;">--platform</span>=mswin32</pre></div></div>

<div id="attachment_2746" class="wp-caption alignright" style="width: 160px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2009/06/Create_Role.jpg"><img class="size-thumbnail wp-image-2746 " title="Create Role" src="http://www.haslo.ch/blog/wp-content/uploads/2009/06/Create_Role-150x150.jpg" alt="Create Role 150x150 Actually Installing Ruby on Rails, on Windows 7" width="150" height="150" /></a><p class="wp-caption-text">Create Role</p></div>
<p>(Not sure if the platform is necessary, but it can&#8217;t hurt.)</p>
<p>Set up a database user (&#8220;login role&#8221; in PostreSQL-speak) with pgAdmin &#8211; I called mine rails &#8211; and give it a password and the ability to create databases.</p>
<p>Now we change the <em>database.yml</em> in the project&#8217;s <em>/config/</em> directory. Open it with some text editor (I like <a href="http://www.editplus.com/">EditPlus</a> myself) and change it to fit your configuration.</p>
<p>This is the <em>database.yml</em> that makes my test project fly:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">development:
 adapter: postgresql
 database: testproject
 username: rails
 password: ***
 pool: <span style="">5</span>
 timeout: <span style="">5000</span>
 host: localhost</pre></div></div>

<p>Note that host:localhost (or the equivalent for your database) is important, as otherwise Rails will attempt to connect to PostgreSQL using <a href="http://en.wikipedia.org/wiki/Unix_domain_socket">UNIX Sockets</a> &#8211; which obviously wouldn&#8217;t work. After that, let&#8217;s test. Go to your shiny rails project in a command window, and type or paste these:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby script<span style="color: #000000; font-weight: bold;">/</span>generate scaffold <span style="color: #7a0874; font-weight: bold;">test</span> title:string description:text
rake db:create
rake db:migrate
ruby script<span style="color: #000000; font-weight: bold;">/</span>server</pre></div></div>

<p>After that, point your browser to <em>http://127.0.0.1:3000/tests/</em> and poke around a bit. You should have your first Rails app running, including all four <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> actions.</p>
<div id="attachment_2733" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2009/06/CRUD_Running.jpg"><img class="size-medium wp-image-2733 " title="CRUD Running" src="http://www.haslo.ch/blog/wp-content/uploads/2009/06/CRUD_Running-300x199.jpg" alt="CRUD Running" width="300" height="199" /></a><p class="wp-caption-text">CRUD Running</p></div>
<h2>I Think I&#8217;m Done Here</h2>
<p>Done after installing aforementioned <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug</a> (just click &#8220;install&#8221;), <a href="https://addons.mozilla.org/en-US/firefox/addon/60">Web Developer</a> (yep, &#8220;install&#8221;) and <a href="http://git-scm.com/">Git</a> for msys <a href="http://code.google.com/p/msysgit/downloads/list">from Google Code</a> (which seems to work fine so far). There&#8217;s a Ruby gem for Git, too; install with this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> git</pre></div></div>

<p>I then also got <a href="http://www.vim.org/download.php#pc">gVim 7.3</a> &#8211; not for the faint-hearted, I do have some experience with vim from coding Python on Linux though. I will also test how well editing works with my <a href="http://www.editplus.com/">EditPlus</a>, and probably some IDEs. Either way, I&#8217;ll probably make a little personal user interface evaluation post later on.</p>
<p>Looking back to <a href="http://www.haslo.ch/blog/assessing-installing-ruby-on-rails-on-windows-7/">the assessment post</a>, I&#8217;m actually not done at all. The following things are still missing &#8211; most of them, for a reason:</p>
<ul>
<li>ruby-debug <a href="http://rubyforge.org/tracker/index.php?func=detail&amp;aid=26292&amp;group_id=1900&amp;atid=7439">doesn&#8217;t work with Ruby 1.9.x yet</a>, so that falls flat. It&#8217;s merely a wrapper for the built-in debugger though, so I will be able to debug anyway.</li>
<li><a href="http://mongrel.rubyforge.org/">Mongrel</a> has &#8220;ruby script/server&#8221; produce another segmentation fault, and I don&#8217;t want to spend another 7 hours for not finding a solution to it.</li>
<li><a href="http://httpd.apache.org/">Apache</a> doesn&#8217;t make much sense in a pure development environment, particularly not if we don&#8217;t have Mongrel anyway.</li>
<li><a href="http://subversion.tigris.org/getting.html#windows">Subversion has plenty of versions on their download page</a>. I downloaded <a href="http://www.sliksvn.com/en/download">the one from Slik SVN</a> myself, but haven&#8217;t installed it yet until I actually need it. So that&#8217;s technically missing, too.</li>
</ul>
<h2>Where To Go Now</h2>
<p>There are some resources that can get you started with your most splendid new Ruby on Rails environment. If you have no idea what Rails is yet or why you should use it, have a look at <a href="http://www.railsenvy.com/2007/3/22/ruby-on-rails-video-presentation">this presentation</a>. I found the <a href="http://oreilly.com/catalog/9780596515775/">Head First Rails</a> book to be a very good introduction, too. Concerning Ruby, <a href="http://poignantguide.net/ruby/">Why&#8217;s (Poignant) Guide to Ruby</a> (available <a href="http://poignantguide.net/ruby/whys-poignant-guide-to-ruby.pdf">as pdf</a>, too) is both free <em>and</em> great, albeit talking about Ruby 1.8.2, a tiny bit outdated. If you like it thorough, get the book only known as <a href="http://www.pragprog.com/titles/ruby3/programming-ruby-1-9">the Pickaxe</a>. It is as good as they say.</p>
<p>Thanks for listening, and I hope I can help one or two among you out. You like it? You hate it? You found something I didn&#8217;t? Let me and all your fellow readers know in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/actually-installing-ruby-on-rails-on-windows-7/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Assessing Installing Ruby on Rails, on Windows 7</title>
		<link>http://www.haslo.ch/blog/assessing-installing-ruby-on-rails-on-windows-7/</link>
		<comments>http://www.haslo.ch/blog/assessing-installing-ruby-on-rails-on-windows-7/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 12:48:45 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=2675</guid>
		<description><![CDATA[So I wanted to get up and running with a development system for Ruby on Rails, on my shiny freshly installed Windows 7 machine (so, no development stuff on it as of yet whatsoever). However, the recent Ruby update to 1.9.1 brought such big changes that apparently, many libraries don&#8217;t work with it anymore &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>So I wanted to get up and running with a development system for Ruby on Rails, on my shiny freshly installed Windows 7 machine (so, no development stuff on it as of yet whatsoever). However, the recent Ruby update to 1.9.1 brought such big changes that apparently, many libraries don&#8217;t work with it anymore &#8211; so the installer of choice, the <a href="http://rubyforge.org/projects/rubyinstaller/">One-Click Ruby Installer</a>, is (for now) stuck at supporting Ruby 1.8.6 only.</p>
<p><strong>Update 2009-06-23 15:12:</strong> There seems to be <a href="http://github.com/oneclick/rubyinstaller/tree/master">a way to make the one-click installer build Ruby 1.9.1</a>, guess I&#8217;ll have to try that out.</p>
<p>The <a href="http://rubyforge.org/forum/forum.php?forum_id=33233">One-Click Ruby Installer is not dead</a>, and there&#8217;s even a <a href="http://blog.mmediasys.com/2009/06/23/rubyinstaller-get-ready-to-vote/">design vote for the 1.9.1 website</a>. But since I want my development environment bleeding edge and I want it now, I&#8217;ll just have to make do with a manual install. Since I haven&#8217;t found a tutorial detailing the steps for that, I&#8217;ll just do it from scratch and take you along the ride. Good tutorials I did find are:</p>
<ul>
<li><a href="http://akitaonrails.com/2009/1/13/the-best-environment-for-rails-on-windows">The Best Environment for Rails on Windows</a>, including a rant against IDEs</li>
<li><a href="http://allaboutruby.wordpress.com/2006/01/09/installing-rails-on-windows-step-by-step-tutorial/">Installing Rails on Windows</a>, including a guide for setting up MySQL</li>
<li><a href="http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/">Time For A Grown-Up Server</a>, for adding Mongrel and Apache</li>
</ul>
<p>So, what do we need, for a full-blown environment?</p>
<ul>
<li><a href="http://www.ruby-lang.org/en/">Ruby 1.9.1</a> &#8211; obviously, can&#8217;t have Ruby without Ruby</li>
<li><a href="http://docs.rubygems.org/">Ruby Gems 1.3.4</a> &#8211; since gems are the Ruby way of distributing anything</li>
<li><a href="http://rubyonrails.org/">Rails 2.3</a> &#8211; obviously, can&#8217;t have Ruby on Rails without Rails</li>
<li><a href="http://www.mysql.com/">MySQL 5.1</a> &#8211; the database we&#8217;ll use, although others would work as well</li>
<li><a href="http://mongrel.rubyforge.org/">Mongrel 1.1.5</a> &#8211; reliable and secure webserver</li>
</ul>
<p>That&#8217;s it for the start, but for additional tool goodness (and in order to have some of the above run properly in the first place) we need more:</p>
<ul>
<li><a href="http://git-scm.com/">Git 1.6.3.3</a> &#8211; the version control used by rubyists</li>
<li><a href="http://subversion.tigris.org/">Subversion 1.6.3</a> &#8211; very widespread version control (and I refuse to add CVS to this list)</li>
<li><a href="http://www.mingw.org/wiki/MinGW">MinGW</a> and <a href="http://www.mingw.org/wiki/MSYS">MSYS</a> &#8211; to make sure gems can be installed and compiled if necessary</li>
<li><a href="http://rake.rubyforge.org/">Rake 0.8.3</a> &#8211; the ruby version of make</li>
<li><a href="http://rubyforge.org/projects/ruby-debug/">Ruby-Debug 0.10.3</a> &#8211; who doesn&#8217;t like debuggers</li>
<li><a href="http://www.imagemagick.org/">ImageMagick 6.5.3</a> &#8211; can be used by Rails</li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug 1.4.0b2</a> &#8211; for debugging web apps&#8217; JavaScript code (requires the <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.5 beta</a>)</li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/60">Web Developer 1.1.6</a> &#8211; for CSS and HTML stuff</li>
<li><a href="http://sourceforge.net/projects/mysqlcc/">MySQL Control Center 0.9.4</a> &#8211; or an alternative that can admin MySQL databases</li>
<li><a href="http://httpd.apache.org/">Apache 2.2.11</a> &#8211; front-end webserver for load balancing and whatnot</li>
</ul>
<p>An intimidating list at first sight, but I did try to get everything I might need in there. The hard part then will be making all of them talk together. Concerning the actual development environment, I am as of yet undecided between <a href="http://www.vim.org/download.php">gVim</a> and an IDE like <a href="http://www.aptana.com/">Aptana</a> or <a href="http://www.jetbrains.com/ruby/">RubyMine</a>. Both have upsides and downsides.</p>
<p>So, is this list complete, do you see anything I&#8217;ve missed? Or do I have anything on it that you think is irrelevant? Let me know in the comments!</p>
<p>So much for talking the talk &#8211; the next step will be walking the walk, and I&#8217;ll make sure to give you a full tutorial afterwards.</p>
<p>Update 2009-06-24 16:05: Yep, Ruby on Rails is installed. <a href="http://www.haslo.ch/blog/actually-installing-ruby-on-rails-on-windows-7/">Here is how I did it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/assessing-installing-ruby-on-rails-on-windows-7/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Stack Overflow: Solutions for Coders</title>
		<link>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/</link>
		<comments>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 12:56:35 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[bubble 2.0]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=1225</guid>
		<description><![CDATA[If you&#8217;re a programmer, you know the problem of solution googlability. It&#8217;s particularly prominent in mainstream languages (and one of the things I decried in my What&#8217;s Wrong with VBA series, as VBA is particularly bad in that respect); you have a problem, you google for it, unless you&#8217;re coding in a good awesome well-designed [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1252" class="wp-caption alignright" style="width: 160px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2008/09/stack_overflow.jpg"><img class="size-thumbnail wp-image-1252" title="Stack Overflow in Firefox 3" src="http://www.haslo.ch/blog/wp-content/uploads/2008/09/stack_overflow-150x150.jpg" alt="Stack Overflow" width="150" height="150" /></a><p class="wp-caption-text">Stack Overflow</p></div>
<p>If you&#8217;re a programmer, you know the problem of solution googlability. It&#8217;s particularly prominent in mainstream languages (and one of the things <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/#community">I decried</a> in my <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">What&#8217;s Wrong with VBA series</a>, as VBA is particularly bad in that respect); you have a problem, you google for it, unless you&#8217;re coding in a <span style="text-decoration: line-through;">good</span> <span style="text-decoration: line-through;">awesome</span> <span style="text-decoration: line-through;">well-designed</span> freak language like Ruby, Python, or Haskell, all you get back is trash and spam.</p>
<p>So, since you&#8217;re reading blogs and a programmer (or you&#8217;d have stopped reading by now, I guess), you know <a href="http://www.joelonsoftware.com/">Joel on Software</a>. Now this guy and his team have done something: They took the Wiki approach of &#8220;everybody can edit&#8221;, added Digg&#8217;s &#8220;let&#8217;s vote stuff up&#8221;, and made the whole thing a generic programming Q&amp;A site.</p>
<p style="text-align: center;"><strong>Behold, and witness, the rise of</strong> <a href="http://stackoverflow.com/"><strong>Stack Overflow</strong></a>.</p>
<p>From <a href="http://www.joelonsoftware.com/items/2008/09/15.html">his launch post</a>:</p>
<blockquote><p>Here’s how it’s supposed to work. This is a community project, so I’m being careful to avoid saying this is how it will work… that’s up to the community. But this is roughly what I have in mind.</p>
<p>Every question in Stack Overflow is like the Wikipedia article for some extremely narrow, specific programming question. <em>How do I enlarge a fizzbar without overwriting the user’s snibbit?</em> [...]</p>
<p>Some people propose answers. Others vote on those answers. If you see the right answer, vote it up. If an answer is obviously wrong (or inferior in some way), you vote it down. Very quickly, the best answers bubble to the top. The person who asked the question in the first place also has the ability to designate one answer as the “accepted” answer, but this isn’t required. The accepted answer floats above all the other answers.</p></blockquote>
<p>Sounds great! It has tags, it&#8217;s not restricted to one programming language, it could provide the framework for thousands of programmers collaborating. And the best thing: They plan to &#8220;keep it ad-free and open to the public forever&#8221;.</p>
<p>You&#8217;ll need an <a href="http://en.wikipedia.org/wiki/Openid">OpenID</a> to log in &#8211; I got one <a href="https://www.myopenid.com/">at myOpenID</a> myself. Too bad it&#8217;s not possible to log in without, but whatever &#8211; I guess this approach has more promise than the closed one that Microsoft tried to force upon us with that whole <a href="http://en.wikipedia.org/wiki/Windows_Live_ID">Live ID</a> crap, but still shares the same weaknesses (which are, mainly, another party involved and a central point of failure).</p>
<p>Nevertheless, I do think this could be awesome. Let&#8217;s see where it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What&#8217;s Wrong With VBA? (3/3)</title>
		<link>http://www.haslo.ch/blog/whats-wrong-with-vba-33/</link>
		<comments>http://www.haslo.ch/blog/whats-wrong-with-vba-33/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 17:27:48 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scorchers]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=496</guid>
		<description><![CDATA[part 1 &#8211; part 2 &#8211; part 3 This is the third part of a three-part series, have a look at part 1 and part 2 first. So let&#8217;s go on ahead and jump into the final bits of my troubles with MS Access and VBA as environment for medium and larger IT projects &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
<p>This is the third part of a three-part series, have a look at <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> and <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> first.</p>
<p>So let&#8217;s go on ahead and jump into the final bits of my troubles with MS Access and VBA as environment for medium and larger IT projects &#8211; basically, anything having 2 and up users.</p>
<h2>Syntax</h2>
<p>There are quite a few things that bother me about the VBA syntax. First of all: it&#8217;s inconsistent, even more so than the one of <a href="http://en.wikipedia.org/wiki/PHP">php</a> (where mainly <a href="http://www.tnx.nl/php.html#args">method signatures are inconsistent</a>), in several places. The most prominent of them are functions. Take these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> answer <span style="color: #000080;">As</span> VbMsgBoxResult
MsgBox <span style="color: #800000;">&quot;Hello World!&quot;</span>, vbOKOnly
answer = MsgBox(<span style="color: #800000;">&quot;Hello World?&quot;</span>, vbYesNo)</pre></div></div>

<p>Did you notice anything? Yes, indeed. Take note, the parentheses are forbidden in the first case, and mandatory in the second. Now assume that you have a function call that you suddenly need the answer from, whereas you didn&#8217;t need it earlier. Not only do you now have to write the assignment, but you also have to add parentheses to the function call. Way to spend productive time.</p>
<p>The same goes for &#8220;Set&#8221; and assignments to native data types and Objects: For Objects, it&#8217;s mandatory, for native types it&#8217;s forbidden. That goes even for Objects stored in a Variant variable &#8211; all Variants are not made equal, some will throw runtime errors when you assign stuff to them.<br />
<span id="more-496"></span><br />
Another annoying syntax bit is this one:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #008000;">' Won't work!
</span><span style="color: #000080;">Dim</span> someNumber <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = 5
<span style="color: #008000;">' Will work :-/
</span><span style="color: #000080;">Dim</span> someNumber <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
someNumber = 5</pre></div></div>

<p>Yay, more <a href="http://en.wikipedia.org/wiki/Lines_of_code#Disadvantages">lines of code</a>, how great. But wait, I have more! What do you think this produces?</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> firstVar, secondVar <span style="color: #000080;">As</span> <span style="color: #000080;">Double</span>
<span style="color: #000080;">Dim</span> thirdVar, fourthVar <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span></pre></div></div>

<p>Yep, you guessed it. &#8220;firstVar&#8221; is a Variant, &#8220;secondVar&#8221; a Double, &#8220;thirdVar&#8221; a Variant, &#8220;fourthVar&#8221; an Object. Oh, you got it wrong? Don&#8217;t worry, it&#8217;s just that silly.</p>
<p>My final pet peeve with the syntax is its capitalization (and thus technically the IDE and not VBA itself). VBA is not per se case sensitive &#8211; and it expresses that through simple means: Whenever it encounters a variable, it tries to capitalize it the way it&#8217;s supposed to be, to make it clear that it&#8217;s the same.</p>
<p>Now, enter <a href="http://en.wikipedia.org/wiki/Scope_(programming)">scoping</a>, and intuitively named API bits. So you have a local variable called &#8220;currentItem&#8221; &#8230; what happens if you try to access the property &#8220;CurrentItem&#8221; in, say, an <a href="http://msdn.microsoft.com/en-us/library/aa210923(office.11).aspx">Inspector Object</a>? Right, it&#8217;s capitalized &#8220;currentItem&#8221;. It has nothing at all to do with my local variable, but the IDE is too stupid to notice.</p>
<p>Of course there&#8217;s other things amiss or strange, like the lack of a <a href="http://www.fmsinc.com/free/NewTips/NET/NETtip33.asp">side effect free ternary operator</a>, or the (nearly syntax) VBA developer gospel for the redundant <a href="http://en.wikipedia.org/wiki/Hungarian_notation">Hungarian notation</a> taken to <a href="http://www.joelonsoftware.com/articles/Wrong.html">wrong extremes</a>, but I&#8217;ll just stop here.</p>
<h2>MS Access</h2>
<p>I won&#8217;t even go into how I find Access 2007 to be much worse than its predecessors when it comes to usability, with longer mouse paths and more clicks per action, the <a href="http://office.microsoft.com/en-us/excel/HA101730521033.aspx">developer tab not showing by default</a>, and shortcuts no longer easily visible. Just so much: <strong>it&#8217;s annoying</strong>. I guess I still haven&#8217;t gotten used to the new, less quick, <a href="http://teaandbiscuits.org.uk/drupal/node/8">way of displaying shortcuts</a>, and often, object placements aren&#8217;t intuitive for me and I&#8217;m looking for things in the wrong tab.</p>
<p>What really bothers though is that Access has <strong>stability issues</strong>. Access is the only program so far that managed to crash on my development system. <a href="http://www.adobe.com/products/dreamweaver/">Dreamweaver MX 04</a> is quite an old make and has huge problems with Vista&#8217;s <a href="http://www.microsoft.com/windows/windows-vista/features/user-account-control.aspx">UAC</a>, but it doesn&#8217;t crash. Nor does any other tool. But Access, which happens to often corrupt databases when it does.</p>
<p>Even crashes aside, occasionally the <strong>database just corrupts</strong> like that, without any visible reason. Or it keeps code that you deleted, and keeps spitting error messages how that code isn&#8217;t valid &#8211; without you having any possibility to do anything about it, not even overwriting helps.</p>
<p>I hope you make backups every couple minutes, I certainly learned to. I haven&#8217;t found another reliable way to get those databases back into stable conditions.</p>
<p>Then, what&#8217;s one of the many responsibilities of a proper <a href="http://en.wikipedia.org/wiki/Database_management_system">DBMS</a>? <strong>Housekeeping</strong>! What does Access do? Provide you with &#8220;compact and repair&#8221;, which is user-initiated <em>only</em>. Well, I guess with huge databases that Access couldn&#8217;t handle anymore anyway, that would make sense, not having that an automated regular task.</p>
<p>Furthermore, every single user <strong>needs MS Access installed</strong> on his computer. Potentially a security risk, certainly not something that should be necessary. You don&#8217;t go and install an MS SQL Server on everybody&#8217;s machine. At least Microsoft consequently follows through, and their <a href="http://office.microsoft.com/en-us/access/HA011225981033.aspx">Access security FAQ</a> is all about how you can protect your computer from malicious Access macros, and not how you can secure your Access databases.</p>
<p>By the way, <strong>Access doesn&#8217;t scale</strong> &#8211; what else is new. <a href="http://databases.aspfaq.com/database/what-are-the-limitations-of-ms-access.html">Multiple users are a problem</a>, for larger databases (&gt;<a href="http://support.microsoft.com/kb/302524/">2GB</a>, but you&#8217;re hitting <a href="http://www.programmingado.net/a-381/Performance-testing-Access-MySQL-MSSQL.aspx">performance bottlenecks</a> way before that) you have to move to a proper backend &#8211; essentially, lugging around the whole DBMS part of your GUI windowing toolkit for naught afterwards.</p>
<p>It does at least have <a href="http://mcs.open.ac.uk/kgw9/interesting/AccessLocking.html">proper locking</a>, quite refined <a href="http://www.databasedev.co.uk/permissions.html">permissions</a>, and with Access 2007, <a href="http://databases.about.com/od/productinfo/a/encryption.htm">database encryption</a> (without which all the permission structures in the world are worth naught) as well.</p>
<h2>Other Things</h2>
<p>There&#8217;s a few other small things that I found incredibly annoying, coming from other languages like <a href="http://java.sun.com/">Java</a>, <a href="http://www.python.org/">Python</a> and my personal favourite, <a href="http://www.ruby-lang.org/en/">Ruby</a>.</p>
<p>For one, <a href="http://wesnerm.blogs.com/net_undocumented/2005/10/null_comparison.html"><strong>Null comparisons</strong> work in odd ways</a>, VBA uses <a href="http://en.wikipedia.org/wiki/Ternary_logic">ternary logic</a> (although admittedly, this is consistent with the way SQL handles Nulls).</p>
<p>The next minor thing are <strong>silly defaults</strong> &#8211; there are two lines every halfway serious programmer adds as a very first thing to every single bit of VBA code they write (and this goes so far that there&#8217;s option settings to add it automatically to every source bit):</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
<span style="color: #000080;">Option</span> Compare Database</pre></div></div>

<p>The reason for this is simple: backwards compatibility. While VB6 (maybe earlier?) changed the default to the need for explicit variable declaration, VBA never did, and thus it&#8217;s still possible to mistype a simple variable and never find the error later on. And as for Compare Database &#8230; yeah sure, let&#8217;s compare stuff everywhere a bit differently, to make sure the database behaves completely unforeseeable.</p>
<p>Another one: <strong>No external source file support</strong>. If you want to use any library or other cool thingy you found on the net or elsewhere, you have to copy-paste the code into Access. Where it&#8217;s needed, that is, which of course means that if it&#8217;s needed anywhere else, you have to copy-paste it again. The only way around this are Access databases that allow you to copy-paste (oh, look, a pattern) the respective classes or modules as a whole. No, you can&#8217;t just link (with <a href="http://www.access-programmers.co.uk/forums/showthread.php?t=27139">very limited exceptions</a>). Hooray?</p>
<p>We&#8217;re not done yet: You <strong>can&#8217;t return an array</strong>! If you want a Function that returns an array, you have to define it as returning a Variant. If you do that, you can return arrays (since they&#8217;re a subtype of Variant), but will lose all meta information: Data type, size, everything. So if you want to return an array and its size, you have to do it via separate functions, or with a stateful returning object, or with a custom ADT.</p>
<p>Something that was rightfully pointed out to me (thanks Ste!) &#8211; why is it that VBA (and VB.NET, for that matter) distinguishes between <strong>Functions and Subs</strong>? There&#8217;s no fundamental difference, one returns something, the other doesn&#8217;t (so, essentially returns Nothing), but that&#8217;s it. Visual Studio goes so far to automatically change the keyword from Sub to Function when you return something.</p>
<h2>Conclusion</h2>
<p>I guess it&#8217;s apparent by now: I do think that it&#8217;s justifiable to say that VBA is outdated and should be dumped right now.</p>
<p>For Microsoft: There are good replacements, there&#8217;s no reasonable reason beyond &#8220;it was easier&#8221; to have Office 2007 use VBA still, and there&#8217;s certainly no reason to make the next version of MS Office still have VBA support &#8211; particularly if that means that there still won&#8217;t be native VB.NET support inside MS Office.</p>
<p>How delusional of me to think they&#8217;ll actually see this, though <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Whats Wrong With VBA? (3/3)" /> </p>
<p>For anybody considering Access and VBA for their projects: Well, if you still want to, you can of course. Or get the free <a href="http://www.microsoft.com/express/vb/Default.aspx">Visual Studio Express for VB.NET</a> plus <a href="http://www.microsoft.com/express/sql/Default.aspx">SQL Server Express</a>, and stick to a Microsoft environment with a very similar but much more mature language, and a more performant database, for less money.</p>
<p>I still do work with VBA, when I really have to, but I think I laid out here why it&#8217;s a fact that I really only do so when there is no other way.</p>
<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/whats-wrong-with-vba-33/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What&#8217;s Wrong With VBA? (2/3)</title>
		<link>http://www.haslo.ch/blog/whats-wrong-with-vba-23/</link>
		<comments>http://www.haslo.ch/blog/whats-wrong-with-vba-23/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 11:08:11 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scorchers]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=494</guid>
		<description><![CDATA[part 1 &#8211; part 2 &#8211; part 3 This is the second part of a three-part series, have a look at part 1 first if you haven&#8217;t read it yet. Let&#8217;s just jump right in and continue the list of problems I have with VBA as a programming language for medium and larger MS Access [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
<p>This is the second part of a three-part series, have a look at <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> first if you haven&#8217;t read it yet.</p>
<p>Let&#8217;s just jump right in and continue the list of problems I have with VBA as a programming language for medium and larger MS Access projects.</p>
<h2>Error Handling</h2>
<p>The modern paradigm for error handling is <a href="http://en.wikipedia.org/wiki/Exception_handling">exception handling</a>, but to VBA that doesn&#8217;t apply for a simple reason: VBA doesn&#8217;t have exceptions. Instead, it <a href="http://msdn.microsoft.com/en-us/library/5hsw66as(VS.80).aspx">has this</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> ErrorHandler</pre></div></div>

<p>Yes, that&#8217;s a GoTo, and we all know <a href="http://xkcd.com/292/">those are evil</a>. Even worse than GoTo though is this line:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span></pre></div></div>

<p>Basically, &#8220;just ignore all the errors that happen now, they&#8217;re not important&#8221;. Ah, right. We can still check for error codes, so even this evil construct (<a href="http://www.developerfusion.co.uk/show/4325/">never ever</a> use this as default!) is occasionally necessary to avoid the spaghetti code that the GoTo alternative would produce, while providing reasonable feedback instead of the techy default Access warnings:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">DoCmd.SetWarnings <span style="color: #000080;">False</span>
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span>
doSomethingUseful
<span style="color: #000080;">If</span> <span style="color: #000080;">Not</span> Err.Number = 0 <span style="color: #000080;">Then</span>
    MsgBox <span style="color: #800000;">&quot;Descriptive error message&quot;</span>, vbCritical, <span style="color: #800000;">&quot;Error&quot;</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0
DoCmd.SetWarnings <span style="color: #000080;">True</span></pre></td></tr></table></div>

<p>What a silly contraption. In VB.NET, it&#8217;d look like this, and be way more flexible and expandable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Try</span>
    doSomethingUseful<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception
    <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Descriptive error message&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">Critical</span>, <span style="color: #808080;">&quot;Error&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span></pre></td></tr></table></div>

<p><span id="more-494"></span>The current consensus among programmers is that <a href="http://david.tribble.com/text/goto.html">GoTo isn&#8217;t necessarily evil</a>, but it has to be used the right way. Myself, I have yet to see a case where I find GoTo to be better than its alternatives, but that&#8217;s subjective I guess. Excursus: I couldn&#8217;t resist solving the <a href="http://david.tribble.com/text/goto.html#conclusion">Last Example &#8211; Nontrivial Gotos</a> from that last link, with less C code and no GoTo and, in my opinion, clearer:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> parse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>Token tok <span style="color: #339933;">=</span> gettoken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> END<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>shift<span style="color: #009900;">&#40;</span>tok<span style="color: #009900;">&#41;</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: #339933;">!</span>reduce<span style="color: #009900;">&#40;</span>tok<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> ERROR<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> ACCEPT<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Anyway. VBA provides a GoTo, which is neutral, and it forces that GoTo upon programmers for error handling due to lack of alternatives, which is bad. <a href="http://cprogramming.com/tutorial/goto.html">Only C gets away with that</a>.</p>
<h2>The IDE</h2>
<p>The Access VBA <a href="http://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> basically consists of two parts (and thus technically isn&#8217;t really integrated, but we&#8217;ll let that slide): The in-Access editor (or layouter) for forms and reports, and the code editor for actual programming.</p>
<p>The layouter works quite well really, except that the one even in the free <a href="http://www.microsoft.com/Express/">Visual Studio Express</a> (which isn&#8217;t even the central part of that IDE, unlike in Access) is lightyears ahead, be it concerning auto-snaps or ease of use or number of clicks for anything. Microsoft doesn&#8217;t seem to use its internal synergies all that well.</p>
<p>The same then goes for the code editor, just worse: It hasn&#8217;t gotten an update in recent years, the one that shipped in Office 2007 is exactly the same as the one from Office 2003. Grievances are&#8230;</p>
<ul>
<li>Whenever you have a half-finished line, and switch line to write something somewhere else, you have an error message box pop up that you have to click away first &#8211; because it only checks errors when you leave a line, and has no other way to display them.</li>
<li>Class lookup is shaky, with new custom classes it often it only works after you exit and restart Access.</li>
<li>Even if it works it&#8217;s not completely reliable, working autocompletion is a rare positive surprise.</li>
<li>Debugging is pretty limited, often when there&#8217;s an error the IDE doesn&#8217;t even stop at the point where the error actually occurred but at some point further up the call stack.</li>
</ul>
<p>I can live with those shortcomings of course, but I really don&#8217;t see why Microsoft didn&#8217;t just use the Visual Studio IDE instead &#8211; it would not only have fixed those problems, but also have gained better searching capabilities, snippets, refactoring and wizards to go with it.</p>
<h2>Object Orientation</h2>
<p>VBA is object oriented! Yeah, to some extent: You can make classes, and there&#8217;s even <a href="http://www.developerfusion.co.uk/show/3965/3/">properties</a>. However, yet again there&#8217;s a bullet list of things that should be better:</p>
<ul>
<li>You can&#8217;t have parameters for your class constructors (the <a href="http://www.xtremevbtalk.com/showthread.php?t=224316">Class_Initialize Sub</a>), so initializing a class must be either with default values or through a separate &#8220;init&#8221; function that&#8217;s called after instantiation with &#8220;New&#8221;.</li>
<li>No real <a href="http://en.wikipedia.org/wiki/Inheritance_(computer_science)">inheritance</a>, you can just implement interfaces (and thus inherit method signatures, but no content or properties or anything) &#8211; considering there&#8217;s no compile time type checking anyway, I don&#8217;t see much purpose in that.</li>
<li>No <a href="http://en.wikipedia.org/wiki/Method_overloading">overloading</a>. Yes, you can have <a href="http://vbadud.blogspot.com/2007/04/optional-parameter-example.html">optional parameters</a>, but the same function name with different signatures won&#8217;t work (the language does support encapsulation though, so you can <a href="http://www.utteraccess.com/forums/showflat.php?Board=48&amp;Number=1252455&amp;Main=1252455">override functions in narrower namespaces</a>, but that&#8217;s not really overloading).</li>
<li>Objects are handled different from native data types. Not just &#8220;they are different&#8221;, but they have differing syntax too &#8211; an assignment to an object needs &#8220;Set&#8221;, one to a Variant must not have &#8220;Set&#8221;. So even though you can store an Object in a Variant, you&#8217;ll run into runtime errors if you do. Admittedly, Java shares similar problems, but at least it has <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html">autoboxing</a> meanwhile.</li>
</ul>
<p>Some people also have a problem with VBA having both modules <em>and</em> classes &#8211; myself, I see that more pragmatically: There are cases when you&#8217;d make just static functions in a class anyway, or work just with a singleton, so why not pack those in a module instead? You can always change to a class structure later if it becomes necessary.<br />
<a name="community" /><br />
<h2>The Community</h2>
<p>Now this one is certainly controversial, and even more subjective than the rest of this post series. First off, there are quite a few great community sites full of dedicated and knowledgable authors, like the <a href="http://msdn.microsoft.com/en-us/vbasic/cc546545.aspx">MS VB forums</a>, <a href="http://www.databasedev.co.uk/">databasedev.co.uk</a>, <a href="http://www.vb-helper.com/index.html">VB Helper</a>, <a href="http://www.mvps.org/access/">The Access Web</a>, <a href="http://www.freevbcode.com/">FreeVBCode</a>, the <a href="http://www.tek-tips.com/threadminder.cfm?pid=707">TekTips VBA section</a> and the <a href="http://www.fmsinc.com/free/tips.html#TipsforAccess">FMS tips section</a>.</p>
<p>But what I&#8217;m talking about is the &#8220;Googlability&#8221; of problems.</p>
<p>If I don&#8217;t find a solution in a site I know (or sometimes while looking in one of those sites, with &#8220;site:foobar.com&#8221;), I just Google it (<a href="http://www.justfuckinggoogleit.com/">of course</a>) &#8211; problems do occur, and others must have had the same ones before. Now what happens if I do that for VBA problems?</p>
<p>I find trash.</p>
<p>This is for these three reasons, I guess:</p>
<ul>
<li>There isn&#8217;t so much a VBA community as 4 VBA communities: ASP VBA, Excel VBA, Word VBA, and finally also Access VBA (about in that order of prominence) &#8211; and then there&#8217;s the VB6 and VB.NET community too, with solutions that often don&#8217;t apply to VBA.<br />
So the search queries have to get more and more elaborate, adding &#8220;-asp&#8221; and &#8220;-excel&#8221; is common if you want to find anything &#8211; thus it takes longer until you find your solutions.</li>
<li>There are plenty of solution providers that might provide reasonable solutions, but cost money. Now, that&#8217;s a problem of mine I guess, but I don&#8217;t like giving my credit card info to some obscure website for a solution that might or might not work for me. Luckily, <a href="http://www.customizegoogle.com/">CustomizeGoogle</a> filters those out quite reliably, after I tell it to.</li>
<li>There are plenty of inexperienced users when it comes to VBA, way more so than with most other programming languages and other technologies I used so far (well, <a href="http://en.wikipedia.org/wiki/Python_(programming_language)">Python</a> and <a href="http://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby</a> tend to have geeky target audiences anyway, but the same applies to <a href="http://en.wikipedia.org/wiki/Java_(programming_language)">Java</a>). This means two things:
<ul>
<li>Often, questions are answered less than optimal by only slightly more knowledgable users than the ones asking the question, meaning the solution is either just wrong or not generic and thus doesn&#8217;t work in a slightly altered context.</li>
<li>Even more often, questions aren&#8217;t answered at all. The favourites here are the people later writing &#8220;nevermind, I found the solution&#8221;, without providing <em>what</em> that solution was that I could use now as well&#8230;</li>
</ul>
</li>
</ul>
<p>Keep in mind that I don&#8217;t have any problems with inexperienced users whatsoever, I&#8217;m regularly a newbie in yet another programming language I&#8217;m learning myself. And asking questions is a good thing anyway &#8211; it&#8217;s just that it seems like more experienced VBA programmers move on to VB.NET or something and thus VBA queries often disappear in the void, because there&#8217;s nobody experienced around to answer them.</p>
<p>That&#8217;s all for now, stay tuned for <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3 with syntax, MS Access, other things and a conclusion</a>.</p>
<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/whats-wrong-with-vba-23/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What&#8217;s Wrong With VBA? (1/3)</title>
		<link>http://www.haslo.ch/blog/whats-wrong-with-vba-13/</link>
		<comments>http://www.haslo.ch/blog/whats-wrong-with-vba-13/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 13:56:11 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scorchers]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=267</guid>
		<description><![CDATA[part 1 &#8211; part 2 &#8211; part 3 Visual Basic for Applications &#8211; the legacy language that Microsoft apparently still won&#8217;t drop even in future Office updates, although they&#8217;re working on it, and certainly hasn&#8217;t dropped in Office 2007. The plague with a lobby. Why is it so bad, is it so bad at all? [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
<p><a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">Visual Basic for Applications</a> &#8211; the legacy language that Microsoft apparently still won&#8217;t drop even in future Office updates, although they&#8217;re working on it, and certainly hasn&#8217;t dropped in Office 2007. The plague with a <a href="http://classicvb.org/petition/">lobby</a>. Why is it so bad, is it so bad at all?</p>
<p>This very subject here was, in fact, one of the things that made me think &#8220;I have to get a blog again&#8221; over those years when I didn&#8217;t have one &#8211; I so wanted to write it whenever I was forced to work with applications coded inside <a href="http://office.microsoft.com/en-us/access/">MS Access</a>, or when I coded them myself from scratch. I decided to make what started as a single post into a three part series, simply because there was so much content that one blog post couldn&#8217;t hold it all.</p>
<p>I won&#8217;t go into VBA for the rest of the Office suite too much, for tiny macro things it might even fit, what I&#8217;m talking about is medium (and larger) applications. And I will use the term &#8220;database&#8221; for the course of this article series for something it technically doesn&#8217;t strictly apply to, which is a MS Access application and actually an entire <a href="http://en.wikipedia.org/wiki/Database_management_system">DBMS</a>, plus GUI stuff and application logic, all mashed together.</p>
<p>So, what are all those things that bug me so much?<br />
<span id="more-267"></span></p>
<h2>Language Updates</h2>
<p>That&#8217;s possibly the biggest gripe of them all. Quoting the very <a href="http://msdn.microsoft.com/en-us/isv/bb190538.aspx">Microsoft VBA site</a>, at least MS makes their stance towards VBA perfectly clear:</p>
<blockquote><p>[...] As previously announced, Microsoft does not expect to make significant enhancements to VBA. [...]</p>
<p>As of July 1, 2007, Microsoft will no longer offer VBA distribution licenses to new customers. Existing VBA customers can still purchase additional VBA licenses from Summit Software and Microsoft for existing solutions.</p></blockquote>
<p>In other words, &#8220;we&#8217;re desparately trying to get rid of this mess&#8221;. Well, why wouldn&#8217;t they: VBA wasn&#8217;t updated for nearly 10 years now &#8211; while programming languages evolved around it, some very well-designed behemoths (for the time they were designed at) like <a href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a> still stand their ground, many others have vanished and were replaced.</p>
<p>Yes, of course there&#8217;s always a demand for coders for older languages, just look at <a href="http://en.wikipedia.org/wiki/Cobol">COBOL</a>. If you like VBA, there&#8217;s plenty of legacy Access applications, and <a href="http://www.fmsinc.com/TPapers/genaccess/DBOD.asp">places where Access makes sense</a>, so you won&#8217;t be out of a job any time soon.</p>
<p>That doesn&#8217;t change that the language would be in desparate need of an update, wouldn&#8217;t the update already exist in the form of <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET">VB.NET</a> and, to a lesser extent, in <a href="http://en.wikipedia.org/wiki/C_Sharp_(programming_language)">C#</a>. Why Microsoft hasn&#8217;t at least added support for those inside MS Access in the course of introducing <a href="http://www.microsoft.com/NET/">.NET</a> is beyond me.</p>
<h2>The API</h2>
<p>What clearly does belong to the VBA API, particularly in our context of developing MS Access applications, is the windowing framework. While it has its very own problems that I&#8217;ll describe in the IDE chapter, it does what it&#8217;s supposed to do rather well: Forms, reports and components, with a nice drag-and-drop interface for tinkering interfaces. It doesn&#8217;t have the best performance out there, but for database applications that work, who makes that their top priority.</p>
<p>Beyond those widgets and happy drag-and-drop things, for the most part, the Application Programmer Interface of VBA can be described with a few simple words: It doesn&#8217;t exist. While VBA does make use of <a href="http://en.wikipedia.org/wiki/Data_Access_Objects">DAO</a> and <a href="http://en.wikipedia.org/wiki/ActiveX_Data_Objects">ADO</a> (not the way better <a href="http://en.wikipedia.org/wiki/ADO.NET">ADO.NET</a>, mind you), and the <a href="http://support.microsoft.com/kb/222101">Office object model</a> (which is actually more comfortable to use in VBA than in C#), everything that goes beyond that is out of bounds.</p>
<p>Let me illustrate by means of a symptomatic example.</p>
<p>Let&#8217;s assume you want a dynamically sized array, a Vector or ArrayList, that grows and shrinks as you add and remove items, preferrably useable as a <a href="http://en.wikipedia.org/wiki/Stack_(data_structure)">stack</a> or <a href="http://en.wikipedia.org/wiki/Queue_(data_structure)">queue</a> too by means of simple procedures. Now, in every other language that&#8217;s as simple as importing a bit of the API and then using said dynamic array. Not so in VBA, that bit of the API doesn&#8217;t exist (except if you count the <a href="http://msdn.microsoft.com/en-us/library/a1y8b3b3(VS.80).aspx">Collection</a> that has whooping 4 methods). There are &#8220;dynamically sized arrays&#8221; through lines like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> myArray(0 <span style="color: #000080;">To</span> arraySize)</pre></div></div>

<p>This does mean though that all the automatic growing and shrinking has to be added by hand, and push and pop don&#8217;t work at all. In short, you have to do everything yourself.</p>
<p>Which I did by the way, you can <a href="http://www.haslo.ch/blog/vba-vector-classes/">download my classes here</a>.</p>
<h2>Mangled Tiers</h2>
<p>Every non-trivial application, particularly multi-user applications, is best handled with <a href="http://en.wikipedia.org/wiki/Multitier_architecture">multiple tiers</a>: A database tier, a client/presentation tier, maybe an application server in between, that&#8217;s usually enough but can be extended. Less is bad, more is more complicated but depending on the application, necessary.</p>
<p>In MS Access, everything is &#8220;in the database&#8221; by default. Now,what does this mean? Basically, it&#8217;s a mess, and sorting things out properly is nearly impossible, whenever you update the database you also update the application and extending the user circle beyond, say, 1-2 users inevitably leads to problems (more on this later).</p>
<p>It is worth noting that these tiers can be somewhat simulated by using multiple databases for different purposes: You can easily have an &#8220;application&#8221; database that just does frontend work, and a &#8220;data&#8221; database. You can then create links from the &#8220;application&#8221; database to the &#8220;data&#8221; database, and have the frontend update independendly of the backend.</p>
<p>That&#8217;s common practice and makes sense, and comes close to true multi tier programming, although it still makes a mess of things because those links are hardcoded file paths and aren&#8217;t as easy to manage as they should be; the &#8220;data&#8221; database&#8217;s location has to be known on a file share, and every user needs write access to that share, which leads to obvious security inconsistencies and further problems that we&#8217;ll explore in the course of part 3.</p>
<p>The problem about paths being hardcoded can of course be solved by writing libraries that update paths, and the tier problem as a whole can to some extent be resolved by having data reside in an MS SQL (or other, through the rather dated <a href="http://en.wikipedia.org/wiki/Open_Database_Connectivity">ODBC</a>) database.</p>
<h2>What&#8217;s MVC?</h2>
<p>This problem is very similar to the last one concerning mangled tiers. Modern applications often use, for maintenance and development reasons, a <a href="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</a> approach. Some languages and frameworks, like <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, even make it really hard not to (which is good, considering the approach makes sense). Now, if you want to create a program that&#8217;s cleanly using that paradigm in VBA, you basically have to ignore all the structure there is and make a new one. The good news is that you can actually do that, of course, and VBA doesn&#8217;t stop you from doing it &#8211; it just doesn&#8217;t help you a bit.</p>
<p>It all starts out so nicely:</p>
<ul>
<li>The <strong>model</strong> is the database, tables and views can be accessed via DAO or ADO</li>
<li>The <strong>view</strong> is WYSIWYG-edited and stashed in Reports and Forms</li>
<li>Every Frame has its own class, akin to its very own <strong>controller</strong></li>
</ul>
<p>Regarding <strong>model</strong>s: Yes, in theory it&#8217;s nice like that. However, as soon as we start having things that go beyond tables and views, we&#8217;re screwed. That goes for simple things like tree view models (that are stored inside the tree view and not really accessible from the outside). You can make <a href="http://www.4guysfromrolla.com/webtech/011401-1.shtml">stand-alone recordsets</a> (aka RAM tables), you can even <a href="http://support.microsoft.com/kb/184397">disconnect recordsets</a> from their source, that&#8217;s good. But overall, the model is in the view and vice versa, splitting the two is hard and in some places impossible.</p>
<p>The <strong>view</strong> only in the WYSIWYG editor is a nice thought for simple things, but when you want to start exchanging components on the fly, you have to write your own view classes (or end in chaos, as many VBA projects do) anyway. Or, the alternative many VBA projects take, have invisible components that you make visible or visible depending on the mode the form currently is in &#8211; a maintenance nightmare. The best approach here is having subforms and exchanging those, that actually works really well too and is my favourite approach. It&#8217;s just not the way VBA and Access make easy, and it can yet again very easily lead to duplicated bits (code or UI components).</p>
<p>Finally, the <strong>controller</strong> &#8211; you can avoid duplicated code and make pretty good controllers if you completely ignore the Frame&#8217;s default classes and only use those for proxying your own controller classes that you write in addition to them. Works well, it&#8217;s just, again, not the way VBA and Access makes easy.</p>
<p>That&#8217;s all for now, we&#8217;re warming up. Stay tuned for&#8230;</p>
<ul>
<li><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">Part 2 with error handling, IDE, object orientation and the community</a></li>
<li><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">Part 3 with syntax, MS Access, other things and a conclusion</a></li>
</ul>
<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/whats-wrong-with-vba-13/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VBA Vector Classes</title>
		<link>http://www.haslo.ch/blog/vba-vector-classes/</link>
		<comments>http://www.haslo.ch/blog/vba-vector-classes/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 15:36:25 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[downloads]]></category>
		<category><![CDATA[haslo]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=416</guid>
		<description><![CDATA[If you&#8217;re anything like me and ever had to code anything in Visual Basic for Applications, you must have felt the need for a proper API with some very basic common ADTs. The most basic of them being, in my opinion, the dynamic array (Vector, ArrayList, List and Collection are often used more or rather [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re anything like me and ever had to code anything in <a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">Visual Basic for Applications</a>, you must have felt the need for a proper API with some very basic common <a href="http://en.wikipedia.org/wiki/Abstract_data_type">ADTs</a>. The most basic of them being, in my opinion, the <a href="http://en.wikipedia.org/wiki/Dynamic_array">dynamic array</a> (Vector, ArrayList, List and Collection are often used more or rather less synonymous). So what would it feel like to be able to write this and have it work?</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> primitiveVector <span style="color: #000080;">As</span> clsVector
<span style="color: #000080;">Set</span> primitiveVector = <span style="color: #000080;">New</span> clsVector
<span style="color: #000080;">Dim</span> myInt <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
myInt = 3
&nbsp;
primitiveVector.AddItem myInt</pre></div></div>

<p>There are of course other people who have made such a thing, and there&#8217;s the VBA <a href="http://msdn.microsoft.com/en-us/library/a1y8b3b3(VS.80).aspx">Collection</a>, but none of them quite fit what I wanted. I wanted a Vector that had random access, was able to handle <a href="http://en.wikipedia.org/wiki/LIFO">LIFO</a> and <a href="http://en.wikipedia.org/wiki/FIFO">FIFO</a> (and thus act as stack and queue), addition and insertion and deletion, could check for item existence, and had dynamic resizing. So I went and coded my own. I did this while working at <a href="http://promino.ch/">Promino AG</a> and thus the code is technically theirs, so thanks to <a href="https://www.xing.com/profile/PaulB_Moser/">Paul Moser</a> for the nod for publishing this under the GNU <a href="http://www.gnu.org/licenses/lgpl.html">Lesser General Public License</a>.</p>
<p><a href="http://www.haslo.ch/media/Vectors.zip"><img src="/media/floppy.png" style="border: none; float: left" title="VBA Vector Classes" alt="floppy VBA Vector Classes" /></a>You can download both classes (two? more below), together with a tiny trivial testing framework, <a href="http://www.haslo.ch/media/Vectors.zip">as Access 2002-2003 .mdb</a>.<br />
<span id="more-416"></span><br />
This here is the vector class for Variants. It goes into a new class module, I called mine &#8220;clsVector&#8221;:</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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> Compare Database
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #008000;">' Copyright 2007, 2008 by Promino AG
</span><span style="color: #008000;">' Author: Guido Gloor
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is free software: you can redistribute it and/or modify
</span><span style="color: #008000;">' it under the terms of the GNU Lesser General Public License as published by
</span><span style="color: #008000;">' the Free Software Foundation, either version 3 of the License, or
</span><span style="color: #008000;">' (at your option) any later version.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is distributed in the hope that it will be useful,
</span><span style="color: #008000;">' but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span style="color: #008000;">' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span style="color: #008000;">' GNU Lesser General Public License for more details.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' You should have received a copy of the GNU Lesser General Public License
</span><span style="color: #008000;">' along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
</span>
<span style="color: #000080;">Private</span> itemCount <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBufferSize <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBuffer() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Class_Initialize()
    itemCount = 0
    itemBufferSize = 100
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> NewDimensions()
    <span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> itemBuffer(itemBufferSize) <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CheckDimensions()
    <span style="color: #000080;">If</span> itemCount &gt; itemBufferSize - 2 <span style="color: #000080;">Then</span>
        itemBufferSize = itemBufferSize * 2
        NewDimensions
    <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> ShrinkDimensions()
    itemCount = itemBufferSize + 10
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetSize() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    GetSize = itemCount
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> AddItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>)
    CheckDimensions
    itemBuffer(itemCount) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    GetItem = itemBuffer(index)
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> PopItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    PopItem = itemBuffer(itemCount - 1)
    DeleteItem itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetNextQueueItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    GetNextQueueItem = itemBuffer(0)
    DeleteItem 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> ContainsItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    ContainsItem = <span style="color: #000080;">False</span>
&nbsp;
    <span style="color: #000080;">For</span> i = 0 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">If</span> itemBuffer(i) = item <span style="color: #000080;">Then</span> ContainsItem = <span style="color: #000080;">True</span>
    <span style="color: #000080;">Next</span> i
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> InsertItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>, item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    CheckDimensions
    <span style="color: #000080;">For</span> i = itemCount - 1 <span style="color: #000080;">To</span> index <span style="color: #000080;">Step</span> -1
        itemBuffer(i + 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemBuffer(index) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> DeleteItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">For</span> i = index + 1 <span style="color: #000080;">To</span> itemCount - 1
        itemBuffer(i - 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemCount = itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> IsEmpty() <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    IsEmpty = itemCount &lt;= 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>Now, it would be nice if we would be done. But, we&#8217;re not done yet, obviously, after all there&#8217;s plenty more code below this break. The reason for that is simple: In VBA, a Variant is not an Object, and an Object is not a Variant, consequently functions that take Objects don&#8217;t take Variants and vice versa (although you can technically store an Object in a Variant, this <em>will</em> lead to problems). Objects also happen to have &#8220;Set&#8221; mandatory in places where it isn&#8217;t allowed for Variants.</p>
<p>What this means is that there is no way to have the same Vector class for both your custom ADTs and the primitive data types. So basically, we have another class that has exactly the same functions and exactly the same algorithms, but takes Objects instead of Variants. I called mine &#8220;clsObjectVector&#8221;. Here is the source for the class 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> Compare Database
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #008000;">' Copyright 2007, 2008 by Promino AG
</span><span style="color: #008000;">' Author: Guido Gloor
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is free software: you can redistribute it and/or modify
</span><span style="color: #008000;">' it under the terms of the GNU Lesser General Public License as published by
</span><span style="color: #008000;">' the Free Software Foundation, either version 3 of the License, or
</span><span style="color: #008000;">' (at your option) any later version.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is distributed in the hope that it will be useful,
</span><span style="color: #008000;">' but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span style="color: #008000;">' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span style="color: #008000;">' GNU Lesser General Public License for more details.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' You should have received a copy of the GNU Lesser General Public License
</span><span style="color: #008000;">' along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
</span>
<span style="color: #000080;">Private</span> itemCount <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBufferSize <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBuffer() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Class_Initialize()
    itemCount = 0
    itemBufferSize = 100
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> NewDimensions()
    <span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> itemBuffer(itemBufferSize) <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CheckDimensions()
    <span style="color: #000080;">If</span> itemCount &gt; itemBufferSize - 2 <span style="color: #000080;">Then</span>
        itemBufferSize = itemBufferSize * 2
        NewDimensions
    <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> ShrinkDimensions()
    itemCount = itemBufferSize + 10
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetSize() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    GetSize = itemCount
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> AddItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)
    CheckDimensions
    <span style="color: #000080;">Set</span> itemBuffer(itemCount) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> GetItem = itemBuffer(index)
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> PopItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> PopItem = itemBuffer(itemCount - 1)
    DeleteItem itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetNextQueueItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> GetNextQueueItem = itemBuffer(0)
    DeleteItem 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> ContainsItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    <span style="color: #008000;">' The used ADTs need to have an Equals function for this to work
</span>    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    ContainsItem = <span style="color: #000080;">False</span>
&nbsp;
    <span style="color: #000080;">For</span> i = 0 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">If</span> itemBuffer(i).Equals(item) <span style="color: #000080;">Then</span> ContainsItem = <span style="color: #000080;">True</span>
    <span style="color: #000080;">Next</span> i
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> InsertItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>, item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    CheckDimensions
    <span style="color: #000080;">For</span> i = itemCount - 1 <span style="color: #000080;">To</span> index <span style="color: #000080;">Step</span> -1
        <span style="color: #000080;">Set</span> itemBuffer(i + 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    <span style="color: #000080;">Set</span> itemBuffer(index) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> DeleteItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">For</span> i = index + 1 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">Set</span> itemBuffer(i - 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemCount = itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> IsEmpty() <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    IsEmpty = itemCount &lt;= 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>This is it, have fun <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' title="VBA Vector Classes" />  As much as you can have if you&#8217;re forced to work with VBA, that is.</p>
<p>Oh, and if you can use it &#8211; comments are appreciated <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' title="VBA Vector Classes" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/vba-vector-classes/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

