<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0"><channel><title>Tales from the Evil Empire</title><link>http://weblogs.asp.net/bleroy/default.aspx</link><description>Bertrand Le Roy's blog</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/TalesFromTheEvilEmpire" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><title>You can do the TODOs today too!</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/16/you-can-do-the-todos-today-too.aspx</link><pubDate>Tue, 16 Jun 2009 23:08:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7127059</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>15</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7127059</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/16/you-can-do-the-todos-today-too.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Don&amp;#39;t just leave your junk lying around. (c) Bertrand Le Roy 2002" border="0" alt="Don&amp;#39;t just leave your junk lying around. (c) Bertrand Le Roy 2002" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN0304_249D594C.jpg" width="244" height="184" /&gt; If you’re anything like me, you probably litter your code with TODO comments, postponing random tasks for the sake of moving the project forward. And there is of course a non-zero probability that you are going to ship with those comments still in.&lt;/p&gt;  &lt;p&gt;So I want to propose the following call to action to all readers of this blog: &lt;strong&gt;open your current project and start implementing what’s in those TODOs today&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Finding the TODOs is easy. In Visual Studio, do CTRL+SHIFT+F (search in files), make sure you are searching across the whole solution and search for “TODO”. In the results window, you can click on each result to open the file at the TODO comment location.&lt;/p&gt;  &lt;p&gt;While working on a file, you can open the task list window (CTRL+\, CTRL+T), open that drop-down menu and choose “Comments”:   &lt;br /&gt;    &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="TaskList Comments" border="0" alt="TaskList Comments" src="http://weblogs.asp.net/blogs/bleroy/TaskListComments_37140328.png" width="292" height="92" /&gt; &lt;/p&gt;  &lt;p&gt;Once you’ve done that, Visual Studio brings you a list of all those TODO comments in the currently open files, which is easier on the eyes than the search results window:   &lt;br /&gt;    &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Task List" border="0" alt="Task List" src="http://weblogs.asp.net/blogs/bleroy/TaskList_667418CD.png" width="521" height="246" /&gt; &lt;/p&gt;  &lt;p&gt;You can now go through those one by one (double-clicking on the TODO in the task list takes you there) and delete the comments once you’ve implemented the feature they describe.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; some commenters pointed out that the todo list is limited in scope. I updated the post. Resharper also apparently has a greater feature around that.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7127059" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/iu4Z1nXDOnc" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Mocking indexer setters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx</link><pubDate>Mon, 15 Jun 2009 22:27:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7124027</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7124027</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2004" border="0" alt="(c) Bertrand Le Roy 2004" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2906_5A7A1A17.jpg" width="244" height="184" /&gt; I quite like &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; because it makes sense for me. Shamefully, I’ve always had some trouble understanding test code that was using mocks built with other frameworks. With &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt;, I can just grok it for some reason. It’s just super-clear to me. It doesn’t mean I have any idea how it really works but for now I’m just happy with the magic.&lt;/p&gt;  &lt;p&gt;Anyway, yesterday I wanted to check that a controller action was setting some Application variable (let’s not get into the debate on whether or not it should do that at all). Something like this:&lt;/p&gt;  &lt;pre class="code"&gt;application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now how do I enable the object to be set by the tested code? Well, that one is easy, I can use &lt;a href="http://www.clariusconsulting.net/labs/moq/html/87C20A5E.htm"&gt;SetupSet&lt;/a&gt; on indexers just fine:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
mockHttpContext.SetupSet(&lt;br /&gt;    c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This call tells the mock that it can accept to run code that attempts to set the “Foo” application variable with any object.&lt;/p&gt;

&lt;p&gt;But now, what if I want to get a reference to that object from my test code in order to check the value of some of its properties?&lt;/p&gt;

&lt;p&gt;Well, &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; has a Callback method that you can hook to the result of any Setup call. The action that you provide it will be run whenever the setup code is called. The problem with that callback method is that its signature must match that of the setter exactly. Unfortunately, that signature is implicit. If you get it wrong, the test will fail more or less silently (it will just tell you setup failed with little details). To get this right, you need to know what the setter syntactic sugar compiles to, which kinda sucks, but the good news is that you only have to figure it out once, which I just spent some time doing for you (and for myself too, let’s be honest):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;map = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;br /&gt;mockHttpContext&lt;br /&gt;    .SetupSet(c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;())
    .Callback((&lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object &lt;/span&gt;m) =&amp;gt; { map = (&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;)m; });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Because this is an indexer setter, the compiled code actually takes a name and a value, which is reflected by the signature of the callback lambda. We can now call into the code to test, knowing that when it sets our “Foo” application variable, the local “map” variable of the test code will get set. The test code can then party on the object and assert whatever it wants.&lt;/p&gt;

&lt;p&gt;I hope this saves some time whoever is trying to do the same thing.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7124027" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/KuF8hzNQWmQ" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category></item><item><title>Why is ASP.NET encoding &amp;’s in script URLs? A tale of looking at entirely the wrong place for a cause to a non-existing bug.</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/05/why-is-asp-net-encoding-amp-s-in-script-urls-a-tale-of-looking-at-entirely-the-wrong-place-for-a-cause-to-a-non-existing-bug.aspx</link><pubDate>Sat, 06 Jun 2009 00:27:15 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7109236</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7109236</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/05/why-is-asp-net-encoding-amp-s-in-script-urls-a-tale-of-looking-at-entirely-the-wrong-place-for-a-cause-to-a-non-existing-bug.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Bug_50FD5922.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Bug_thumb_774F16A0.jpg" width="254" height="184" /&gt;&lt;/a&gt; Several people have &lt;a href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134"&gt;reported&lt;/a&gt; seeing errors in their logs that seem to be due to requests such as this:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;/ScriptResource.axd?d=     &lt;br /&gt;[lots of junk]&lt;strong&gt;&amp;amp;amp;       &lt;br /&gt;&lt;/strong&gt;t=ffffffffee24147c&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The important part here is the HTML-encoded “&amp;amp;amp;” sequence, which stands for “&amp;amp;” of course. &lt;em&gt;If&lt;/em&gt; this exact URL is sent to the server, the server won’t know what to do with the escape sequence (URLs are not supposed to be HTML-encoded on the wire) so the parameters won’t get separated as expected, potentially resulting in a server error. This bug in the toolkit is an example of that: &lt;a href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134"&gt;http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Of course, when people see 500 errors popping up in their server logs, they immediately assume the application is failing for some users. Or that some idiot at Microsoft did something incredibly stupid (that’s what we idiots at Microsoft do after all).&lt;/p&gt;  &lt;p&gt;Case in point, a quick peek into the source code of the application’s pages immediately reveals that the script tags generated by ScriptManager do indeed generate these URLs:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;lt;script src=&amp;quot;/ScriptResource.axd?d=[lots of junk]&amp;amp;amp;t=ffffffff8824ac28&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;So that’s where it came from! See? &lt;strong&gt;When I copy this URL into the browser’s URL bar, I do get the same error!&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Then ensue various more or less rational reactions such as:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Correlate the user agent to the faulty requests (which correlates more or less with normal browser usage, i.e. lots of IE and then lots of Firefox, when there is a large enough sample).&lt;/li&gt;    &lt;li&gt;Blame IE6 (lots of these requests come from IE6, hence it must be responsible: IE6 sucks).&lt;/li&gt;    &lt;li&gt;“Fix” ScriptManager and remove the HTML encoding.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Well, by copying the URL from the source view into the URL bar, you did indeed reproduce the problem. A little too well. Better than you realize.&lt;/p&gt;  &lt;p&gt;This is why. &lt;strong&gt;All of the errors in your server logs come from people doing precisely what you just did&lt;/strong&gt;: copy the URL from the source view into the browser’s URL bar. They do it for various reasons: look at the source code for the scripts, understand what these weird URLs are, who knows?&lt;/p&gt;  &lt;p&gt;But the point is, you will never be able to reproduce these errors during normal use of the application. There is nothing to fix here. The value that gets sent to the server never has the “&amp;amp;amp;” sequence. You can verify it in IE6, you can verify it in any browser on any OS, it will just work.&lt;/p&gt;  &lt;p&gt;When putting a URL in an HTML attribute, you should &lt;strong&gt;*always*&lt;/strong&gt; HTML-encode it. It’s the standard, and for good reason (it enables the browser to tell between “&amp;amp;”, “&amp;amp;amp;” and “&amp;amp;amp;amp;”, it enables quotes to be embedded into attributes, etc.).&lt;/p&gt;  &lt;p&gt;A consequence of that is that if you’re going to copy the value of one of these attributes from the source view, you should do what the browser does when parsing the HTML: decode the value first (in other words, replace “&amp;amp;amp;” with “&amp;amp;”).&lt;/p&gt;  &lt;p&gt;So yes, people do fail to do that and copy the URL without decoding. Well, they are not supposed to do that, nor do they need to do it. The error is normal, it results from a bad URL having been entered manually. Nobody would be surprised to get an error when querying foo.aspx?somenumber=thisisnotanumber for example. Same thing here. Pretty much.&lt;/p&gt;  &lt;p&gt;Of course, this is not entirely trivial to figure out and I did pull my remaining hair a bit trying to understand what was going on, and you tend to trust people when they tell you there is a problem, especially when the description seems to make sense. There is some sort of confirmation bias going on there. But the more I looked at the different pieces of evidence, the more this explanation looked like the most likely, by far.&lt;/p&gt;  &lt;p&gt;But of course, I may be missing something…&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7109236" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/VbpySOOODT0" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category></item><item><title>Survey: Ajax usage among .NET developers</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx</link><pubDate>Fri, 22 May 2009 19:13:20 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7094884</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7094884</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Copenhagen_36DBEC47.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Copenhagen_thumb_55B26025.jpg" width="204" height="154" /&gt;&lt;/a&gt; If you haven’t already and you are a .NET developer, please take a couple minutes and answer this survey, whether you use Ajax or not. There are a number of Ajax surveys around, but Simone’s is the only one that focuses on .NET developers.&lt;/p&gt;  &lt;p&gt;The survey:   &lt;br /&gt;&lt;a title="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H" href="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H"&gt;http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Simone’s post:   &lt;br /&gt;&lt;a href="http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx"&gt;http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7094884" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/sLbq3fV2J24" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>Twitter contest: what can you code in 130 characters?</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/20/twitter-contest-what-can-you-code-in-130-characters.aspx</link><pubDate>Thu, 21 May 2009 04:59:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093917</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>10</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7093917</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/20/twitter-contest-what-can-you-code-in-130-characters.aspx#comments</comments><description>&lt;p&gt;I just posted the following snippet on Twitter. The exercise is to write meaningful and preferably cool code that fits in a Twitter message along with the &lt;a href="http://search.twitter.com/search?q=%23twitcode"&gt;#twitcode&lt;/a&gt; keyword, which leaves 130 characters.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private static readonly byte&lt;/span&gt;[] _blankGif =
  &lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.FromBase64String(&lt;br /&gt;      &lt;span style="color: #a31515"&gt;&amp;quot;R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAEBMgA7&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

&lt;p&gt;I can’t wait to see what people come up with.&lt;/p&gt;

&lt;p&gt;Monitor the results from here:
  &lt;br /&gt;&lt;a title="http://search.twitter.com/search?q=%23twitcode" href="http://search.twitter.com/search?q=%23twitcode"&gt;http://search.twitter.com/search?q=%23twitcode&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093917" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/Koea-UQzFT4" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Twitter/default.aspx">Twitter</category></item><item><title>setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx</link><pubDate>Fri, 15 May 2009 05:04:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7087574</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7087574</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="bleroy05" border="0" alt="bleroy05" align="left" src="http://weblogs.asp.net/blogs/bleroy/bleroy05_7555BBC5.jpg" width="244" height="164" /&gt; JavaScript has two ways of delaying execution of code: &lt;a href="http://msdn.microsoft.com/en-us/library/ms536749(VS.85).aspx"&gt;setInterval&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx"&gt;setTimeout&lt;/a&gt;. Both take a function or a string as the first parameter, and a number of milliseconds as the second parameter. The only difference is that the code provided to setInterval will run every n milliseconds whereas the code in &lt;a href="http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx"&gt;setTimeout&lt;/a&gt; will run only once.&lt;/p&gt;  &lt;p&gt;Before I explain why I think setInterval is evil, allow me to rant on a related subject for a paragraph: you should never pass a string into any of those functions and instead always pass a function reference (unless you really, really know what you’re doing). If you pass a string, it will have to be evaluated on the fly, and eval is quite evil itself (unless you really, really know what you’re doing). It might seem tempting to generate code this way to inject dynamic parameters, but there are better ways of doing that, using &lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;currying&lt;/a&gt;. In Microsoft Ajax, for example, we provide the handy &lt;a href="http://weblogs.asp.net/bleroy/archive/2007/04/10/how-to-keep-some-context-attached-to-a-javascript-event-handler.aspx"&gt;Function.createCallback and Function.createDelegate&lt;/a&gt; for exactly that usage.&lt;/p&gt;  &lt;p&gt;Anyways now let’s get back to the issue at hand: setInterval is evil. To better compare, let’s assume you have a reason to use setInterval (why else would you be reading this?). Here’s the code you might write:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;setInterval&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
        var &lt;/span&gt;interval = setInterval(appendDateToBody, 5000);&lt;br /&gt;&lt;span style="color: blue"&gt; &lt;br /&gt;        function &lt;/span&gt;appendDateToBody() {
            document.body.appendChild(
                document.createTextNode(&lt;span style="color: blue"&gt;new &lt;/span&gt;Date() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;));
        }

        &lt;span style="color: blue"&gt;function &lt;/span&gt;stopInterval() {
            clearInterval(interval);
        }
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Stop&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;stopInterval();&amp;quot; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Both APIs have a corresponding clear API that enables the developer to cancel the interval or timeout. To identify what interval or timeout you’re cancelling, you pass into the API a token that is whatever value the call to setInterval or setTimeout returned. And that’s a first reason why setInterval is (mildly) evil: if you lose that token, there is no way you can ever stop that code from running every five seconds until you navigate away from the page. But that is a minor inconvenience, it just means you need to carefully manage your own stuff, right? Well, actually if code that you don’t know or don’t control created that interval, you’re probably in trouble even if you kept your own house real nice and tidy…&lt;/p&gt;

&lt;p&gt;But the real reason why I dislike setInterval is that it is quite hard to debug, in particular if you have more than one running simultaneously. For example, if you have two intervals, one running every 100 milliseconds, the other every five seconds, and if you want to debug the second one, the first one will constantly get triggered and will get in the way.&lt;/p&gt;

&lt;p&gt;So what, you might say, is the alternative? Well, here is code that is quasi-equivalent to the code above, but that uses the much less evil setTimeout:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;interval = setTimeout(appendDateToBody, 5000);

&lt;span style="color: blue"&gt;function &lt;/span&gt;appendDateToBody() {
    document.body.appendChild(
        document.createTextNode(&lt;span style="color: blue"&gt;new &lt;/span&gt;Date() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;));
    interval = setTimeout(appendDateToBody, 5000);
}

&lt;span style="color: blue"&gt;function &lt;/span&gt;stopInterval() {
    clearTimeout(interval);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here, instead of taking a subscription for life, we’re just renewing the lease as we go. The big advantage of this code is that to stop the interval, you don’t need the token. Actually, I rarely even bother to keep hold of it. All you have to do is skip the line of code that renews the timeout for the next iteration. So no housekeeping to do, and if during a debugging session you need to get one of the interval functions out of the way, just drag your debugger’s current execution pointer to the end of the function, hit F5 and you’ll never see that function run ever again, boom, it’s out of the way and you can focus on your own debugging.&lt;/p&gt;

&lt;p&gt;So to summarize, replacing setInterval with setTimeout is easy, pain-free and removes the inconveniences of setInterval. So while the setInterval evil is about at the level of not replacing the cap of the toothpaste, the non-evil alternative is so easy that I can’t see a reason not to forget about setInterval for good.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7087574" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/d5zWxbMtl2w" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>New release of the Ajax Control Toolkit</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx</link><pubDate>Thu, 14 May 2009 05:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7086151</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>43</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7086151</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx#comments</comments><description>&lt;p&gt;A &lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;new version of the AJAX Control Toolkit&lt;/a&gt; is now available for download from the CodePlex website. It contains three new controls:&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="HtmlEditor" border="0" alt="HtmlEditor" align="left" src="http://weblogs.asp.net/blogs/bleroy/HtmlEditor_1E6A1BAC.png" width="244" height="159" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/HTMLEditor/HTMLEditor.aspx"&gt;HTMLEditor&lt;/a&gt;&lt;/strong&gt; - allows you to easily create and edit HTML content. You can edit in WYSIWYG mode or in HTML source mode. The control exists as a server-side extender but can also be instantiated purely on the client-side with a single line of code. Many thanks to &lt;a href="http://www.obout.com"&gt;Obout&lt;/a&gt; for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="ComboBox" border="0" alt="ComboBox" align="right" src="http://weblogs.asp.net/blogs/bleroy/ComboBox_0834005A.png" width="162" height="244" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx"&gt;ComboBox&lt;/a&gt;&lt;/strong&gt; - provides a DropDownList of items, combined with TextBox. Different modes determine the interplay between the text entry and the list of items. this control behaves very much like a Windows combo. Many thanks to Dan Ludwig for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ColorPicker" border="0" alt="ColorPicker" align="left" src="http://weblogs.asp.net/blogs/bleroy/ColorPicker_23002666.png" width="238" height="189" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ColorPicker/ColorPicker.aspx"&gt;ColorPicker&lt;/a&gt;&lt;/strong&gt; - can be attached to any ASP.NET TextBox control to provide client-side color-picking functionality from a popup. Many thanks to Alexander Turlov for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;The ASP.NET website has been updated with new &lt;a href="http://www.asp.net/learn/ajax-videos"&gt;videos&lt;/a&gt; and &lt;a href="http://www.asp.net/learn/ajax"&gt;tutorials&lt;/a&gt; for these controls.&lt;/p&gt;  &lt;p&gt;This new release also includes fixes for over 20 of the most voted bugs in existing AJAX Control Toolkit controls and now features minimized release versions of the script files.&lt;/p&gt;  &lt;p&gt;The release can be downloaded as a server dll or as a set of files for use with pure client-side applications.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7086151" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/jHRTZBF4vKU" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category></item><item><title>Creating jQuery plug-ins from MicrosoftAjax components</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx</link><pubDate>Tue, 05 May 2009 05:23:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7073206</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>16</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7073206</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy" border="0" alt="(c) Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/ConjonctionRetouche_3337CDB8.jpg" width="244" height="156" /&gt; We had an interesting discussion recently on the &lt;a href="http://aspinsiders.com/"&gt;ASP Insiders&lt;/a&gt; mailing list and ended up talking about what cool stuff we could build on top of jQuery. Many interesting things were mentioned and it was a very useful discussion but one suggestion in particular struck my curiosity as it was something I had investigated before and that could be improved on with very little code.&lt;/p&gt;  &lt;p&gt;I had already written &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/09/28/jquery-now-officially-part-of-the-net-developer-s-toolbox.aspx"&gt;a little plugin to enable instantiation of Microsoft Ajax components on the results of a jQuery selector&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;jQuery.fn.create = &lt;span style="color: blue"&gt;function&lt;/span&gt;(type, properties) {
&lt;span style="color: blue"&gt;    return this&lt;/span&gt;.each(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
        Sys.Component.create(type, properties, {}, {},&lt;span style="color: blue"&gt; this&lt;/span&gt;);
    });
};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I have another version that is a little more elaborate and takes a bag of properties and events instead of just properties (get it from the attached sample) but you get the idea. This makes it fairly easy to instantiate components based on a selector:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;)
    .create(Bleroy.Sample.CharCount, { maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;But if this were a native jQuery plugin instead of a Microsoft Ajax component, as &lt;a href="http://west-wind.com/weblog/"&gt;Rick Strahl&lt;/a&gt; suggested, chances are you’d do something like this instead:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;).charCount({ maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We can actually get that exact result fairly easily by writing a plugin that is specific to this component, but we can’t expect every Microsoft Ajax component author to also write a jQuery plugin, can we? We can do better than that. Here is a second small piece of code that is still generic but is in the business of &lt;em&gt;creating jQuery plugins&lt;/em&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.Component.exposeTojQuery = &lt;span style="color: blue"&gt;function&lt;/span&gt;(type, pluginName) {
    &lt;span style="color: blue"&gt;return &lt;/span&gt;jQuery.fn[pluginName] = &lt;span style="color: blue"&gt;function&lt;/span&gt;(properties) {
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.each(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
            Sys.Component.create(type, properties, {}, {}, &lt;span style="color: blue"&gt;this&lt;/span&gt;);
        });
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Like above, the version in the sample is better than that in that it supports events in addition to properties but again you get the idea. Thanks to this function, we can create a jQuery plugin for any existing Microsoft Ajax component with a single line of code:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.Component.exposeTojQuery(Bleroy.Sample.CharCount, &lt;span style="color: #a31515"&gt;&amp;quot;charCount&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;After this call, we can write exactly the code we wrote before, but without having had to write a specific plugin. We can then use charCount like a native jQuery plugin:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;).charCount({ maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is really easy to use, and I quite like it. We could for example add the following line of code to the new MicrosoftAjaxTemplates.js:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(jQuery)&lt;br /&gt;    Sys.Component.exposeTojQuery(Sys.UI.DataView, &lt;span style="color: #a31515"&gt;&amp;quot;dataView&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This would enable us to instantiate DataView controls as simply as this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dv&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{{ $dataItem }}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
&lt;/span&gt;    $(&lt;span style="color: #a31515"&gt;&amp;quot;.dv&amp;quot;&lt;/span&gt;).dataView({ data: [&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;baz&amp;quot;&lt;/span&gt;] });
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Get the code from here (you need to include &lt;a href="http://docs.jquery.com/Downloading_jQuery"&gt;jquery-1.3.2.js&lt;/a&gt; to the script folder):

  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7073206" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/ayB1fhyZXqU" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>Five gems of XBLA</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/03/five-gems-of-xbla.aspx</link><pubDate>Sun, 03 May 2009 07:20:44 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7071021</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>5</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7071021</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/03/five-gems-of-xbla.aspx#comments</comments><description>&lt;ol&gt;   &lt;li&gt;&lt;strong&gt;&lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d802584108c2/"&gt;The Maw&lt;/a&gt;&lt;/strong&gt; is a charming and seriously fun game that is going to make you smile from start to finish.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d80258410896/"&gt;Puzzle Quest&lt;/a&gt;&lt;/strong&gt; is one of those “just one more” time sinks. The improbable mix of puzzle gaming and RPG is just awesome. The sequel, &lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d8025841091b/"&gt;Galactrix&lt;/a&gt;, is pretty good too (but quite buggy, you might want to wait for a patch or at least regularly copy your game save).&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d80258410960/"&gt;Portal&lt;/a&gt;&lt;/strong&gt; is simply the best game ever, at least for me. Portal proves once more that the simplest ideas can be the deepest and does so with amazing grace and mastery of the art of video games.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d80258410954/"&gt;Banjo Kazooie&lt;/a&gt;&lt;/strong&gt; and &lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d80258410955/"&gt;Banjo Tooie&lt;/a&gt; were two of the best N64 games, maybe on par with Super Mario but with that unique Rare sense of humour.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d802584107e7/"&gt;Cloning Clyde&lt;/a&gt;&lt;/strong&gt;: smart and funny.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;What are your favorite XBLA games?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7071021" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/kDxhQUACzzc" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/Gaming/default.aspx">Gaming</category></item><item><title>Glimmer: visually build jQuery animations and stuff</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/28/glimmer-visually-build-jquery-animations-and-stuff.aspx</link><pubDate>Tue, 28 Apr 2009 18:35:29 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7063913</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>4</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7063913</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/28/glimmer-visually-build-jquery-animations-and-stuff.aspx#comments</comments><description>&lt;p&gt;&lt;a title="Glimmer" href="http://visitmix.com/Articles/Glimmer-a-jQuery-Interactive-Design-Tool"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="glim4s" border="0" alt="glim4s" align="left" src="http://weblogs.asp.net/blogs/bleroy/glim4s_380D1EFE.png" width="244" height="183" /&gt;&lt;/a&gt;If you’re still intimidated by jQuery or DOM manipulation in general, if you need to quickly build web animations, if you’re more a designer guy, if you think tooling makes sense, or a combination of the above, you should probably check out &lt;a href="http://visitmix.com/Articles/Glimmer-a-jQuery-Interactive-Design-Tool"&gt;Glimmer&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;In a nutshell, &lt;a href="http://visitmix.com/Articles/Glimmer-a-jQuery-Interactive-Design-Tool"&gt;Glimmer&lt;/a&gt; is a visual tool that builds HTML animations, menus, tooltips on jQuery. It builds all the code you need (HTML, CSS and JavaScript with jQuery) at the click of a button.&lt;/p&gt;  &lt;p&gt;Check it out!   &lt;br /&gt;&lt;a href="http://visitmix.com/Articles/Glimmer-a-jQuery-Interactive-Design-Tool"&gt;http://visitmix.com/Articles/Glimmer-a-jQuery-Interactive-Design-Tool&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7063913" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/tzEKMIxdgVg" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CSS/default.aspx">CSS</category></item><item><title>A blog on Microsoft Ajax client templates and data</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/21/a-blog-on-microsoft-ajax-client-templates-and-data.aspx</link><pubDate>Tue, 21 Apr 2009 21:45:01 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7054330</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>5</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7054330</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/21/a-blog-on-microsoft-ajax-client-templates-and-data.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://politian.wordpress.com/"&gt;Politian&lt;/a&gt; has a &lt;a href="http://politian.wordpress.com/"&gt;great blog series&lt;/a&gt; where he goes into the details of building a data-driven Ajax application using &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;the new 4.0 client templates and data&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Check it out!   &lt;br /&gt;&lt;a href="http://politian.wordpress.com/"&gt;http://politian.wordpress.com/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7054330" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/RtuIUbZs3s8" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>CSS isolation: there has got to be a better way</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/14/css-isolation-there-has-got-to-be-a-better-way.aspx</link><pubDate>Tue, 14 Apr 2009 07:26:04 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7045774</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7045774</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/14/css-isolation-there-has-got-to-be-a-better-way.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/spider_058EE49A.jpg" width="244" height="184" /&gt; CSS can be a tricky thing. I’m trying to do something that I think should be pretty simple. Let’s say a page contains a section (e.g. an admin panel) that must be styled independently from the rest of the page, but consistently and predictably. The DOM and CSS for the main part of the page is undetermined (e.g. because it’s part of a user-defined theme). Of course, you could use iframes, which are about the only isolation mechanism in HTML but we can’t do this here because iframes are quite rigid in shape (they are rectangles), they make scripting the DOM more difficult and they pretty much require an additional round-trip to the server to serve their contents.&lt;/p&gt;  &lt;p&gt;The real problem to solve here is that if the main CSS for the page defines very general styles that for example target all elements with a given tag name, and those styles are going to bleed into our specialized region unless we find a way to block that CSS from cascading down. Ideally, you’d have an attribute on the tag, something like inheritcss=”false”, but no such thing exists.&lt;/p&gt;  &lt;p&gt;So the only way I’ve found to solve this problem is to write a stylesheet that explicitly resets the defaults for all properties and all elements. Here is an excerpt from the CssIsolation.css file:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #a31515"&gt;.isolate *
&lt;/span&gt;{
    &lt;span style="color: red"&gt;background&lt;/span&gt;: &lt;span style="color: blue"&gt;white none repeat scroll 0% 0%&lt;/span&gt;;
    &lt;span style="color: red"&gt;border&lt;/span&gt;: &lt;span style="color: blue"&gt;0px none black&lt;/span&gt;;
    &lt;span style="color: red"&gt;border-collapse&lt;/span&gt;: &lt;span style="color: blue"&gt;separate&lt;/span&gt;;
    &lt;span style="color: red"&gt;border-spacing&lt;/span&gt;: &lt;span style="color: blue"&gt;0px&lt;/span&gt;;&lt;br /&gt;[...]
    &lt;span style="color: red"&gt;z-index&lt;/span&gt;: &lt;span style="color: blue"&gt;auto&lt;/span&gt;;
}
&lt;span style="color: #a31515"&gt;.isolate p 
&lt;/span&gt;{
    &lt;span style="color: red"&gt;display&lt;/span&gt;:&lt;span style="color: blue"&gt;block&lt;/span&gt;;
    &lt;span style="color: red"&gt;margin-top&lt;/span&gt;: &lt;span style="color: blue"&gt;16px&lt;/span&gt;;
    &lt;span style="color: red"&gt;margin-bottom&lt;/span&gt;: &lt;span style="color: blue"&gt;16px&lt;/span&gt;;
}
&lt;span style="color: #a31515"&gt;.isolate strong&lt;/span&gt;, &lt;span style="color: #a31515"&gt;.isolate b 
&lt;/span&gt;{
    &lt;span style="color: red"&gt;font-weight&lt;/span&gt;: &lt;span style="color: blue"&gt;bold&lt;/span&gt;;
}
&lt;span style="color: #a31515"&gt;.isolate h1
&lt;/span&gt;{
    &lt;span style="color: red"&gt;display&lt;/span&gt;: &lt;span style="color: blue"&gt;block&lt;/span&gt;;
    &lt;span style="color: red"&gt;font-size&lt;/span&gt;: &lt;span style="color: blue"&gt;xx-large&lt;/span&gt;;
    &lt;span style="color: red"&gt;font-weight&lt;/span&gt;: &lt;span style="color: blue"&gt;bold&lt;/span&gt;;
    &lt;span style="color: red"&gt;margin-top&lt;/span&gt;: &lt;span style="color: blue"&gt;21px&lt;/span&gt;;
    &lt;span style="color: red"&gt;margin-bottom&lt;/span&gt;: &lt;span style="color: blue"&gt;21px&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is lame for a number of reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Defaults might vary from browser to browser (this is particularly true of fonts and sizes).&lt;/li&gt;

  &lt;li&gt;Some styles can’t be reset to defaults, such as default button styles (at least, not without using browser-specific styles that aren’t consistently available):
    &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="Default button styles can&amp;#39;t be reproduced." border="0" alt="Default button styles can&amp;#39;t be reproduced." src="http://weblogs.asp.net/blogs/bleroy/DefaultButtonCSS_3086D29F.png" width="329" height="78" /&gt; &lt;/li&gt;

  &lt;li&gt;An exhaustive list of tags and style properties is sure to be wrong now or in the future (I did CSS 2, so it’s already outdated by CSS3).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still, imperfect as it is, this is a solution that works reasonably well and can be improved as problems are found. The equivalent of that inheritcss=”false” attribute, using my isolation stylesheet, is to set class=”isolate” on the parent element of the section of the page you want to isolate. None of the styles defined for the rest of the page (which is defined just as usual, with no difference whatsoever) should bleed then.&lt;/p&gt;

&lt;p&gt;Here a snapshot of my test page, where you can see, side-by-side, unstyled HTML and HTML with reset styles:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px 0px; display: inline; border-top: 0px; border-right: 0px" title="Comparison of contents, unstyled and with reset applied." border="0" alt="Comparison of contents, unstyled and with reset applied." src="http://weblogs.asp.net/blogs/bleroy/CssIsolationTest_7DB6CC2A.png" width="411" height="838" /&gt; &lt;/p&gt;

&lt;p&gt;Differences exist but are quite subtle.&lt;/p&gt;

&lt;p&gt;The HTML for an isolated section is defined like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;isolate&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;isolated1&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;Isolated 1...
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If for example the main style sheet defines the style of lists like follows, this style won’t bleed into any element that has the isolate class (on the right on the next screenshot), just because we set the “isolate” class on the parent element.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #a31515"&gt;ul li 
&lt;/span&gt;{
    &lt;span style="color: red"&gt;list-style-type&lt;/span&gt;: &lt;span style="color: blue"&gt;square&lt;/span&gt;;
}
&lt;span style="color: #a31515"&gt;ol li 
&lt;/span&gt;{
    &lt;span style="color: red"&gt;list-style-type&lt;/span&gt;: &lt;span style="color: blue"&gt;upper-roman&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="Main CSS doesn&amp;#39;t bleed into isolated section." border="0" alt="Main CSS doesn&amp;#39;t bleed into isolated section." src="http://weblogs.asp.net/blogs/bleroy/CssResetList_1F52F4FC.png" width="304" height="189" /&gt; &lt;/p&gt;

&lt;p&gt;You can then style each of these sections independently by just qualifying each of the style selectors with the ID of the parent element. In other words, just paste “#isolated1” in front of each selector of the local stylesheet, if the id of the parent element is “isolated1”:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #a31515"&gt;#isolated1 p
&lt;/span&gt;{
    &lt;span style="color: red"&gt;color&lt;/span&gt;: &lt;span style="color: blue"&gt;Green&lt;/span&gt;;
    &lt;span style="color: red"&gt;border&lt;/span&gt;: &lt;span style="color: blue"&gt;solid 1px black&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This style will override the isolation CSS because it is qualified by id, which always wins over styles that are only qualified by class:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="Local isolated CSS" border="0" alt="Local isolated CSS" src="http://weblogs.asp.net/blogs/bleroy/LocalIsolatedCSS_057EBECD.png" width="307" height="37" /&gt; &lt;/p&gt;

&lt;p&gt;I really wish one of you will tell me how stupid I am for not knowing about feature/hack X that is way simpler and gets you to the same place… There has &lt;strong&gt;got&lt;/strong&gt; to be a better way.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The code for the isolation stylesheet, as well as the test pages, can be found here:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/CssIsolation.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/CssIsolation.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/CssIsolation.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7045774" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/LXJF8Cq1PVk" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CSS/default.aspx">CSS</category></item><item><title>Quantum computing done right</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/01/quantum-computing-done-right.aspx</link><pubDate>Wed, 01 Apr 2009 23:44:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7021720</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7021720</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/01/quantum-computing-done-right.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2004 Bertrand Le Roy" border="0" alt="(c) 2004 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/IMG_1561_43B6A0BD.jpg" width="244" height="164" /&gt; I know as a good microsoftee I should be supportive of what my employer does no matter what it is, and I might get fired for this post, but &lt;a href="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx"&gt;Eilon’s latest article&lt;/a&gt; is wrong on so many levels I have to step up with whatever integrity I have left and fix this mess.&lt;/p&gt;  &lt;p&gt;In his post, he exposes the new ShrödingOr&amp;lt;TDead, TAlive&amp;gt; type that will be introduced in .NET 5.0 as part of the System.QuantumEntanglement namespace. Well, let’s face it, the current implementation has nothing quantum about it.&lt;/p&gt;  &lt;p&gt;Here’s how I would have written it:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;System.QuantumEntanglement {
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SchrödingOr&lt;/span&gt;&amp;lt;TDead, TAlive&amp;gt; {
        &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;_howDead;
        &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;_howAlive;

        &lt;span style="color: blue"&gt;public &lt;/span&gt;SchrödingOr(&lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;howDead, &lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;howAlive) {
            _howDead = howDead;
            _howAlive = howAlive;
        }

        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;Measure() {
            &lt;span style="color: blue"&gt;double &lt;/span&gt;howAliveSquareModulus =
                _howAlive.Real * _howAlive.Real +
                _howAlive.Imaginary * _howAlive.Imaginary;
            &lt;span style="color: blue"&gt;double &lt;/span&gt;howDeadSquareModulus =
                _howDead.Real * _howDead.Real +
                _howDead.Imaginary * _howDead.Imaginary;
            &lt;span style="color: blue"&gt;double &lt;/span&gt;probabilityOfBeingAlive = howAliveSquareModulus /
                (howAliveSquareModulus + howDeadSquareModulus);

            &lt;span style="color: blue"&gt;if &lt;/span&gt;((&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Random&lt;/span&gt;()).NextDouble() &amp;lt; probabilityOfBeingAlive) {
                _howAlive = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0);
                _howDead = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(0, 0);
                &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(TAlive);
            } &lt;span style="color: blue"&gt;else &lt;/span&gt;{
                _howAlive = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(0, 0);
                _howDead = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0);
                &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(TDead);
            }
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This way, the state really is a (complex) linear combination of the dead and alive types, which are the eigenstates of the system. Once you’ve created the state, you can never get it back unless you do a measurement. When you do that, the object collapses its state to one of the eigenstates based on probabilities determined by the actual quantum state of the system.&lt;/p&gt;

&lt;p&gt;The first measurement is random but once you’ve measured it, the cat remains alive or dead forever (the entanglement is destroyed by the measure).&lt;/p&gt;

&lt;p&gt;Here’s a little console app that creates a cat, puts it in the box and then opens the box:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args) {
    &lt;span style="color: green"&gt;// put the cat in the box
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;cat = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SchrödingOr&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DeadCat&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;LiveCat&lt;/span&gt;&amp;gt;(
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0), &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0));
    &lt;span style="color: green"&gt;// Open the box
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.Write(cat.Measure() == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DeadCat&lt;/span&gt;) ?
        &lt;span style="color: #a31515"&gt;&amp;quot;Cat is dead.&amp;quot; &lt;/span&gt;: &lt;span style="color: #a31515"&gt;&amp;quot;Cat is alive&amp;quot;&lt;/span&gt;);
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadKey();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This, I believe, is a much more realistic and useful implementation of SchrödingOr.&lt;/p&gt;

&lt;p&gt;The code: &lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Quantum.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Quantum.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Quantum.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eilon’s original article: &lt;a title="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx" href="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx"&gt;http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7021720" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/0UUwjpQllwI" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Science/default.aspx">Science</category></item><item><title>Some ASP.NET compiler black magic</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/30/some-asp-net-compiler-black-magic.aspx</link><pubDate>Mon, 30 Mar 2009 23:48:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7013147</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7013147</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/30/some-asp-net-compiler-black-magic.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="default" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/default_02BD11AD.jpg" width="244" height="164" /&gt; In the work we’ve been doing with Rob on the &lt;a href="http://blog.wekeroad.com/kona/kona-1/"&gt;Kona commerce app&lt;/a&gt;, our quest for extreme pluggability has led us to look at quite a few interesting features of ASP.NET compilation. Features I didn’t know about before &lt;a href="http://blogs.msdn.com/dmitryr/"&gt;Dmitry&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/davidebb/"&gt;David&lt;/a&gt; pointed them out for me. I thought I’d share…&lt;/p&gt;  &lt;p&gt;It starts with the &lt;a href="http://msdn.microsoft.com/en-us/library/d864zc1k(VS.80).aspx"&gt;&amp;lt;%@ Assembly src= %&amp;gt;&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/w70c655a(VS.80).aspx"&gt;&amp;lt;%@ Reference virtualpath= %&amp;gt;&lt;/a&gt; directives which you may have seen show up in IntelliSense when building a page. But what are they doing exactly and what differentiates them?&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="Works in Medium Trust" border="0" alt="Works in Medium Trust" align="right" src="http://weblogs.asp.net/blogs/bleroy/WorksInMediumTrust_41AE9248.gif" width="103" height="104" /&gt; &lt;/p&gt;  &lt;p&gt;They both enable you to reference code that is in a different file in the site. With both of them, you get full IntelliSense on the referenced code, but they don’t reference the same kinds of files. @Reference is meant to reference a specific class whereas @Assembly brings in an arbitrary code file. As a consequence, @Reference needs a file where there is a well-defined default class, such as a Page or a User Control. @Assembly on the other hand will enable you to reference an arbitrary code file with as many classes as you wish and get the compiler to dynamically build a neat assembly out of it. The difference really amounts to whether or not the build provider implements &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.getgeneratedtype.aspx"&gt;GetGeneratedType&lt;/a&gt;, which the aspx and ascx build provider implements, and which generic code file providers typically don’t. In other words, if you built your own &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.aspx"&gt;Build Provider&lt;/a&gt; and implemented that method, the files it compiles could be referenced using Reference. Without it, ASP.NET wouldn’t know which type to pick as the referenced one. But @Assembly will still work.&lt;/p&gt;  &lt;p&gt;Building an assembly from a virtual path was really important for us because it enables a file anywhere in the web site to be dynamically compiled and used, which is exactly how we wanted plug-ins to work: we could have put the plug-ins into App_code (which does dynamic compilation automatically) but the name is not exactly intuitive, and it limits your ability to mix languages within the same folder. Please note that there &lt;strong&gt;*is*&lt;/strong&gt; a way to mix languages in App_code:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.web&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;compilation &lt;/span&gt;&lt;span style="color: red"&gt;debug&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;codeSubDirectories&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;directoryName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;cs&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;directoryName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;js&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;codeSubDirectories&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;compilation&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.web&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This config setting instructs the compiler to compile the cs and js folders in App_code into separate assemblies, which is all we need to allow for multiple languages. But we can do better than that.&lt;/p&gt;

&lt;p&gt;It so happens that the compiler feature that enables the Assembly directive is also available as a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getcompiledassembly.aspx"&gt;public API&lt;/a&gt; (that works in Medium Trust):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;BuildManager&lt;/span&gt;.GetCompiledAssembly&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Seriously, this is now officially &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getcompiledassembly.aspx"&gt;my favorite API in the whole .NET framework&lt;/a&gt;. This quite remarkable API takes a code file within the site and compiles it into an assembly (if that hasn’t already been done by a previous call to that API or by an @Assembly directive). What you get back from it is an Assembly object, which you can reflect on (using public reflection, which works in Medium Trust) and use any way you want.&lt;/p&gt;

&lt;p&gt;This will enable us to dynamically compile the files in a Plugins top-level directory of the app. Doing so, we are getting multi-language support without config settings or special folders, nicer folder name and bonus points for not shutting down the app domain every effing time any file is touched. Bye bye App_code!&lt;/p&gt;

&lt;p&gt;Well, of course that’s now one assembly per code file, which could be a problem if you have 800 plug-ins in your app, but then again if that’s the case maybe it’s time you moved all those into a nice pre-compiled assembly. Or do some clean-up. Anyway, this will work just fine for the type of scale we have in mind.&lt;/p&gt;

&lt;p&gt;A question you may ask at this point is what exactly happens when one of those files is modified. Well, the BuildManager will generate a new assembly and “forget” about the old one. And when I say forget, I don’t mean it’s getting unloaded, just that it won’t get used anymore (unless you have code that held on to a reference). That means that potentially, there could be some assembly rot after a while if your files change often. To mitigate that, BuildManager has a set of rules that it uses to determine that it needs to restart the app domain after a while. Just like App_code, just a lot less often. Basically, the rules are pretty much the same as for aspx or ascx files.&lt;/p&gt;

&lt;p&gt;All right, so this is all quite useful (I know I’m going to use that stuff a lot, at least). How about a useless hack now? (if you don’t like a fun hack, feel free to skip the rest of this post)&lt;/p&gt;

&lt;p&gt;So think about all the neat stuff we could do by combining this with Virtual Path Providers... Except that in ASP.NET up to and including 3.5 SP1, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx"&gt;Virtual Path Providers&lt;/a&gt; don’t work in Medium Trust. Neither do &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.aspx"&gt;Build Providers&lt;/a&gt;, which would also be quite neat to play with. Bummer.&lt;/p&gt;

&lt;p&gt;But wait, people in the team thought about that and wondered what harm exactly you could do with VPPs and BPs that you couldn’t already do by simply writing code and well, the answer is pretty much nothing. So the good news is that VPPs and BPs will work in Medium Trust in ASP.NET 4.0. Hurray!&lt;/p&gt;

&lt;p&gt;So just for the sake of it, here’s the real black magic part of this post. Please note that there is more than one better way to do what I’m about to do and pretty much all of them would be simpler. I’m just hacking here and it’s going to be relatively convoluted.&lt;/p&gt;

&lt;p&gt;And as &lt;a href="http://en.wikipedia.org/wiki/Adam_Savage"&gt;Adam Savage&lt;/a&gt; says, “don’t try any of what you’re about to see at home. Ever!”. Seriously, this is dangerous code that has too many holes to count and that I wouldn’t run on anything but Visual Studio’s built-in web server (which is limited to requests from the same machine). Again, just hacking for fun here.&lt;/p&gt;

&lt;p&gt;The thing I’ve done is build a VPP that takes an operation embedded in the file name and builds a function that executes that operation. So for example, if you ask it for “A+B.cs”, it will get you a cs file that contains a class that has a method that takes two arguments and returns their sum:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Dynamic {
    &lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Calculator &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public static double &lt;/span&gt;Operation(&lt;span style="color: blue"&gt;double &lt;/span&gt;A, &lt;span style="color: blue"&gt;double &lt;/span&gt;B) {
            &lt;span style="color: blue"&gt;return &lt;/span&gt;A + B;
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Mix that with the amazing &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getcompiledassembly.aspx"&gt;BuildManager.GetCompiledAssembly&lt;/a&gt; and we have dynamic compilation of a user-specified function. Which is the scariest part of course, but still fun, eh?&lt;/p&gt;

&lt;p&gt;The usual disclaimer to use this at your own risk applies… I also didn’t spend too much time encoding the file name in the VPP thingy so some operations won’t go through too well. Multiplications for example. But eh, &lt;a href="http://www.drhorrible.com/"&gt;even the brightest minds don’t get everything right the first time&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/VppTest.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/VppTest.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/VppTest.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7013147" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/7fgT17aiHEU" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category></item><item><title>asp:menu fix for IE8 problem available</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/23/asp-menu-fix-for-ie8-problem-available.aspx</link><pubDate>Mon, 23 Mar 2009 21:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6995426</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>30</slash:comments><wfw:commentRss>http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6995426</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/23/asp-menu-fix-for-ie8-problem-available.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2003 Bertrand Le Roy" border="0" alt="(c) 2003 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/dscn22991_47AB4CCA.jpg" width="244" height="184" /&gt; Internet Explorer 8 is a unique release in the history of Internet Explorer in more than one way, but the decision to make standards mode the default means that authors of existing sites are impacted by it, if only to set the compatibility mode to IE7.&lt;/p&gt;  &lt;p&gt;But what if your site is built using components that render out markup and script over which you have little control, such as ASP.NET WebControls? Well, if one of the controls fails in IE8 standards mode, you need to either switch to compatibility mode (ouch!) or you need the component developer to ship an updated version.&lt;/p&gt;  &lt;p&gt;During the whole IE8 development cycle, we monitored the behavior of existing controls. Most ASP.NET built-in controls have been doing just fine in IE8, or the faulty behavior was actually due to an IE bug that we reported and that got fixed.&lt;/p&gt;  &lt;p&gt;All except asp:menu. It so happens that the menu control is making a bad assumption on what the default value for z-index should be. We debated this at length with the IE team, but it became clear as we did so that they were right and that we were wrong. We had to fix that.&lt;/p&gt;  &lt;p&gt;So here it is, the patch for menu is out and you can apply it to build IE8-compatible ASP.NET WebForms sites…&lt;/p&gt;  &lt;p&gt;Windows 2000, XP, Server 2003:   &lt;br /&gt;&lt;a href="http://code.msdn.microsoft.com/KB962351/Release/ProjectReleases.aspx?ReleaseId=2294"&gt;http://code.msdn.microsoft.com/KB962351/Release/ProjectReleases.aspx?ReleaseId=2294&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Windows Vista, Server 2008:   &lt;br /&gt;&lt;a href="http://code.msdn.microsoft.com/KB967535/Release/ProjectReleases.aspx?ReleaseId=2328"&gt;http://code.msdn.microsoft.com/KB967535/Release/ProjectReleases.aspx?ReleaseId=2328&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;UPDATE:&lt;/b&gt; the KB article can now be found here:&lt;br/&gt;&lt;a href="http://support.microsoft.com/kb/962351"&gt;http://support.microsoft.com/kb/962351&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6995426" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/TalesFromTheEvilEmpire/~4/YkiZk71dmuI" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft/default.aspx">Microsoft</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category></item></channel></rss>
