<?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>Megatheriums for Breakfast &#187; Programming</title>
	<atom:link href="http://rightwordsoft.com/blogs/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://rightwordsoft.com/blogs</link>
	<description>musings from David Grigg</description>
	<lastBuildDate>Tue, 17 Apr 2012 23:03:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Adventures in WPF</title>
		<link>http://rightwordsoft.com/blogs/2009/08/05/adventures-in-wpf/</link>
		<comments>http://rightwordsoft.com/blogs/2009/08/05/adventures-in-wpf/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 04:40:17 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[DoEvents]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=438</guid>
		<description><![CDATA[I&#8217;m very new to Microsoft&#8217;s Windows Presentation Foundation (WPF), even though the technology has been out for a few years now (since .NET Framework and Visual Studio 2008). As with many new programming paradigms, I tend not to put in the hard yards in learning them until (1) they appear bedded down and stable and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m very new to Microsoft&#8217;s Windows Presentation Foundation (WPF), even though the technology has been out for a few years now (since .NET Framework and Visual Studio 2008).  As with many new programming paradigms, I tend not to put in the hard yards in learning them until (1) they appear bedded down and stable and (2) I see a real use for them.  The latter is usually the big decider &#8211; I can&#8217;t learn everything, particularly as my aging brain finds it harder and harder to cram in new stuff.</p>
<p>Anyway, I did do some work in WPF earlier this year, in building a digital signage system for the <a href="http://www.nsm.org.au/">National Sports Museum</a>.  Basically a display of current world records, it needed to be attractive, have interesting transitions, and be very easy to update.  </p>
<p>In years gone by I would certainly have built this kind of thing in Flash, possibly under the control of a C# shell program.  But it seemed like a good opportunity to get my feet wet with WPF, and so I gave it a go.  It was a struggle, but with the aid of a good textbook (<a href="http://www.amazon.com/gp/product/0596510373?ie=UTF8&#038;tag=grilledpterod-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0596510373">Programming WPF</a><img src="http://www.assoc-amazon.com/e/ir?t=grilledpterod-20&#038;l=as2&#038;o=1&#038;a=0596510373" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Chris Sells and Ian Griffiths) I managed to achieve a pretty good result, I think.  By the way, I do find that when learning something new it&#8217;s far more helpful to have a good, hardcopy, text book by my side than any amount of electronic help.</p>
<p>So over the weekend I got the impulse to do some more with WPF, the trigger being some astronomy articles I was reading.  </p>
<p>A <strong>long</strong> time ago I wrote a program called Gravitorium, <a href="http://rightwordsoft.com/products/gravitorium/index.html">still available as shareware</a> (though hardly anyone ever buys it now). The purpose was to act as a simple simulator of gravitational interactions between arbitrary stars, planets, asterioids, etc.  Build your own solar system and see what happens.  Throw a black hole through the middle of our own solar system.  That sort of thing.</p>
<p><img src="http://rightwordsoft.com/img/2009-08-05_1403.png" alt="The old version of Gravitorium" /></p>
<p>Now here&#8217;s the thing: Gravitorium was written nearly 10 years ago, in Visual Basic 6.  Calculating all the interactions in such a system is very slow, and updating the display is even slower.  Running on the kind of computers and graphics cards we had 10 years ago, it&#8217;s surprising that I was able to get any kind of reasonable result at all.  Watching a simulation the motion of the Moon around the Earth, for example, was a bit like watching grass grow, unless you cranked down the accuracy of the simulation a fair bit.</p>
<p>So for some time I&#8217;ve been toying with revisiting the program, using modern programming techniques and a much faster computer platform.  And I figured that WPF, which gives direct access to the graphics processor, would be just the shot to give a much speedier result.</p>
<p>Well, so far it hasn&#8217;t worked out like that.  Now do bear in mind that in what follows I&#8217;m just going to be exposing my experimentation and learning processes to you.  If you already thoroughly understand WPF, this article is not for you!</p>
<p>I can see that there are going to be major benefits from a WPF approach.  For example, in the old Gravitorium I was doing some pretty dumb things to draw planets and orbits.  Everything happened on a PictureBox control.  To create the illusion of a moving object I first drew a colored circle and then on the next phase, &#8216;erased&#8217; it by drawing a black rectangle over it.  All the objects and all of orbital trails were just bitmapped onto a single layer.  The orbit trails were just literally painted dots, and each time the display is rescaled, I had to keep track of the dots and repaint them.</p>
<p>In my new version, I&#8217;m playing with using a WPF Canvas object, and associating Ellipse, Label and Polyline objects with each body in the system.  These get moved around as the calculation proceeds, and WPF takes care of redrawing the display.  The Polyline object redraws the orbital trails (admittedly as a series of straight lines rather than curves, but the segments are so small they look smooth).</p>
<p>It&#8217;s kind of working, but I&#8217;m not there yet!  Refreshing the display regularly is a bit of a struggle, which I don&#8217;t yet understand.  In the old Windows Forms days, I would just repaint things in a loop, invalidating the picture box or doing an Application.DoEvents() call.  You can&#8217;t do that in WPF, which seems odd.  I found a couple of ways around this limitation.  The one that works best so far is a &#8216;fake&#8217; DoEvents() procedure which I found <a href="http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!526.entry">here</a>.  I modified it by adding the null object check, which avoids an exception on closing the application.</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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Threading</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">//....</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Processes all messages currently in the message queue.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;</span>
<span style="color: #008080; font-style: italic;">/// This method can potentially cause code re-entrancy problem, so use it with great care.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;  </span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoEvents<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #008000;">!=</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Dispatcher</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span>DispatcherPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> ThreadStart<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I do suspect that having to use a crutch like the above means that I don&#8217;t have my head around WPF as yet.</p>
<p>Anyway, the revisited application (which at present is only a proof of concept) is coming along:</p>
<p><img src="http://rightwordsoft.com/img/2009-08-05_1520.png" alt="Gravitorium WPF POC" /></p>
<p>This is working fast enough to be able to animate the labels along with the objects (the VB6 version has to pause to show labels) and still get a pretty swift animation.  At an equivalent level of accuracy the VB6 version takes 95 seconds to complete an orbit of the Moon.  The WPF one, even with the overhead of redrawing labels, only takes 25 seconds.  That&#8217;s about a four-fold increase in speed, and the results look much nicer.</p>
<p>Anyway, this is a WPF Work in Progress, so I&#8217;ll return to it as I continue my learning process.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/08/05/adventures-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bracket Creep</title>
		<link>http://rightwordsoft.com/blogs/2009/07/05/programming-journal-2/</link>
		<comments>http://rightwordsoft.com/blogs/2009/07/05/programming-journal-2/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 10:39:26 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=398</guid>
		<description><![CDATA[Well, last week I was back doing some Flash programming, creating an on-line game for one of our corporate clients. I must say that Flash&#8217;s Action Script 3 is a much better way to do this kind of work than the old days of AS2 (yeah, I know that is a long while ago now, [...]]]></description>
			<content:encoded><![CDATA[<p>Well, last week I was back doing some Flash programming, creating an on-line game for one of our corporate clients.</p>
<p>I must say that Flash&#8217;s Action Script 3 is a much better way to do this kind of work than the old days of AS2 (yeah, I know that <em>is</em> a long while ago now, but I&#8217;m old and I&#8217;ve been working in Flash since Flash 4, so I still think of AS3 as <strong>new</strong>).</p>
<p>But I do find the problem with moving between all of these C-like languages is keeping them all straight in my head, they are all so similar these days.  So similar that it&#8217;s really easy to forget the differences until you are brought up short by something going wrong.  With luck, this will occur at compile time.  If you&#8217;re not so lucky, it will happen at run time, in certain circumstances only.</p>
<p><img src="http://rightwordsoft.com/img/javacode.png" alt="PHP code" align="right" /><br />
The languages I&#8217;m thinking of are:</p>
<ul>
<li>C#</li>
<li>Action Script 3</li>
<li>Javascript</li>
<li>PHP</li>
<li>Objective C</li>
</ul>
<p>I program in these languages pretty much all the time (though mostly in C#, and with Objective C very much the newcomer to my experience).  </p>
<p>You could also add:</p>
<ul>
<li>Java</li>
<li>&#8230;and probably several other languages</li>
</ul>
<p>They all have essentially the same structures &#8211; a <strong>switch</strong> statement, a <strong>for statement</strong> or an <strong>if-then-else</strong> is pretty much identical in them all.  It&#8217;s as though the whole world had converted to speaking English &#8211; but with various regional dialects.  And you have to keep straight in your head which country you are in at the moment so you get the dialect right.</p>
<p>So changing from C# back to AS3 requires a fair bit of concentration.</p>
<p>Where they don&#8217;t compare, though, is in their respective IDEs (Integrated Development Environments).  I should probably be using Flex, rather than Flash, and the Eclipse IDE, which isn&#8217;t bad, as the tools inside Flash itself are absolute rubbish.  I actually find myself using Dreamweaver as a better AS3 code editor than what is available inside Flash CS3.  </p>
<p>The Objective-C IDE on the Mac (XCode) is pretty good, in comparison, but still not a patch on Microsoft&#8217;s Visual Studio, in my humble opinion.  </p>
<p>Microsoft do a lot of things very poorly, but developer tools they still do extremely well. If only they would stop fiddling with C# and not keep issuing new versions of it, each new version requiring a yet bigger .NET Framework to be downloaded by my end-users.  I have to say that I&#8217;m yet to spend much coding time using the additional features in C# version 3, such as LINQ, let alone the new features which are planned for C# version 4, out real soon now.  Most of the time I still target the .NET Framework 2, which is nice and small.  Well, 20 MB is small by today&#8217;s standards.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/07/05/programming-journal-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Journal</title>
		<link>http://rightwordsoft.com/blogs/2009/06/29/programming-journal/</link>
		<comments>http://rightwordsoft.com/blogs/2009/06/29/programming-journal/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 02:30:36 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Chapter Master]]></category>
		<category><![CDATA[chapter stops]]></category>
		<category><![CDATA[iTunes]]></category>
		<category><![CDATA[MarkAble]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=371</guid>
		<description><![CDATA[I thought I might try to start writing about what coding I&#8217;m doing, without going in to a lot of detail, but just to record my day-to-day struggles with this programming business. Updating Chapter Master At the moment, my main challenge is trying to get out another version of my shareware software Chapter Master. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I might try to start writing about what coding I&#8217;m doing, without going in to a lot of detail, but just to record my day-to-day struggles with this programming business.</p>
<p><strong>Updating Chapter Master</strong></p>
<p>At the moment, my main challenge is trying to get out another version of my shareware software <a href="http://rightwordsoft.com/products/chaptermaster">Chapter Master</a>. </p>
<p><img src="http://rightwordsoft.com/img/2009-06-29_1224.png" alt="New Chapter Master" /></p>
<p>It&#8217;s going on for a year since the last release, and at that stage the software, while working well for most people, was still in my mind a little experimental.  Certainly time to consolidate what I have learned since the last release, add some requested features, and some ideas of my own.</p>
<p>The purpose of the software is to create or manipulate &#8216;chapter stops&#8217; and optionally &#8216;chapter images&#8217; in AAC files (*.m4b or *.mp4 extension).  These are the files treated as audiobooks or &#8216;enhanced podcasts&#8217; by Apple&#8217;s iTunes and the iPod.  Chapter stops create points within the file to which the user can easily navigate.  Ideally, of course, one would position these chapter stops at the exact times in the audiobook where the narrator says &#8220;Chapter Five&#8221; or &#8220;Book Three&#8221; or &#8220;Part Two&#8221;.  The software will let you do that manually, but no way can I imagine what artificial intelligence would be required for the software to automatically find such points, though it has been requested.</p>
<p>My companion software <a href="http://rightwordsoft.com/products/markable">MarkAble</a> will allow you to merge together a bunch of source files and insert chapter stops at the beginning of each new file.  Sometimes the source files are such that these points are at the logical breaks in the book.  Chapter Master lets you edit these existing chapter stops to your heart&#8217;s content.</p>
<p>Most of my programming struggles over the last week, though, have been to do with trying to pull out &#8211; or create &#8211; the &#8216;cover art&#8217; which iTunes can associate with any audio file.</p>
<p>There are two types of image which can be embedded in an AAC file which has been through, or is going to be used by, iTunes and the iPod.  The first is the above-mentioned &#8216;cover art&#8217;, which is what shows up in the cover flow mode of iTunes, or in its grid view.  The second type of image is embedded along with the audio data.  These images can change at different times as the audio is played, and are what gives &#8216;enhanced podcasts&#8217; some of their charm.</p>
<p>Chapter Master originally only handled the latter type of image, and some users found it puzzling that the &#8216;cover art&#8217; didn&#8217;t turn up in Chapter Master.  So most of my efforts over the last week or so have been to alter the program so that it can manipulate either kind of image.  In the case of cover art, this meant that I had to parse and unpack the iTunes meta data, which includes the cover art images.</p>
<p>The AAC file structure is based on the structure Apple originally developed for QuickTime, and consists of &#8216;atoms&#8217; which can be nested (that is, atoms can contain other atoms).  Each atom starts with a data length (a 32-bit unsigned integer) and an atom type indicator which can be read as a 4-character ASCII string.</p>
<p>The iTunes meta data is stored in a &#8216;user data atom&#8217;, type-string &#8216;udta&#8217;.  Now this type of atom can contain anything the encoder software desires &#8211; which means one has to be careful to check that it contains data structured in the iTunes manner.  Apple structure their udta atom with yet another atom hierarchy containing the meta data and any images.</p>
<p>I managed to decode all this without too much trouble if the meta data atom already existed.  But I also wanted to allow people to add a new cover art image in Chapter Master, and fill in any meta data, if it didn&#8217;t already exist.  This meant that I had to create my own &#8216;fake&#8217; meta data atom and trick iTunes into recognizing it.  This proved considerably trickier than I had anticipated, but I think I have managed it.  It took a lot of careful working with a hexadecimal editor (I use the excellent <a href="http://www.hexworkshop.com/">Hex Workshop</a>), lots of trial and error, and lots of thinking.  Anyway, it looks like I got there.</p>
<p><img src="http://rightwordsoft.com/img/iTunesSample.png" alt="Books in iTunes" /></p>
<p>One trick I will pass on to others is that it seems that iTunes expects the udta meta data atom to be the LAST atom inside the &#8216;moov&#8217; (or &#8216;movie&#8217;) atom.  If it&#8217;s not in that location, it&#8217;s ignored.</p>
<p>Other features I&#8217;ve added include the ability to add titles to an image, the ability to do sophisticated search/replace on chapter text, and multi-selection ability in the chapter list.</p>
<p>I&#8217;ll be releasing the new version of Chapter Master (1.2.0) as a beta for a while, as so much has changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/06/29/programming-journal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Little-Ease</title>
		<link>http://rightwordsoft.com/blogs/2009/05/31/little-ease/</link>
		<comments>http://rightwordsoft.com/blogs/2009/05/31/little-ease/#comments</comments>
		<pubDate>Sun, 31 May 2009 00:28:27 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[pain]]></category>
		<category><![CDATA[physiotherapy]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=318</guid>
		<description><![CDATA[Here one can neither stand nor lie nor sit&#8230; &#8211; T.S.Elliot, The Wasteland Mediaeval dungeons often had a cell called the &#8220;little-ease&#8221;, devilishly designed so that the prisoner was unable to find any comfortable position in which to rest. I feel like I&#8217;m in such a place now. I&#8217;ve &#8220;done my back&#8221; &#8211; the penalty [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Here one can neither stand nor lie nor sit&#8230;<br />
&#8211; T.S.Elliot, <em>The Wasteland</em>
</p></blockquote>
<p>Mediaeval dungeons often had a cell called the &#8220;little-ease&#8221;, devilishly designed so that the prisoner was unable to find any comfortable position in which to rest.</p>
<p>I feel like I&#8217;m in such a place now.  I&#8217;ve &#8220;done my back&#8221; &#8211; the penalty of years of sitting badly while I program, I fear.  I&#8217;ve damaged something in my lower back and for the last four weeks I&#8217;ve been in some pain and often find myself in a situation where I can &#8220;neither stand nor lie nor sit&#8221;.  Which makes it hard to write software or to blog, or &#8230; well, do most things.  It&#8217;s a nuisance.</p>
<p>Anyway, my physiotherapist and doctor are both working on the problem for me.  The key, it seems, is to keep moving at all costs.  And regularly do targeted exercises to build up both abdominal and back muscles.</p>
<p>The moral of this lesson to younger programmers (I&#8217;m nearly 58) is &#8211; be very careful how you sit at the computer.  </p>
<p>If you slouch and don&#8217;t pay attention to how straight your back is, you may not suffer now, but in years to come you may well be as broken as I am.  <a href="http://www.codinghorror.com/blog/archives/001146.html">Get a good chair</a> and sit properly.  When your teachers told you to &#8220;sit up straight&#8221; they had your best interests at heart!</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/05/31/little-ease/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are You Paranoid Enough?</title>
		<link>http://rightwordsoft.com/blogs/2009/05/01/are-you-paranoid-enough/</link>
		<comments>http://rightwordsoft.com/blogs/2009/05/01/are-you-paranoid-enough/#comments</comments>
		<pubDate>Fri, 01 May 2009 04:35:23 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Digital Life]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[source control]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=205</guid>
		<description><![CDATA[When it comes to preserving your valuable data, it pays to be paranoid. Assume the worst, think of all the possible ways things could go wrong. Be paranoid. The question is not whether you are paranoid, but whether you are paranoid enough. These days, everyone has valuable data, and it&#8217;s taking up more and more [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rightwordsoft.com/img/iStock_000001398345.png" alt="(Image from iStockPhoto)" align="right"/><br />
When it comes to preserving your valuable data, it pays to be paranoid.</p>
<p>Assume the worst, think of all the possible ways things could go wrong.  Be paranoid.  The question is not whether you are paranoid, but whether you are paranoid enough.</p>
<p>These days, <em>everyone</em> has valuable data, and it&#8217;s taking up more and more room.  Documents, spreadsheets and databases; emails, photographs and even video.  Game players have many save files capturing their progress in various games.  In my case and that of most programmers I also have many files and folders comprising highly valuable program source code.  We&#8217;re all working hard generating digital files of one form or another, almost every day.</p>
<p>So, think about how you would feel if you lost that data.  Bad?  It could be worse than bad.</p>
<p>About ten years ago the young programmers in my office were having fun replaying an audio file garnered off the Internet, from some computer company&#8217;s help-line.  It was a call from some guy who had been writing a book on his computer, maybe a novel.  Something went wrong with the machine and he sent it in for repair.  The computer company had (for whatever reason) wiped the hard disk.  The young guys in my office thought it was hugely amusing to hear this guy raging in despair about the loss of all of his data, almost literally foaming at the mouth.  Personally, I found it very painful to listen to.  I could feel his pain.</p>
<p>Losing data, particularly creative work you have labored over, is tragic.  This is why I am paranoid about backups.</p>
<p>Here are my paranoid rules:</p>
<ul>
<li>If there&#8217;s only one copy of something, it might as well not exist at all, you&#8217;re going to lose it.  Make an immediate copy of any irreplaceable item (such as photographs of a wedding, which can&#8217;t be replaced at all).<br />
 </li>
<li>If you only have two copies of something, each copy must be kept in a different physical location.  The house or office could burn down (or, as happened to my daughter and her husband, the backup drive can be stolen along with the laptop it was backing up).<br />
 </li>
<li>Two copies is not enough.  Your hard drive could crash AND the backup copy could be lost or prove corrupt.  I&#8217;ve had it happen.<br />
 </li>
<li>How do you know your backup files aren&#8217;t corrupt?  Check them regularly.<br />
 </li>
<li>Are you backing up <em>everything</em> you might want?  I regularly (say once very 18 months) reformat my desktop computer.  That&#8217;s when I remember all the things I should have backed up but could easily forget to: fonts, passwords, email archives, application settings.<br />
 </li>
<li>What about backups of material not on your desktop computer?  Like on-line blogs, forum posts, etc.  A colleague of mine forgot to renew his domain name and hosting package and lost a valuable travel blog and photographs.<br />
 </li>
<li>Think about how much time you would be prepared to put in to re-create your work if it was lost.  In other words, how much work could you bear to lose?  A day&#8217;s worth?  A week&#8217;s worth?  Backup <strong>more</strong> often than that.<br />
 </li>
<li>There&#8217;s no such thing as too many copies if the data is really valuable.</li>
</ul>
<p>So here&#8217;s my backup strategy.  Personally, I don&#8217;t yet think it&#8217;s paranoid enough.</p>
<ul>
<li>For program source code, I use a version control system (<a href="http://subversion.tigris.org/">Subversion</a>) to save progressive versions, each time I do significant work on any programming project.  The commit happens via a VPN, to a server in a different physical location to my desktop &#8211; in fact, to an office some 20 km away.  This generally happens once or twice a day on a current project.  As Jeff Attwood says, <a href="http://www.codinghorror.com/blog/archives/000686.html">source control is the absolute bedrock of software engineering</a>.<br />
 </li>
<li>I have an external network drive (a <a href="http://www.lacie.com/products/product.htm?pid=10844">500GB LaCie Ethernet Mini</a>).   I use <a href="http://www.acronis.com/homecomputing/download/trueimage/">Acronis</a> to schedule weekly backups of &#8216;My Documents&#8217; and source code folders, on an incremental basis (this means I can backtrack, <em>à la</em> Mac Time Machine, to previous versions of things).<br />
 </li>
<li>I copy the Acronis backup files from the LaCie to one of two identical external USB drives (<a href="http://www.wdc.com/en/products/index.asp?cat=8">Western Digital MyBooks</a>).  Only one of these is kept at my house.  On an approximately weekly basis, I take the current MyBook drive with me to another physical location, and swap it with the identical drive already there.  Even in my house, the MyBook drive doesn&#8217;t live on my desk, but somewhere else in the house where a thief won&#8217;t find it.<br />
 </li>
<li>When I&#8217;m feeling particularly vulnerable, I burn DVD-ROMs of really critical files and keep them somewhere else.</li>
</ul>
<p>You might quite reasonably ask why I don&#8217;t use &#8216;cloud storage&#8217; to backup files to a location on the Internet, like Amazon&#8217;s S3.  The answer is that in Australia our broadband upload speeds are still so feeble that it would take weeks to backup any significant volume of data this way.</p>
<p>So am I paranoid enough?  I guess I&#8217;ll only find out when things go wrong.</p>
<blockquote><p>&#8220;Even paranoids have enemies&#8221;<br />
&#8212; Golda Meir to Henry Kissinger</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/05/01/are-you-paranoid-enough/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lazy People Make Progress</title>
		<link>http://rightwordsoft.com/blogs/2009/04/22/lazy-people-make-progress/</link>
		<comments>http://rightwordsoft.com/blogs/2009/04/22/lazy-people-make-progress/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 03:29:36 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[compulsive programming]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=196</guid>
		<description><![CDATA[Progress isn&#8217;t made by early risers. It&#8217;s made by lazy men trying to find easier ways to do something. -Robert Heinlein This came up today on the &#8216;Quote of the Day&#8217; box on my iGoogle page. It strikes me as being very true, and very applicable to many software developers. It&#8217;s not that we don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Progress isn&#8217;t made by early risers. It&#8217;s made by lazy men trying to find easier ways to do something.</p>
<p>  <em>-Robert Heinlein</em></p></blockquote>
<p>This came up today on the &#8216;Quote of the Day&#8217; box on my iGoogle page.  </p>
<p>It strikes me as being very true, and very applicable to many software developers.  It&#8217;s not that we don&#8217;t work very hard; but it&#8217;s that that we&#8217;re so lazy that we will often work very hard to save ourselves the effort of having to work on stuff we hate doing.</p>
<p>Someone, like me, who is a compulsive programmer, would much rather spend an hour writing code to automate a boring task than spend half an hour actually doing the task manually.  <strong>Even if we&#8217;re only ever going to have to perform the task once!</strong></p>
<p>There&#8217;s some great stuff about compulsive programmers in <em><a href="http://www.amazon.com/Computer-Power-Reason-Joseph-Weizenbaum/dp/0140225358/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1240370356&#038;sr=1-1">Computer Power and Human Reason</a></em> by Joseph Weizenbaum, written way back in 1976, but full of very useful insights into today&#8217;s world even so.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/04/22/lazy-people-make-progress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>It&#8217;s Optional</title>
		<link>http://rightwordsoft.com/blogs/2009/04/19/its-optional/</link>
		<comments>http://rightwordsoft.com/blogs/2009/04/19/its-optional/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 06:06:05 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=173</guid>
		<description><![CDATA[What&#8217;s the best way to store and retrieve optional settings or preferences for users for your C# application? For simple stuff, there&#8217;s no easier way than creating settings in Visual Studio: Note that you can use settings for both simple values like bool and string, and for more complex values like colors. These settings get [...]]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s the best way to store and retrieve optional settings or preferences for users for your C# application?  For simple stuff, there&#8217;s no easier way than creating settings in Visual Studio:</p>
<p><img src="http://rightwordsoft.com/img/2009-04-19_1458.png" alt="Settings dialog" /></p>
<p>Note that you can use settings for both simple values like bool and string, and for more complex values like colors.</p>
<p>These settings get persisted in a file called [your program name].exe.config.</p>
<p>And you can refer to them in code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetBackgroundColor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
         <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackColor</span> <span style="color: #008000;">=</span> Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Settings</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackgroundColor</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetBackgroundColor<span style="color: #008000;">&#40;</span>Color backcolor<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
          Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Settings</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackgroundColor</span> <span style="color: #008000;">=</span> backcolor<span style="color: #008000;">;</span>
          Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Settings</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all very well, simple, and very useful.  But it gets messy if you want to set up a lot of options and particularly if you need options using your own enums, in various categories or for different states or objects in your application &#8211; the Settings dialog is just a very simple, line by line set of options with no structure.</p>
<p>My own preference* is to create one or more Options classes, and to use the nifty <a href="http://rightwordsoft.com/blogs/2009/03/18/in-praise-of-xml-serialization/">Xml Serialization stuff</a> which I talked about previously on this blog to load and save the class.</p>
<p>Here&#8217;s a considerably cut-down version of my Options class from my software <a href="http://rightwordsoft/products/chaptermaster">Chapter Master</a>:</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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Options
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> ImageGrabOptions
	<span style="color: #008000;">&#123;</span>
	    None,
	    CropFromTop,
	    CropFromLeft,
	    CropFromRight,
	    CropFromBottom,
	    CropFromCentre,
	    Pad
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> SaveOptions
	<span style="color: #008000;">&#123;</span>
	    None,
	    RenameOriginal,
	    PromptForNew
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> ImageGrabOptions ImageGrab <span style="color: #008000;">=</span> ImageGrabOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">CropFromTop</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> SaveOptions SaveOption <span style="color: #008000;">=</span> SaveOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">PromptForNew</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> PadColorAsInt <span style="color: #008000;">=</span> Color<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToArgb</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlIgnore</span><span style="color: #008000;">&#93;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> Color PadColor
	<span style="color: #008000;">&#123;</span>
	    get <span style="color: #008000;">&#123;</span>
	        <span style="color: #0600FF; font-weight: bold;">return</span> Color<span style="color: #008000;">.</span><span style="color: #0000FF;">FromArgb</span><span style="color: #008000;">&#40;</span>PadColorAsInt<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	        <span style="color: #008000;">&#125;</span>
&nbsp;
	    set <span style="color: #008000;">&#123;</span>
	        PadColorAsInt <span style="color: #008000;">=</span> value<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArgb</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	    <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> Options<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Save<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> filename<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	    <span style="color: #0600FF; font-weight: bold;">return</span> Seralize<span style="color: #008000;">.</span><span style="color: #0000FF;">ToXmlFile</span><span style="color: #008000;">&#40;</span>filename, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Options Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> filename<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	    <span style="color: #0600FF; font-weight: bold;">return</span> opts <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Options<span style="color: #008000;">&#41;</span> Seralize<span style="color: #008000;">.</span><span style="color: #0000FF;">FromXmlFile</span><span style="color: #008000;">&#40;</span>filename, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Options<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Where Serialize is a class containing just the Xml serialization routines I wrote based on <a href="http://www.codeproject.com/KB/XML/Serialization_Samples.aspx">this Code Project article</a>.</p>
<p>Note the way that the Color is serialized in lines 27-40, the same sort of workaround as is needed for TimeSpan values.</p>
<p>I like this approach because among other things, it allows different sets of preferences to saved and loaded as required.</p>
<p>Accessing these option values is very simple (this code is a fudge written just to demonstrate my general approach):</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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span> MySettingsFolder <span style="color: #008000;">=</span> 
      <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Environment</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetFolderPath</span><span style="color: #008000;">&#40;</span>Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">SpecialFolder</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MyDocuments</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\ProgramName&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> OptionClass MyOptions<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetOptions<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     MyOptions <span style="color: #008000;">=</span> Options<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>MySettingsFolder  <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\Default Options.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//....and thus code like:</span>
&nbsp;
picCover<span style="color: #008000;">.</span><span style="color: #0000FF;">BackColor</span> <span style="color: #008000;">=</span> MyOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">PadColor</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>MyOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">ImageGrab</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CropFromBottom</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//........</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SaveSpecialOptions<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> MyOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>MySettingsFolder  <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\Special Options.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>* I&#8217;m happy to have others tell me why there are better ways.  As always, I certainly don&#8217;t claim to be any kind of expert. This is just the way <strong>I</strong> like to do it.  If you think it&#8217;s dumb, tell me so.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/04/19/its-optional/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In praise of XML serialization</title>
		<link>http://rightwordsoft.com/blogs/2009/03/18/in-praise-of-xml-serialization/</link>
		<comments>http://rightwordsoft.com/blogs/2009/03/18/in-praise-of-xml-serialization/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 01:00:14 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://rightwordsoft.com/blogs/?p=15</guid>
		<description><![CDATA[I know that Jeff Atwood over at Coding Horror isn&#8217;t very fond of XML, but personally, I have grown to love it. Well, not XML as such, but the ease of serializing and de-serializing C# classes to XML and back provided by Visual Studio. Jeff&#8217;s complaints seem to be all about the difficulty that humans [...]]]></description>
			<content:encoded><![CDATA[<p>I know that Jeff Atwood over at Coding Horror <a href="http://www.codinghorror.com/blog/archives/001114.html">isn&#8217;t very fond of XML</a>, but personally, I have grown to love it.  Well, not XML as such, but the ease of serializing and de-serializing C# classes to XML and back provided by Visual Studio.</p>
<p>Jeff&#8217;s complaints seem to be all about the difficulty that humans (well, programmers, anyway) have in reading all those angle brackets.  To me, this seems to be beside the point, since humans aren&#8217;t really supposed to be reading XML anyway &#8211; computers are.  It&#8217;s just a nice side-benefit that we programmers can look at XML files with a text editor and generally get a pretty good sense of what is going on.</p>
<p>Anyway, what I love about serializing to XML is the way that in C# you can build up a very complex object, with nested lists of diverse objects and such, and yet just blatt it all out to a file, or send it by MSMQ, and just magically read it back and have that fully complex object back again, without any need to spend time coding to parse it all in or out.</p>
<p>For example, in one of the applications I built for <a href="http://www.kaleidio.com.au">Kaleidio</a>, we are simulating electronic transactions between companies.  So I have a class called &#8216;Company&#8217;, name, address, etc, as you would expect it; but among other complexities, the company class also includes an array of storage locations.  Each storage location has a list of products being stored, each with its own information about price, unit size, bar-code, etc.  Obviously, these are &#8216;toy&#8217; companies with far less complexity than the real thing; but nevertheless, they are complex objects.</p>
<p>With serialization, I can just set up and save an entire company object to disk, or pick it up and despatch it to another computer via MSMQ (or SOAP, or whatever).</p>
<p>There&#8217;s a great article with some really good utility routines for reading and writing serialized XML <a href="http://www.codeproject.com/KB/XML/Serialization_Samples.aspx">here at Code Project</a>.  If you base your code on these routines, you can write code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	var myCompany <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Company<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">//fill in company details, add storage locations, </span>
	<span style="color: #008080; font-style: italic;">//fill locations with products, etc.</span>
&nbsp;
	ToXMLfile<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\temp\company.xml&quot;</span>, myCompany<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">//then, later:</span>
&nbsp;
	var loadedCompany <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Company<span style="color: #008000;">&#41;</span>FromXMLfile<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\temp\company.xml&quot;</span>, 
						<span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Company<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">//now work with the company again.</span></pre></div></div>

<p>To make this all work well you generally have to assign attributes to members of your classes to give the serializer hints as to how you want the XML to work.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Company
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MyBusiness&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Address<span style="color: #008000;">=</span> <span style="color: #666666;">&quot;41 Smith Street, Collingwood, Vic 3066&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//....</span></pre></div></div>

<p>This creates XML like:</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;Company</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;MyBusiness&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>41 Smith Street, Collingwood, Vic 3066<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		....
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Company<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p> <br />
I have used this a lot in the last 6 or 7 years, but I have to admit that it&#8217;s a feature of Visual Studio that I have basically taken for granted, and only rarely had to worry about the implementation.  In other words, I haven&#8217;t really researched it and thought about it in any detail, just used it at a basic level.  I&#8217;m guessing that most practicing programmers use many such language features in the same way.</p>
<p>But it&#8217;s when what you have taken for granted suddenly causes you grief that you have to start hitting the F1 key (or, more usefully, googling) to get some answers.</p>
<p>For example, there are some kinds of data type which you can&#8217;t serialize directly, such as TimeSpan.  So you have to use a workaround like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">long</span> DurationTicks<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlIgnore</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> TimeSpan Duration
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> TimeSpan<span style="color: #008000;">&#40;</span>DurationTicks<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set
            <span style="color: #008000;">&#123;</span>
                DurationTicks <span style="color: #008000;">=</span> value<span style="color: #008000;">.</span><span style="color: #0000FF;">Ticks</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note that one of the disadvantages of serialization is that any value to be serialized must be marked as <em>public</em>, so DurationTicks above is visible as a member of the class to which it belongs, even though you don&#8217;t really want it messed with directly.</p>
<p>Where I have gotten myself into difficulties in the past is where you want a class to be able to have a member which may be of various different kinds of object.  They are different, but they do all inherit from a base class.</p>
<p>You can use a construct like this, which uses an additional class member to indicate to the serializer the type of object being included:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlChoiceIdentifier</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ControlType&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span>Type <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>VideoControl<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span>Type <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ImageControl<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span><span style="color: #008000;">&#40;</span>Type <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FlashControl<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Control ControlObj <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Identifies the type of control being sent.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlIgnore</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> ControlTypes ControlType <span style="color: #008000;">=</span> ControlTypes<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">;</span></pre></div></div>

<p>Note the XmlIgnore tag, which tells the serializer not to explictly include this property in the XML.</p>
<p>But I struggled for a while to figure out what to do when the class I was trying to serialize had a member which was a <strong>list</strong> of such variable objects.</p>
<p>For example in one of my <a href="http://rightwordsoft.com/products/markable">shareware projects</a>, I have an object which includes such a list (a List&lt;FileRecord&gt;, to be specific).  Mostly this list contains plain FileRecord objects, but I also have a class called CDTrackRecord which inherits from FileRecord.  For quite a while I couldn&#8217;t figure out how to correctly serialize a class with a list which included one or more such inherited objects.</p>
<p>I&#8217;m still not sure if this is the best solution, but I discovered that you can use an XmlInclude attribute on the base class of list members, like this, to show it also includes the possibility of the inherited object:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlInclude</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>CDTrackRecord<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FileRecord
        <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//class members</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The class including a List&lt;FileRecord&gt; now serializes nicely.</p>
<p><strong>Disclaimer:</strong> I don&#8217;t claim to be any kind of programming expert, just a dumb old programmer with a passion for coding.  Bear this in mind if you comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://rightwordsoft.com/blogs/2009/03/18/in-praise-of-xml-serialization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: rightwordsoft.com @ 2012-05-18 19:18:18 by W3 Total Cache -->
