<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Bill Sithiro</title>
	<atom:link href="http://sithiro.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sithiro.wordpress.com</link>
	<description>var r = new Random();</description>
	<lastBuildDate>Sat, 24 Sep 2011 22:33:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sithiro.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/5f6f08ff92a96a84151ebedd20994d01?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Bill Sithiro</title>
		<link>http://sithiro.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sithiro.wordpress.com/osd.xml" title="Bill Sithiro" />
	<atom:link rel='hub' href='http://sithiro.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Silverlight Snake (Nibbler Clone) 2009</title>
		<link>http://sithiro.wordpress.com/2009/04/18/silverlight-snake-nibbler-clone-2009/</link>
		<comments>http://sithiro.wordpress.com/2009/04/18/silverlight-snake-nibbler-clone-2009/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 10:30:33 +0000</pubDate>
		<dc:creator>Bill Sithiro</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Silverlight 2]]></category>
		<category><![CDATA[Control Templates]]></category>
		<category><![CDATA[Nibbler]]></category>

		<guid isPermaLink="false">http://sithiro.wordpress.com/2009/04/18/silverlight-snake-nibbler-clone-2009/</guid>
		<description><![CDATA[Here’s a game I wrote in Silverlight 2.0 that is a clone of the old-school classic Nibbler. If you never played or heard of Nibbler, you’re either too young; as it was released in 1982, or you never hanged out in many arcades as a child. Nibbler was originally the first game to feature a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=164&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://silverarcade.com/games/sithiro/snake2009" target="_blank"><img style="display:inline;border-width:0;margin:0 10px 5px 0;" title="snake2009" src="http://sithiro.files.wordpress.com/2009/04/snake2009.png?w=418&#038;h=425&#038;h=425" border="0" alt="snake2009" width="418" height="425" align="left" /></a></p>
<p>Here’s a game I wrote in Silverlight 2.0 that is a clone of the old-school classic Nibbler. If you never played or heard of Nibbler, you’re either too young; as it was released in 1982, or you never hanged out in many arcades as a child.</p>
<p>Nibbler was originally the first game to feature a billion point scoring system, which at the time I guess was even more important than graphics. In fact, I believe someone did manage to reach the billion point after playing the game straight for more than 48 hours!</p>
<p>For a long time, I pondered on the game design and mechanics of Nibbler; you know it isn’t the best game out there (in the eyes of most) it’s rather dull. However, it is certainly the fastest game you’ll ever experience. I can’t think of any other game that allows you to complete a level in 2 or 3 seconds! Yes, it gets that fast! There’s only a handful of levels that repeat over and over yet all of us (me and my friends) just wanted to play the game more and more, as if we suffered from an OCD to play this game until we dropped. And sometimes we would, after a dozen hours or so. <img class="wp-smiley" src="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" /></p>
<p>But the interesting thing about Nibbler is it allowed the dedicated soul to play the game for endless hours. Once you figured out the patterns for each maze, you could clear the maze with your eyes closed, literally. But be a split millisecond too slow and you would crash hard!</p>
<p>My attempt to recreate the game is not so much an attempt to deliver a faithful representation of the game, but to allow you to play it (if you should so decide to do so) for hours upon hours, or days even, and make it as fast as the original. That’s easier said then done, considering the browser containment, performance, memory leaks and constrained only to keyboard input as opposed to an arcade style joystick. So at least, that’s my goal.</p>
<p>My first impression is that I think it might be finally possible with Silverlight, but it’s going to be tricky. Note, the entire game is XAML based and designed to play in a 1000×1000 pixel canvas! So the fidelity of the game is quite… HD and therefore performance issues lurking. There’s also some crazy cool control template stuff going on in regards to the levels. I might however start looking into a raster based version in Silverlight 3 when it’s released.</p>
<p>Well anyway, you can checkout and play the game over at <a href="http://silverarcade.com/games/sithiro/snake2009" target="_blank">SilverArcade.com</a><a href="http://silverarcade.com/games/sithiro/snake2009" target="_blank"></a>.</p>
<p>Thanks,</p>
<p>–Bill Sithiro</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sithiro.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sithiro.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sithiro.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sithiro.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sithiro.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sithiro.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sithiro.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sithiro.wordpress.com/164/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=164&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sithiro.wordpress.com/2009/04/18/silverlight-snake-nibbler-clone-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">billy</media:title>
		</media:content>

		<media:content url="http://sithiro.files.wordpress.com/2009/04/snake2009.png?w=418&#38;h=425" medium="image">
			<media:title type="html">snake2009</media:title>
		</media:content>

		<media:content url="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif" medium="image">
			<media:title type="html">:)</media:title>
		</media:content>
	</item>
		<item>
		<title>Frog Puzzle</title>
		<link>http://sithiro.wordpress.com/2009/03/18/frog-puzzle/</link>
		<comments>http://sithiro.wordpress.com/2009/03/18/frog-puzzle/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 15:53:56 +0000</pubDate>
		<dc:creator>Bill Sithiro</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Silverlight 2]]></category>
		<category><![CDATA[Frog Puzzle]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://sithiro.wordpress.com/2009/03/18/frog-puzzle/</guid>
		<description><![CDATA[Have you ever found yourself with no Internet connection, no computer, no TV, or anything to entertain yourself with after a hard day’s work? I caught myself at such a time once (luckily, only for just a week) and my only amusement was a little puzzle game I managed to pickup for just a dollar [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=158&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever found yourself with no Internet connection, no computer, no TV, or anything to entertain yourself with after a hard day’s work? I caught myself at such a time once (luckily, only for just a week) and my only amusement was a little puzzle game I managed to pickup for just a dollar or two in a remote oddities store. The game was simple; it had no instructions and consisted of just 9 pieces. Your goal was to place the pieces as such so as the frogs depicted were all lined up. Seems simple right? Boy, I tell you, did I had a hard time solving it! Anyway, I took a picture with my mobile phone once I solved it (see bellow) so I could remember the solution next time I would play it. Unfortunately, I lost the puzzle and all I had to remind me of it was this picture. Fortunately, I had some spare time in hands recently, so I decided to recreate the puzzle game in Silverlight 2. And this time, it was just as fun making it as it was playing it. <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><a href="http://silverarcade.com/games/sithiro/frog-puzzle" target="_blank">Click here to play the game.</a></p>
<div id="scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:56059b5d-e67f-4b0c-9113-fdecdef42b7a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><img src="http://sithiro.files.wordpress.com/2009/03/13082007263.png?w=700" border="0" alt="" /></div>
<p>Odd, isn&#8217;t it?</p>
<p>The original game took me a couple of hours to solve. In my version, I’ve included a little hint to help you solve it quicker. See if you can pick up on it.</p>
<p>Thank you,</p>
<p>&#8211;Bill Sithiro</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sithiro.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sithiro.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sithiro.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sithiro.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sithiro.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sithiro.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sithiro.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sithiro.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=158&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sithiro.wordpress.com/2009/03/18/frog-puzzle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">billy</media:title>
		</media:content>

		<media:content url="http://sithiro.files.wordpress.com/2009/03/13082007263.png" medium="image" />
	</item>
		<item>
		<title>Pandora’s Unbox (and conversions)</title>
		<link>http://sithiro.wordpress.com/2009/03/17/pandoras-unbox-and-conversions/</link>
		<comments>http://sithiro.wordpress.com/2009/03/17/pandoras-unbox-and-conversions/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 14:53:26 +0000</pubDate>
		<dc:creator>Bill Sithiro</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Boxing]]></category>
		<category><![CDATA[Convert]]></category>
		<category><![CDATA[IConvertible]]></category>
		<category><![CDATA[Unboxing]]></category>

		<guid isPermaLink="false">http://sithiro.wordpress.com/2009/03/17/pandoras-unbox-and-conversions/</guid>
		<description><![CDATA[As Pandora, a C#/.NET developer should be familiar with the concepts of “boxing” and “unboxing” of .NET types (although Pandora may have not been familiar with the boxing part). As a reminder take a look at the following statements: int i = 1234; object o = i; // Box System.Console.WriteLine(o.GetType()); // Will display System.Int32 int [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=150&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">As Pandora, a C#/.NET developer should be familiar with the concepts of “boxing” and “unboxing” of .NET types (although Pandora may have not been familiar with the boxing part). As a reminder take a look at the following statements:</p>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">int</span> i = 1234;
<span class="kwrd">object</span> o = i;                          </span><span style="color:#000080;"><span class="rem">// Box
</span></span><span style="color:#000080;"><span class="rem">System.Console.WriteLine(o.GetType()); // Will display System.Int32
</span><span class="kwrd">int</span> j = (<span class="kwrd">int</span>)o;                        <span class="rem">// Unbox
</span>System.Console.WriteLine(j);           // Will display 1234</span></pre>
<p align="justify">
<p align="justify">In the snippet above we’ve created and assigned a variable of type int (System.Int32) and straight after “boxed-it” into an object variable again via assignment. I believe the .NET definition of “to-box” is to convert to a reference type from a value type. Proof that indeed our object variable is now of type System.Int32 is shown. We’ve then “unboxed” it back into an int-typed variable; definition of “to-unbox” to convert from reference type to value type. Notice the necessary explicit (int) type cast.</p>
<p>Now, ponder on the following statements:</p>
<div>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">object</span> o = 1234;                       // Box an int
System.Console.WriteLine(o.GetType()); // Will display System.Int32
<span class="kwrd">short</span> h = (<span class="kwrd">short</span>)o;                    // Unbox. Throws InvalidCastException at runtime.</span></pre>
</div>
<p align="justify">
<p align="justify">What we’ve done is created and assigned implicitly a direct literal value to a variable of type System.Object. This is perfectly legal and behind the scenes the CLR will automatically box it as a System.Int32 type. We’ll print out the runtime type just to be sure. Straight after, we’ll unbox the value, however this time to a short-typed (System.Int16) variable. Surely this could be possible, especially if the value we want to assign fits into the range of the short. But although the snippet above compiles, you’ll be surprised to see a runtime exception of an InvalidCastException. However, if the offending line looked like this:</p>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">short</span> h = (<span class="kwrd">short</span>)(<span class="kwrd">int</span>)o;               // Unbox and convert?
                                       // Always explicitly unbox the runtime type first,
                                       // then cast to desired type.</span></pre>
<p>It would work perfectly! Here’s another example:</p>
<div>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">byte</span> b = 123;<span class="kwrd">object</span> o = b;             // Box a byte into the object.
System.Console.WriteLine(o.GetType()); // Will display System.Byte
<span class="kwrd">short</span> h = (<span class="kwrd">short</span>)(byte)o;              // Unbox and convert?
                                       // Always explicitly unbox the runtime type first,
                                       // then cast to desired type.</span></pre>
</div>
<p align="justify">
<p align="justify">So, although the CLR already knows the type of your boxed contents in your object-typed variables, you have to explicitly cast to the runtime type during your unboxing operations before performing any further conversions.</p>
<p align="justify">The 15-thousand dollar question would be now, what if you don’t know what the original type is? Obviously in runtime you can get to it as we’ve seen with the displaying of o.GetType(), but how do you unbox your object without explicitly defining it during code authoring? The answer is, by using utility classes like the static Convert class. With this class you can simply do:</p>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">object o = 1234;                      // Box an int
int i = Convert.ToInt32(o);           // Just unbox it.
short</span> h = Convert.ToInt16(o);         // Unbox and convert it to a short.
double d = Convert.ToDouble(o);       // Unbox and convert to a double.
string s = Convert.ToString(o);       // Unbox and convert it to a string.
</span>and so on…</pre>
<p align="justify">
<p align="justify">In fact, you can convert to just about anything without worrying about the details. The Convert class basically performs a set of unboxing teqniques like described above, however the key point is that it provides numerous overloads of the said methods and therefore the runtime automatically calls the correct one and then works its unboxing and conversion magic based on the method’s input parameter. For instance, in the above case:</p>
<div>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">object o = 1234;                           // Box an int
short</span> h = Convert.ToInt16(o);              // Will call Convert.ToInt16(object value) overload.</span></pre>
</div>
<p align="justify">
<p align="justify">You could also take advantage of simple value type’s IConvertible methods that have been provided for the reasons of converting from one type to another. So, in our case, we could also do:</p>
<pre class="csharpcode"><span style="color:#000080;"><span class="kwrd">object o = 1234;                           // Box an int
short h</span> = ((IConvertible)o).ToInt16(null); // Unbox and convert our object from whatever
                                           // it is to an Int16. Providing of course it’s
                                           // one of C#’s value types or string.</span></pre>
<p align="justify">
<p align="justify">In fact, that’s exactly what the Convert utility methods do behind the scenes when working with implicit types!</p>
<p align="justify">Now, obviously writing code like the above is not ideal. There’s so many things that could go wrong; although using Convert is much more sound as it does perform validation. However in most cases, you should not ever have to explicitly cast one type to another, especially with powerful language features like Generics and Type Inference and other OOP techniques, but from the standpoint of performance; just as importantly. To be honest, this is rather reminiscing of the Zen of early languages. However, you’ll be surprised just how many C# screening sheets today drivel down snippets like the said. It’s always useful knowing a rule of thumb… or two.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sithiro.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sithiro.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sithiro.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sithiro.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sithiro.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sithiro.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sithiro.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sithiro.wordpress.com/150/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=150&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sithiro.wordpress.com/2009/03/17/pandoras-unbox-and-conversions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">billy</media:title>
		</media:content>
	</item>
		<item>
		<title>CalcLight &#8211; The under 10kB Silverlight spreadsheet</title>
		<link>http://sithiro.wordpress.com/2009/01/31/calclight-the-under-10kb-silverlight-spreadsheet/</link>
		<comments>http://sithiro.wordpress.com/2009/01/31/calclight-the-under-10kb-silverlight-spreadsheet/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 19:48:53 +0000</pubDate>
		<dc:creator>Bill Sithiro</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Silverlight 2]]></category>
		<category><![CDATA[CalcLight]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MIX09]]></category>
		<category><![CDATA[Under 10kB Competition]]></category>

		<guid isPermaLink="false">http://sithiro.wordpress.com/2009/01/31/calclight-the-under-10kb-silverlight-spreadsheet/</guid>
		<description><![CDATA[Every year Microsoft organises a great conference named MIX that is about web design and development. It’s probably better you visit the official site for a better description, as what I want to blog about is the little competition the MIX09 organisers launched last month (December 2008). The idea behind the competition is to create [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=110&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Every year Microsoft organises a great conference named <a href="http://2009.visitmix.com/" target="_blank">MIX</a> that is about web design and development. It’s probably better you visit the <a href="http://2009.visitmix.com/" target="_blank">official site</a> for a better description, as what I want to blog about is the <a href="http://2009.visitmix.com/MIXtify/TenKGallery.aspx" target="_blank">little competition</a> the MIX09 organisers launched last month (December 2008). The idea behind the competition is to create ”something for the web” in Silverlight, WPF browser application or WPF Click Once application in under 10kB of code. As the competition is now over, I thought I would post a blog on my competition entry, <a href="http://2009.visitmix.com/MIXtify/TenKDisplay.aspx?SubmissionID=0074" target="_blank">CalcLight</a>.</p>
<p>But before I begin, let’s travel back in time to the days of the console and DOS and look at <a href="http://www.devq.net/pascal/src/microcalc.pas" target="_blank">the source code for a spreadsheet</a> written for Turbo Pascal 3. Borland used to release little samples like MicroCalc in their Pascal and C++ development IDE’s. Now, the point of this is to look at the amount of code required to handle a formula. Keep in mind, that there’s is no UI in the MicroCalc sample and you could write a better expression parser now days, but the things that could go wrong in terms of data entry still remain numerous. In fact, to do it properly just a decent parser itself would exceed the 10kB limit. So, how on Earth do you then create a fully functional spreadsheet application with a UI plus all the spreadsheet plumbing in under 10Kb? And the answer is… by taking shortcuts. Enter JavaScript!</p>
<p>When I decided to make CalcLight, my software estimation skills told me that no way I could make it handle formulas plus the UI under the contest’s size limit (although perhaps with just a few more kilobytes it might have been possible). However, I did notice that nothing was stopping me from using JavaScript to handle the expressions. In that case, all I needed to do was simply parse the formula for direct cell values and then simply pass the parsed formula to JavaScript to evaluate. Here’s some sample code to setup usage with JavaScript within Silverlight:</p>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> HtmlElement scriptHost=HtmlPage.Document.CreateElement(<span style="color:#006080;">"script"</span>);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   2:</span> scriptHost.SetAttribute(<span style="color:#006080;">"type"</span>,<span style="color:#006080;">"text/javascript"</span>);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   3:</span> scriptHost.SetProperty(<span style="color:#006080;">"text"</span>,<span style="color:#006080;">"function Evaluate(exp){try{return eval(exp);}catch(e){return "</span>#VALUE!<span style="color:#006080;">";}}"</span>);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   4:</span> HtmlPage.Document.DocumentElement.AppendChild(scriptHost);</pre>
</div>
</div>
<div>What we’ve done in the above code is inserted a JavaScript function in the DOM of the Silverlight host (probably will only work for a browser host) named Evaluate, and that which accepts a string (our formula), evaluates it through JavaScript’s eval function, and returns the value or a fixed string in case of an error.</div>
<p>Our Silverlight application is now ready to execute the Evaluate function. But we’re not quite done yet, although this would work nicely for something like this:</p>
<div>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> HtmlPage.Window.Invoke(<span style="color:#006080;">"Evaluate"</span>, <span style="color:#006080;">"((10 * 3.5) / (234 + 123 + 232.23)) * 2"</span>);</pre>
</div>
</div>
</div>
<p>However, what we’re hoping to do is also pass cell names instead of just direct values. And these cells may just as well contain other formulas pointing to other cells and so on. Things are getting a little more complicated, but nothing out of hand. The way CalcLight works, every cell is a class. The Cell class contains three methods to help us solve the above problem. Additionally, we have an instance (named Cells) of a static Dictionary&lt;String,Cell&gt; with all our cell instances to assist us:</p>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">string</span>[] symb = { <span style="color:#006080;">"="</span>, <span style="color:#006080;">"+"</span>, <span style="color:#006080;">"-"</span>, <span style="color:#006080;">"/"</span>, <span style="color:#006080;">"*"</span>, <span style="color:#006080;">"("</span>, <span style="color:#006080;">")"</span>, <span style="color:#006080;">","</span>, <span style="color:#006080;">"?"</span>, <span style="color:#006080;">":"</span>, <span style="color:#006080;">"&lt;"</span>, <span style="color:#006080;">"&gt;"</span> };</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   2:</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">string</span> Parse(<span style="color:#0000ff;">string</span> x)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   4:</span> {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   5:</span>     x = x.Replace(<span style="color:#006080;">" "</span>, <span style="color:#006080;">""</span>);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   6:</span>     var arr = x.Split(symb, StringSplitOptions.RemoveEmptyEntries);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   7:</span>     var d = from i <span style="color:#0000ff;">in</span> arr orderby i descending select i;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">foreach</span>(var item <span style="color:#0000ff;">in</span> d)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   9:</span>     {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  10:</span>         var v = Regex.Replace(item, <span style="color:#006080;">"^[A-Z]{1,2}[0-9]{1,3}$"</span>, Cell.GetValue(item, <span style="color:#0000ff;">this</span>), RegexOptions.None);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  11:</span>         x = x.Replace(item, v);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  12:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  13:</span>     <span style="color:#0000ff;">return</span> x;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  14:</span> }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  15:</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  16:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> GetValue(<span style="color:#0000ff;">string</span> cid, Cell cell)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  17:</span> {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">if</span> (Cells.ContainsKey(cid))</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  19:</span>     {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  20:</span>         var c = Cells[cid];</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  21:</span>         <span style="color:#0000ff;">if</span> (c != <span style="color:#0000ff;">null</span>)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  22:</span>         {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  23:</span>             <span style="color:#0000ff;">if</span> (c != cell)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  24:</span>                 <span style="color:#0000ff;">return</span> c.Evaluate(c.Text);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  25:</span>             <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  26:</span>                 <span style="color:#0000ff;">return</span> <span style="color:#006080;">"!CIRCULAR!"</span>;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  27:</span>         }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  28:</span>         <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  29:</span>             <span style="color:#0000ff;">return</span> cid;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  30:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  31:</span>     <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  32:</span>         <span style="color:#0000ff;">return</span> cid;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  33:</span> }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  34:</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  35:</span> <span style="color:#0000ff;">string</span> Evaluate(<span style="color:#0000ff;">string</span> x)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  36:</span> {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  37:</span>     <span style="color:#0000ff;">if</span> (x.StartsWith(<span style="color:#006080;">"="</span>))</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  38:</span>     {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  39:</span>         <span style="color:#0000ff;">try</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  40:</span>         {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  41:</span>             <span style="color:#0000ff;">return</span> HtmlPage.Window.Invoke(<span style="color:#006080;">"Evaluate"</span>, Parse(x).Replace(<span style="color:#006080;">"="</span>, <span style="color:#006080;">""</span>)).ToString();</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  42:</span>         }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  43:</span>         <span style="color:#0000ff;">catch</span> (Exception)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  44:</span>         {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  45:</span>             <span style="color:#0000ff;">return</span> <span style="color:#006080;">"#VALUE!"</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  46:</span>         }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  47:</span>     }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  48:</span>     <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  49:</span>         <span style="color:#0000ff;">return</span> x;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  50:</span> }</pre>
</div>
</div>
<p>The entry point of the above snippet is the Evaluate function. We pass a formula to the Evaluate function (e.g. =A1+B1) which in turn calls Parse with the formula and will enter a state of recursion for each cell in the formula until the entire formula(s) are evaluated. Notice, we also check for situations where a cell references itself . This is known as a circular reference. However, there is also the case of one cell reference another that in turn references the first. This is also a circular reference. To make a long story short it results in a stack overflow, something not handled very gracefully in Silverlight and when I first tested for that, it crashed every single browser I checked it with! Luckily, we can fix this easily, and you can use this technique elsewhere as well. But we need to make some slight modifications:</p>
<p>First, we need a static variable to keep count of how many iterations the GetValue method is being called. We’ll just call it ITR for iteration:</p>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span> ITR = 0;</pre>
</div>
</div>
<p>Next, we need to re-initialise it in the exit point (finally clause) of the Evaluate method:</p>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">string</span> Evaluate(<span style="color:#0000ff;">string</span> x)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">if</span> (x.StartsWith(<span style="color:#006080;">"="</span>))</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   4:</span>     {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   5:</span>         <span style="color:#0000ff;">try</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   6:</span>         {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   7:</span>             <span style="color:#0000ff;">return</span> HtmlPage.Window.Invoke(<span style="color:#006080;">"Evaluate"</span>, Parse(x).Replace(<span style="color:#006080;">"="</span>, <span style="color:#006080;">""</span>)).ToString();</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   8:</span>         }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   9:</span>         <span style="color:#0000ff;">catch</span> (Exception)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  10:</span>         {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  11:</span>             <span style="color:#0000ff;">return</span> <span style="color:#006080;">"#VALUE!"</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  12:</span>         }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  13:</span>         <span style="color:#0000ff;">finally</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  14:</span>         {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  15:</span>             ITR = 0;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  16:</span>         }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  17:</span>     }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  19:</span>         <span style="color:#0000ff;">return</span> x;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  20:</span> }</pre>
</div>
</div>
<p>Secondly, we’re incrementing the counter every time GetValue is called.</p>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> GetValue(<span style="color:#0000ff;">string</span> cid, Cell cell)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   3:</span>     ITR++;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   4:</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">if</span> (Cells.ContainsKey(cid))</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   6:</span>     {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   7:</span>         var c = Cells[cid];</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   8:</span>         <span style="color:#0000ff;">if</span> (c != <span style="color:#0000ff;">null</span>)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   9:</span>         {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  10:</span>             <span style="color:#0000ff;">if</span> (c != cell)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  11:</span>                 <span style="color:#0000ff;">return</span> c.Evaluate(c.Text);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  12:</span>             <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  13:</span>                 <span style="color:#0000ff;">return</span> <span style="color:#006080;">"!CIRCULAR!"</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  14:</span>         }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  15:</span>         <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  16:</span>             <span style="color:#0000ff;">return</span> cid;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  17:</span>     }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  19:</span>         <span style="color:#0000ff;">return</span> cid;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  20:</span> }</pre>
</div>
</div>
<p>Now, all we need is a switch that will make sense as an indication that we’re ‘cruising for a bruising’ (stack overflow). We do this in the Parse function and we simply add the following line:</p>
<div>
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">string</span> Parse(<span style="color:#0000ff;">string</span> x)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">if</span> (ITR &gt; 3000)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   4:</span>     {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   5:</span>         ITR=0;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   6:</span>         <span style="color:#0000ff;">return</span> <span style="color:#006080;">"!OVERFLOW!"</span>;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   7:</span>     }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">   8:</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">   9:</span>     x = x.Replace(<span style="color:#006080;">" "</span>, <span style="color:#006080;">""</span>);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  10:</span>     var arr = x.Split(symb, StringSplitOptions.RemoveEmptyEntries);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  11:</span>     var d = from i <span style="color:#0000ff;">in</span> arr orderby i descending select i;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">foreach</span>(var item <span style="color:#0000ff;">in</span> d)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  13:</span>     {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  14:</span>         var v = Regex.Replace(item,<span style="color:#006080;">"^[A-Z]{1,2}[0-9]{1,3}$"</span>, Cell.GetValue(item, <span style="color:#0000ff;">this</span>), RegexOptions.None);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  15:</span>         x = x.Replace(item, v);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  16:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#606060;">  17:</span>     <span style="color:#0000ff;">return</span> x;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#606060;">  18:</span> }</pre>
</div>
</div>
<p>Basically, if our counter has exceeded 3000 iterations it’s time to call it quits and return an error. Now, 3000 is just a guess, it could work just as well with 5 or maybe 10 thousand iterations. I’m not sure, I haven’t really tested it to that detail, but do note however that the larger this number is the larger the amount of formulas you can resolve i.e. recursion deep resolving (e.g. cell 1 points to cell 2, cell 2 points to cell 3 and so on). We want to keep at something practical and remember our primary goal is trying to catch circular references with the less amount of code as possible.</p>
<p>The Parse method is very interesting as this is where we do the actual formula parsing. This is how it works, we give it an expression and we strip out all the whitespace. Secondly, we split the formula from symbols and are left with an array of strings. Now these strings, maybe cell names, maybe number values, maybe function names, or simply text, but all we care about is cell names. We apply a regular expression pattern to test for exactly that, and call GetValue to evaluate the cell’s value. But what’s that LINQ statement you might ask? Why is it important to sort the array in descending order? This catches formulas where we have something like this: =B1+B11. This helps our Regex pattern matching distinguish from B1 and B11 by first evaluating the later cell first.</p>
<p>And that’s about it regarding CalcLight’s formula parsing shortcut. Use JavaScript in your SilverLight projects whenever you can; it’s easier to add functionality and avoid caching problems in browsers with .xap’s. However, also note Silverlight features like Regular Expressions and LINQ that made the formula parsing not only a breeze but in a fraction of code! Compare it with the old-school Pascal source code shown above to appreciate today’s modern coding features.</p>
<p>Finally, one feature I didn’t manage to get in the size limit, sadly, is range cells (e.g. A1:C10) but hey, it’s time to worry about the UI now. And no JavaScript is going to help us pull off the next stunt of visualising 10 thousand cells! <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><a href="http://mix10k.visitmix.com/Entry/Details/61" target="_blank">Launch CalcLight here</a></p>
<p>Thank You,</p>
<p>-Bill Sithiro</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sithiro.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sithiro.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sithiro.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sithiro.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sithiro.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sithiro.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sithiro.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sithiro.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=110&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sithiro.wordpress.com/2009/01/31/calclight-the-under-10kb-silverlight-spreadsheet/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">billy</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.NET &#8211; A Dynamic Image Server Control &#8211; Part 1</title>
		<link>http://sithiro.wordpress.com/2008/11/30/aspnet-a-dynamic-image-server-control-part-1/</link>
		<comments>http://sithiro.wordpress.com/2008/11/30/aspnet-a-dynamic-image-server-control-part-1/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 18:35:42 +0000</pubDate>
		<dc:creator>Bill Sithiro</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://sithiro.wordpress.com/2008/11/30/aspnet-a-dynamic-image-server-control-part-1/</guid>
		<description><![CDATA[As you already know if you&#8217;re an ASP.NET developer, a server control renders pure HTML that is embedded into the page the server control is hosted. But what if you wanted to display a dynamic image on the page. Since the server control renders only HTML it would have to render some &#60;img&#62; tag pointing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=78&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As you already know if you&#8217;re an ASP.NET developer, a server control renders pure HTML that is embedded into the page the server control is hosted. But what if you wanted to display a dynamic image on the page. Since the server control renders only HTML it would have to render some &lt;img&gt; tag pointing to some location on your web server or other. You could argue that instead of placing a custom made server control on the page just place an ASP.NET Image server control and target it appropriately. It could let&#8217;s say target an .aspx page that renders a dynamic image. But that just doesn&#8217;t feels right for a dynamic image. It&#8217;s not something some one can simply get out of the toolbox and drop it on the page in a nice componentized manner. Furthermore, customize it using the property editor and see how it looks even in design mode. That&#8217;s much better, but how?</p>
<p>Unfortunately, the answer is not so straightforward but for some things it can be. If you wanted to display let&#8217;s say a small image, for example a CAPTCHA, you could use the HTML URL scheme <a href="http://www.ietf.org/rfc/rfc2397.txt" target="_blank">data:[&lt;mediatype&gt;][;base64],&lt;data&gt;</a> that embeds the raw image data inside the page as Base64 characters:</p>
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> RenderContents(HtmlTextWriter output)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>  {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>      <span style="color:#0000ff;">using</span> (Bitmap bitmap = <span style="color:#0000ff;">new</span> System.Drawing.Bitmap(40, 20))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>      {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>          <span style="color:#0000ff;">using</span> (Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>          {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>              graphics.Clear(Color.Red);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>              graphics.TextRenderingHint = TextRenderingHint.AntiAlias;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>              Font font = <span style="color:#0000ff;">new</span> Font(<span style="color:#006080;">"Arial"</span>, 8, FontStyle.Bold);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>              graphics.DrawString(<span style="color:#006080;">"HeLlo"</span>, font, Brushes.White, 3, 3);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>              output.Write(<span style="color:#0000ff;">string</span>.Format(<span style="color:#006080;">"&lt;img src="data:image/png;base64,{0}"&gt;"</span>,</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>                                         ImageToBase64(bitmap, ImageFormat.Png)));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>          }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>      }                         </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>  }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>  <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">string</span> ImageToBase64(System.Drawing.Image image, ImageFormat format)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>  {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>      <span style="color:#0000ff;">using</span> (MemoryStream ms = <span style="color:#0000ff;">new</span> MemoryStream())</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>      {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>          <span style="color:#008000;">// Convert Image to byte[]</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>          image.Save(ms, format);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>          <span style="color:#0000ff;">byte</span>[] imageBytes = ms.ToArray();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>          <span style="color:#008000;">// Convert byte[] to Base64 String</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>          <span style="color:#0000ff;">return</span> Convert.ToBase64String(imageBytes);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>      }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>  }</pre>
</div>
</div>
<p>To be continued&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sithiro.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sithiro.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sithiro.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sithiro.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sithiro.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sithiro.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sithiro.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sithiro.wordpress.com/78/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=78&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sithiro.wordpress.com/2008/11/30/aspnet-a-dynamic-image-server-control-part-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">billy</media:title>
		</media:content>
	</item>
		<item>
		<title>Art of Solitaire &#8211; Silverlight 2 Card Game</title>
		<link>http://sithiro.wordpress.com/2008/11/26/art-of-solitaire-silverlight-2-game/</link>
		<comments>http://sithiro.wordpress.com/2008/11/26/art-of-solitaire-silverlight-2-game/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 01:34:45 +0000</pubDate>
		<dc:creator>Bill Sithiro</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Silverlight 2]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Solitaire]]></category>

		<guid isPermaLink="false">http://sithiro.wordpress.com/2008/11/26/art-of-solitaire-silverlight-2-game/</guid>
		<description><![CDATA[This a Hi-Lo Solitaire game I wrote way back in Silverlight 1.1 Alpha and that I recently updated to Silverlight 2. At the time of writing, Silvelright 1.1 Alpha didn&#8217;t have a way of updating the screen programmatically, and the only way to do it, was by defining an event handler of a StoryBoard.Completed event. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=64&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This a Hi-Lo Solitaire game I wrote way back in Silverlight 1.1 Alpha and that I recently updated to Silverlight 2. At the time of writing, Silvelright 1.1 Alpha didn&#8217;t have a way of updating the screen programmatically, and the only way to do it, was by defining an event handler of a StoryBoard.Completed event. I recall there was also an HtmlTimer but it just wasn&#8217;t fast enough. Of course, having concurrent animations all over the place starting at different intervals with only one StoryBoard was challenging enough, and I didn&#8217;t want a StoryBoard for every single object on the screen that would probably kill performance too. So, I distributed all the animations between four StoryBoards.</p>
<p>Now there&#8217;s Timers, Threads and CompositionTarget event rendering along with StoryBoard so you can take your pick on what method to use which I find quite nice. Click the image bellow to play!</p>
<p><a href="http://silverarcade.com/games/sithiro/art-of-solitaire" target="_blank"><img style="border-width:0;" src="http://sithiro.files.wordpress.com/2008/11/image3.png?w=422&#038;h=265" border="0" alt="Art of Solitaire - Click to play" width="422" height="265" /></a></p>
<p>Thank you,<br />
&#8211;Bill Sithiro</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sithiro.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sithiro.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sithiro.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sithiro.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sithiro.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sithiro.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sithiro.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sithiro.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sithiro.wordpress.com&amp;blog=766201&amp;post=64&amp;subd=sithiro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sithiro.wordpress.com/2008/11/26/art-of-solitaire-silverlight-2-game/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">billy</media:title>
		</media:content>

		<media:content url="http://sithiro.files.wordpress.com/2008/11/image3.png" medium="image">
			<media:title type="html">Art of Solitaire - Click to play</media:title>
		</media:content>
	</item>
	</channel>
</rss>
