<?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: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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Kristof's blog</title>
	
	<link>http://kristofmattei.be</link>
	<description>A blog on my experiences in programming, work, and life!</description>
	<lastBuildDate>Mon, 09 Jan 2012 13:41:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/KristofsBlog" /><feedburner:info uri="kristofsblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Team Foundation 2010: Associate work item with changeset AFTER check in.</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/QMGxXwzsALQ/</link>
		<comments>http://kristofmattei.be/2012/01/09/team-foundation-2010-associate-work-item-with-changeset-after-check-in/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 13:41:16 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[TFS]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/?p=1123</guid>
		<description><![CDATA[Today I was struggling associating a work item with a changeset after a check in. There is however no options for this in the UI. Or is there? Let’s check: Ok, so not through the changeset UI. Let’s check the &#8230; <a href="http://kristofmattei.be/2012/01/09/team-foundation-2010-associate-work-item-with-changeset-after-check-in/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I was struggling associating a work item with a changeset after a check in. There is however no options for this in the UI. Or is there? Let’s check:</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2012/01/image.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Cannot edit work items from changeset window" border="0" alt="Cannot edit work items from changeset window" src="http://kristofmattei.be/wp-content/uploads/2012/01/image_thumb.png" width="564" height="402"></a></p>
<p>Ok, so not through the changeset UI.</p>
<p>Let’s check the <a href="http://msdn.microsoft.com/en-us/library/w51xa47k(v=VS.100).aspx" target="_blank">command line</a>.</p>
<pre>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC&gt;tf changeset /?
TF - Team Foundation Version Control Tool, Version 10.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Displays information about a changeset and lets you change the associated
attributes, such as comments and check-in notes.

tf changeset <strong>[/comment:("comment"|@commentfile)]</strong>
             <strong>[/notes:("NoteFieldName"="NoteFieldValue"|@notefile)]</strong> [/noprompt]
             [/collection:TeamProjectCollectionUrl]
             [changesetnumber | /latest] [/login:username,[password]]</pre>
<p>(emphasis mine).</p>
<p>So not through the command line. Now what? Let’s think: we want to associate a changeset with a work item. Why not look the other way around? </p>
<p>After the successful commit remember the changeset # (in our example 25464), go to the work item you want to associate the changeset to.</p>
<p>Right bottom you’ll see a tab called ‘Links’ (1), click it, and then click the ‘Link to’ (2) button.</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2012/01/associate.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Associate work item with changeset" border="0" alt="Associate work item with changeset" src="http://kristofmattei.be/wp-content/uploads/2012/01/associate_thumb.png" width="961" height="727"></a></p>
<p>In the next window, select ‘Changeset’ (1) and enter the number in the box (2). Hit&nbsp; ‘OK’ (3).</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2012/01/link.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Associate part II" border="0" alt="Associate part II" src="http://kristofmattei.be/wp-content/uploads/2012/01/link_thumb.png" width="561" height="275"></a></p>
<p>After that hit ‘CTR+S’ to save your work item.</p>
<p>After that go back to your changeset:</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2012/01/its-there.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="It's there!" border="0" alt="It's there!" src="http://kristofmattei.be/wp-content/uploads/2012/01/its-there_thumb.png" width="564" height="402"></a></p>
<p>It works!</p>
<p>Have a good one!</p>
<p>-Kristof</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/QMGxXwzsALQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2012/01/09/team-foundation-2010-associate-work-item-with-changeset-after-check-in/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2012/01/09/team-foundation-2010-associate-work-item-with-changeset-after-check-in/</feedburner:origLink></item>
		<item>
		<title>Silverlight 4: Bug in TabControl.TabStripPlacement = Dock.Left and OnApplyTemplate</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/lYyTrebJFJ4/</link>
		<comments>http://kristofmattei.be/2011/09/12/silverlight-4-bug-in-tabcontrol-tabstripplacement-dock-left-and-onapplytemplate/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 23:42:45 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/?p=1103</guid>
		<description><![CDATA[At work I ran into a bug with the Silverlight TabControl, more specifically when setting the TabStripPlacement to Dock.Left and hiding one of the TabItems in the parent’s OnApplyTemplate. The way to get to this bug is quite specific. For &#8230; <a href="http://kristofmattei.be/2011/09/12/silverlight-4-bug-in-tabcontrol-tabstripplacement-dock-left-and-onapplytemplate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At work I ran into a bug with the Silverlight <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.tabcontrol(v=VS.95).aspx">TabControl</a>, more specifically when setting the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.tabcontrol.tabstripplacement(v=VS.95).aspx">TabStripPlacement</a> to <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.dock(v=VS.95).aspx#Y0">Dock.Left</a> and hiding one of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.tabitem(v=VS.95).aspx">TabItems</a> in the parent’s <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.onapplytemplate(v=vs.95).aspx">OnApplyTemplate</a>.</p>
<p>The way to get to this bug is quite specific. For example, you can’t do it when you’re using a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=VS.95).aspx">UserControl</a>, since in that kind of class OnApplyTemplate is <a href="http://forums.silverlight.net/p/14765/48752.aspx">never called</a>.</p>
<p>You need to build a class which inherits from <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.control(v=VS.95).aspx">Control</a>.</p>
<pre class="code"><span style="color: blue;">public class </span><span style="color: #2b91af;">TabControlTest </span>: <span style="color: #2b91af;">Control</span></pre>
<p>Then we have the style of this particular class:</p>
<pre class="code"><span style="color: blue;">&lt;</span><span style="color: #a31515;">Style </span><span style="color: red;">TargetType</span><span style="color: blue;">="TabControlTest:TabControlTest"&gt; &lt;</span><span style="color: #a31515;">Setter </span><span style="color: red;">Property</span><span style="color: blue;">="Template"&gt; &lt;</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">ControlTemplate </span><span style="color: red;">TargetType</span><span style="color: blue;">="TabControlTest:TabControlTest"&gt; &lt;</span><span style="color: #a31515;">Grid </span><span style="color: red;">x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">="LayoutRoot" </span><span style="color: red;">Background</span><span style="color: blue;">="White"&gt; &lt;</span><span style="color: #a31515;">Grid.RowDefinitions</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">RowDefinition </span><span style="color: red;">Height</span><span style="color: blue;">="*" /&gt; &lt;</span><span style="color: #a31515;">RowDefinition </span><span style="color: red;">Height</span><span style="color: blue;">="38" /&gt; &lt;/</span><span style="color: #a31515;">Grid.RowDefinitions</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabControl </span><span style="color: red;">TabStripPlacement</span><span style="color: blue;">="{</span><span style="color: #a31515;">TemplateBinding </span><span style="color: red;">TabStripPlacement</span><span style="color: blue;">}" </span><span style="color: red;">Grid.Row</span><span style="color: blue;">="0"&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem </span><span style="color: red;">Header</span><span style="color: blue;">="First"&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem.Content</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">TextBlock </span><span style="color: red;">Text</span><span style="color: blue;">="A" /&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem.Content</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem </span><span style="color: red;">Header</span><span style="color: blue;">="Second" </span><span style="color: red;">x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">="middleTabItem"&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem.Content</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">TextBlock </span><span style="color: red;">Text</span><span style="color: blue;">="B" /&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem.Content</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem </span><span style="color: red;">Header</span><span style="color: blue;">="Last"&gt; &lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem.Content</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">TextBlock </span><span style="color: red;">Text</span><span style="color: blue;">="C" /&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem.Content</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabItem</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabControl</span><span style="color: blue;">&gt; &lt;</span><span style="color: #a31515;">Button </span><span style="color: red;">HorizontalAlignment</span><span style="color: blue;">="Center" </span><span style="color: red;">Grid.Row</span><span style="color: blue;">="1" </span><span style="color: red;">Content</span><span style="color: blue;">="{</span><span style="color: #a31515;">TemplateBinding </span><span style="color: red;">ShowOrHide</span><span style="color: blue;">}" </span><span style="color: red;">Command</span><span style="color: blue;">="{</span><span style="color: #a31515;">TemplateBinding </span><span style="color: red;">ShowOrHideCommand</span><span style="color: blue;">}" /&gt; &lt;/</span><span style="color: #a31515;">Grid</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">Setter</span><span style="color: blue;">&gt; &lt;/</span><span style="color: #a31515;">Style</span><span style="color: blue;">&gt;</span></pre>
<p>As you can see I’ve named the middle TabItem (“middleTabItem”) so I can retrieve it in the OnApplyTemplate:</p>
<pre class="code"><span style="color: blue;">public override void </span>OnApplyTemplate()
{
    <span style="color: blue;">this</span>._middleTabItem = (<span style="color: #2b91af;">TabItem</span>) <span style="color: blue;">this</span>.GetTemplateChild(<span style="color: #a31515;">"middleTabItem"</span>);
    <span style="color: blue;">this</span>._middleTabItem.Visibility = <span style="color: #2b91af;">Visibility</span>.Collapsed;

    <span style="color: blue;">base</span>.OnApplyTemplate();
}</pre>
<p>Now if the following 2 conditions are matched:</p>
<ol>
<li><strong>TabStripPlacement is set to Dock.Left </strong>(it doesn’t happen on ‘Top’)</li>
<li>We set the <a href="http://msdn.microsoft.com/en-us/library/system.windows.visibility(v=VS.95).aspx">Visibility</a> of the said TabItem to Collapsed in the OnApplyTemplate of the surrounding Control</li>
</ol>
<p>Then we cannot make the TabItem Visible anymore.</p>
<p>To show or hide I use the following code:</p>
<pre class="code"><span style="color: blue;">this</span>.ShowOrHideCommand = <span style="color: blue;">new </span><span style="color: #2b91af;">Command</span>(() =&gt;
                                         {
                                            <span style="color: blue;">this</span>.ShowOrHideText = <span style="color: blue;">string</span>.Format(SwitchTo, <span style="color: blue;">this</span>._middleTabItem.Visibility);
                                            <span style="color: blue;">this</span>._middleTabItem.Visibility = <span style="color: blue;">this</span>._middleTabItem.Visibility == <span style="color: #2b91af;">Visibility</span>.Visible ? <span style="color: #2b91af;">Visibility</span>.Collapsed : <span style="color: #2b91af;">Visibility</span>.Visible;
                                         });</pre>
<p>The solution to this is to call the OnApplyTemplate of the TabControl itself so that it recalculates it’s children’s location (or something, I’m not sure):</p>
<p>Give the TabControl a name, fetch it in OnApplyTemplate of your Control:</p>
<pre class="code"><span style="color: blue;">&lt;</span><span style="color: #a31515;">sdk</span><span style="color: blue;">:</span><span style="color: #a31515;">TabControl </span><span style="color: red;">TabStripPlacement</span><span style="color: blue;">="{</span><span style="color: #a31515;">TemplateBinding </span><span style="color: red;">TabStripPlacement</span><span style="color: blue;">}" </span><span style="color: red;">x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">="TabControl" </span><span style="color: red;">Grid.Row</span><span style="color: blue;">="0"&gt;</span></pre>
<pre class="code"><span style="color: blue;">public override void </span>OnApplyTemplate()
{
    <span style="color: blue;">this</span>._middleTabItem = (<span style="color: #2b91af;">TabItem</span>) <span style="color: blue;">this</span>.GetTemplateChild(<span style="color: #a31515;">"middleTabItem"</span>);
    <span style="color: blue;">this</span>._middleTabItem.Visibility = <span style="color: #2b91af;">Visibility</span>.Collapsed;
    <span style="color: blue;">this</span>._tabControl = (<span style="color: #2b91af;">TabControl</span>) <span style="color: blue;">this</span>.GetTemplateChild(<span style="color: #a31515;">"TabControl"</span>);
    <span style="color: blue;">base</span>.OnApplyTemplate();
}</pre>
<p>And call the _tabControl’s OnApplyTemplate after switching the Visibility to Visible:</p>
<pre class="code"><span style="color: blue;">this</span>.ShowOrHideCommand = <span style="color: blue;">new </span><span style="color: #2b91af;">Command</span>(() =&gt;
                                         {
                                            <span style="color: blue;">this</span>.ShowOrHideText = <span style="color: blue;">string</span>.Format(SwitchTo, <span style="color: blue;">this</span>._middleTabItem.Visibility);
                                            <span style="color: blue;">this</span>._middleTabItem.Visibility = <span style="color: blue;">this</span>._middleTabItem.Visibility == <span style="color: #2b91af;">Visibility</span>.Visible ? <span style="color: #2b91af;">Visibility</span>.Collapsed : <span style="color: #2b91af;">Visibility</span>.Visible;
                                            <span style="color: blue;">this</span>._tabControl.OnApplyTemplate();
                                         });</pre>
<p>This will make the UI respond correctly. For your convenience I’ve added the SL project, and you can download it <a title="link to project" href="http://kristofmattei.be/wp-content/uploads/2011/09/TabControlTest.zip">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/lYyTrebJFJ4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/09/12/silverlight-4-bug-in-tabcontrol-tabstripplacement-dock-left-and-onapplytemplate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/09/12/silverlight-4-bug-in-tabcontrol-tabstripplacement-dock-left-and-onapplytemplate/</feedburner:origLink></item>
		<item>
		<title>I won!</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/7mQNz5nEzeo/</link>
		<comments>http://kristofmattei.be/2011/06/30/i-won/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 15:11:22 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/2011/06/30/i-won/</guid>
		<description><![CDATA[In reference to my last post: I won! YES! I’m going to Build! Anaheim, CA is in Germany. Right? Like Hanover is in England. Right?]]></description>
			<content:encoded><![CDATA[<p>In reference to my last <a href="http://kristofmattei.be/2011/06/23/community-day-2011-win-the-ticket-build-in-anaheim-ca/" target="_blank">post</a>: I won! YES! </p>
<p>I’m going to <a href="http://www.buildwindows.com" target="_blank">Build</a>!</p>
<p>Anaheim, CA is in Germany. Right? Like Hanover is in England. Right? </p>
<p><img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smilewithtongueout" alt="Smile with tongue out" src="http://kristofmattei.be/wp-content/uploads/2011/06/wlEmoticon-smilewithtongueout.png"></p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/7mQNz5nEzeo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/06/30/i-won/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/06/30/i-won/</feedburner:origLink></item>
		<item>
		<title>Community Day 2011: Win the ticket Build in Anaheim, CA!</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/MIJy-RlmlR4/</link>
		<comments>http://kristofmattei.be/2011/06/23/community-day-2011-win-the-ticket-build-in-anaheim-ca/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 10:11:00 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/2011/06/23/community-day-2011-win-the-ticket-build-in-anaheim-ca/</guid>
		<description><![CDATA[On September 13th until September 16th Microsoft hosts the Build event in Anaheim California. You can find more information on www.buildwindows.com ! With this I hope to win the ticket to Build, here at Community Day 2011! It would be &#8230; <a href="http://kristofmattei.be/2011/06/23/community-day-2011-win-the-ticket-build-in-anaheim-ca/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On September 13th until September 16th Microsoft hosts the Build event in Anaheim California. You can find more information on <a href="http://www.buildwindows.com">www.buildwindows.com</a> !</p>
<p>With this I hope to win the ticket to Build, here at <a href="http://communityday.be/" target="_blank">Community Day 2011</a>! It would be awesome for me to get the first look at Windows 8! </p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/06/fdvjh.jpg" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Me and some Smurfs" border="0" alt="Me and some Smurfs" src="http://kristofmattei.be/wp-content/uploads/2011/06/fdvjh_thumb.jpg" width="244" height="183"></a></p>
<p>You can follow <a href="https://twitter.com/#!/search?q=%23bldwin">#bldwin</a> also on Twitter! </p>
<p>Stay tuned and thumbs up for me! (please <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-openmouthedsmile" alt="Open-mouthed smile" src="http://kristofmattei.be/wp-content/uploads/2011/06/wlEmoticon-openmouthedsmile.png">)</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/MIJy-RlmlR4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/06/23/community-day-2011-win-the-ticket-build-in-anaheim-ca/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/06/23/community-day-2011-win-the-ticket-build-in-anaheim-ca/</feedburner:origLink></item>
		<item>
		<title>Clear all event logs on Windows using PowerShell</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/W10zqyeeRzY/</link>
		<comments>http://kristofmattei.be/2011/06/14/clear-all-event-logs-on-windows-using-powershell/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 12:16:34 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[7]]></category>
		<category><![CDATA[OS]]></category>
		<category><![CDATA[Various]]></category>
		<category><![CDATA[Vista]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/?p=932</guid>
		<description><![CDATA[I was bored with the vast amount of data in the eventlogs which were really not useful for me. So, in order to improve readability on my machine I decided to look for something to clear all of the eventlogs. &#8230; <a href="http://kristofmattei.be/2011/06/14/clear-all-event-logs-on-windows-using-powershell/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was bored with the vast amount of data in the eventlogs which were really not useful for me. So, in order to improve readability on my machine I decided to look for something to clear all of the eventlogs. Easy.</p>
<p>Since I always use the Administrative Events filter to view every warning and error I get a lot of junk (who cares for Kernel-Power warnings?)</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/06/all-errors.png" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Administrative Events" border="0" alt="Administrative Events" src="http://kristofmattei.be/wp-content/uploads/2011/06/all-errors_thumb.png" width="238" height="244"></a></p>
<p>Since I didn&#8217;t feel like doing the following steps for each frigging event log there is on my machine. You would need to go to the following steps:</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/06/step_1.png" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Step 1" border="0" alt="Step 1" src="http://kristofmattei.be/wp-content/uploads/2011/06/step_1_thumb.png" width="236" height="328"></a></p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/06/step_2.png" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Step 2" border="0" alt="Step 2" src="http://kristofmattei.be/wp-content/uploads/2011/06/step_2_thumb.png" width="430" height="93"></a></p>
<p>Now this is an excerpt&nbsp; from the eventlogs I have on this machine:</p>
<pre>Analytic
Application
DirectShowFilterGraph
DirectShowPluginControl
EndpointMapper
ForwardedEvents
HardwareEvents
Internet Explorer
Key Management Service
MF_MediaFoundationDeviceProxy
MediaFoundationDeviceProxy
MediaFoundationPerformance
MediaFoundationPipeline
MediaFoundationPlatform
Microsoft-IE/Diagnostic
Microsoft-IEDVTOOL/Diagnostic
Microsoft-IEFRAME/Diagnostic
Microsoft-IIS-Configuration/Administrative
Microsoft-IIS-Configuration/Analytic
Microsoft-IIS-Configuration/Debug
Microsoft-IIS-Configuration/Operational
Microsoft-PerfTrack-IEFRAME/Diagnostic
Microsoft-PerfTrack-MSHTML/Diagnostic
Microsoft-Windows-ADSI/Debug
Microsoft-Windows-API-Tracing/Operational
Microsoft-Windows-ATAPort/General
Microsoft-Windows-ATAPort/SATA-LPM
Microsoft-Windows-ActionQueue/Analytic
Microsoft-Windows-AltTab/Diagnostic
Microsoft-Windows-AppID/Operational
Microsoft-Windows-AppLocker/EXE and DLL
Microsoft-Windows-AppLocker/MSI and Script
Microsoft-Windows-Application Server-Applications/Admin
Microsoft-Windows-Application Server-Applications/Analytic
Microsoft-Windows-Application Server-Applications/Debug</pre>
<p>And so on (for about 10 times as large). I’m not going to clear them by hand.</p>
<p>So let’s call Powershell to the rescue! (Play Thunderbirds theme song!)</p>
<p>First of all (and nothing to do with Powershell): <a href="http://technet.microsoft.com/en-us/library/cc732848(WS.10).aspx" target="_blank">wevtutil</a></p>
<p>We’re going to use this tool to display every available event source on this machine:</p>
<pre>wevtutil el</pre>
<p>The help states:
<pre>el | enum-logs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List log names.</pre>
<p>Good, that’s what we need. Next up, we pass every line of this list to a command using a pipe and the Powershell <a href="http://technet.microsoft.com/en-us/library/dd347608.aspx" target="_blank">Foreach-Object</a> cmdlet</p>
<pre>wevtutil el | Foreach-Object { … commands go here … }</pre>
<p>The commands are going to be</p>
<pre>wevtutil cl “$_”</pre>
<p>The help states:
<pre>cl | clear-log&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Clear a log.</pre>
<p>And <abbr title="Within the script block, the current input object is represented by the $_ variable.">$_</abbr> is the current variable in the enumeration of Foreach-Object. I added the quotes since there are event sources with spaces and we need to have the full name in order to have wevtutil to be able to clear that log.</p>
<p>Now let’s add some diagnostics output to see which one we’re currently clearing:</p>
<pre>wevtutil el | Foreach-Object {Write-Host "Clearing $_"; wevtutil cl "$_"}</pre>
<p>Now just run it through Powershell, and bam, a clean event log.</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/06/image.png" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Result" border="0" alt="Result" src="http://kristofmattei.be/wp-content/uploads/2011/06/image_thumb.png" width="353" height="242"></a></p>
<p>Cheers!</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/W10zqyeeRzY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/06/14/clear-all-event-logs-on-windows-using-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/06/14/clear-all-event-logs-on-windows-using-powershell/</feedburner:origLink></item>
		<item>
		<title>Visual Studio 2010 SP1: DebuggerTypeProxy works again! *yay*</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/xcXrghW0UsY/</link>
		<comments>http://kristofmattei.be/2011/03/20/visual-studio-2010-sp1-debuggertypeproxy-works-again-yay/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 16:43:43 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/2011/03/20/visual-studio-2010-sp1-debuggertypeproxy-works-again-yay/</guid>
		<description><![CDATA[A while ago I bumped into a bug in Visual Studio 2010 + Silverlight 4. While working with a List / Dictionary / … it didn’t show the items in the list, instead it showed the normal ‘raw view’: As &#8230; <a href="http://kristofmattei.be/2011/03/20/visual-studio-2010-sp1-debuggertypeproxy-works-again-yay/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A while ago I bumped into a bug in Visual Studio 2010 + Silverlight 4. </p>
<p>While working with a List / Dictionary / … it didn’t show the items in the list, instead it showed the normal ‘raw view’:</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/03/silverlight4Debug1.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="No debug view" border="0" alt="No debug view" src="http://kristofmattei.be/wp-content/uploads/2011/03/silverlight4Debug1_thumb.png" width="244" height="77"></a></p>
<p>As you can see, there was no view to see the items.</p>
<p>What you would expect to see is this:</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/03/consoleDebug1.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="DebugView!" border="0" alt="DebugView!" src="http://kristofmattei.be/wp-content/uploads/2011/03/consoleDebug1_thumb.png" width="244" height="78"></a></p>
<p>I reported the bug (many others did too!). The problem was that it was fixed in SL3, but not ported to SL4.</p>
<p>Now it’s fixed! This makes my work a lot easier. When we get SP1 @ work too that is!</p>
<p>Cheers!</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/xcXrghW0UsY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/03/20/visual-studio-2010-sp1-debuggertypeproxy-works-again-yay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/03/20/visual-studio-2010-sp1-debuggertypeproxy-works-again-yay/</feedburner:origLink></item>
		<item>
		<title>.NET natural sort, a possible solution.</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/T5qYfY5Oq-E/</link>
		<comments>http://kristofmattei.be/2011/03/19/net-natural-sort-a-possible-solution/#comments</comments>
		<pubDate>Sat, 19 Mar 2011 17:51:55 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/2011/03/19/net-natural-sort-a-possible-solution/</guid>
		<description><![CDATA[You have a list of items, let’s say 20 elements, starting at 1, until 20 (inclusive). We shuffle the list and we try to sort it. List&#60;string&#62; normalDotNetSort = GiveMeANewList(); The code to create the list: private static List&#60;string&#62; GiveMeANewList() &#8230; <a href="http://kristofmattei.be/2011/03/19/net-natural-sort-a-possible-solution/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><font color="#333333">You have a list of items, let’s say 20 elements, starting at 1, until 20 (inclusive). We shuffle the list and we try to sort it.</font></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c990ef82-c754-4d5f-804f-ad842915aae3" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;">
<li><span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">string</span>&gt; normalDotNetSort = GiveMeANewList();</li>
</ol></div>
</p></div>
</p></div>
<p>The code to create the list:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7537a55c-def1-4d5e-8163-6e48e2caf001" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">private</span> <span style="color:#0000ff">static</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">string</span>&gt; GiveMeANewList()</li>
<li style="background: #f3f3f3">{</li>
<li>    <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">string</span>&gt; list = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">string</span>&gt;();</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>    <span style="color:#0000ff">for</span> (<span style="color:#0000ff">int</span> i = 1; i &lt;= 20; i++)</li>
<li style="background: #f3f3f3">    {</li>
<li>        list.Add(<span style="color:#0000ff">string</span>.Format(<span style="color:#a31515">&quot;{0}&quot;</span>, i));</li>
<li style="background: #f3f3f3">    }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">    <span style="color:#0000ff">return</span> list.MixList();</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p><a href="http://www.vcskicks.com/randomize_array.php" target="_blank">MixList</a> just shuffles the list.</p>
<p>We’re using a normal Generic <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" target="_blank">List</a> and to sort, let’s use the <a href="http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx" target="_blank">Sort</a> method, and write it to the console:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:46936076-fc87-4967-82a4-580e5018b167" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;">
<li>normalDotNetSort.Sort();</li>
<li style="background: #f3f3f3">normalDotNetSort.ForEach(<span style="color:#2b91af">Console</span>.WriteLine);</li>
</ol></div>
</p></div>
</p></div>
<p>What would be the result?</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>.</p>
<blockquote><p>1<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />2<br />20<br />3<br />4<br />5<br />6<br />7<br />8<br />9</p>
</blockquote>
<p><font color="#555555">That’s not what we wanted, but it is what we told the code to do. It first sorts on the first character, and then on the second (and so on…). That’s why 19 takes precedence of 2, since 1 &lt; 2.</font></p>
<p><font color="#555555">How do we fix this? </font></p>
<p><font color="#555555">There is a little gem in the shlwapi.dll, namely <a href="http://msdn.microsoft.com/en-us/library/bb759947(v=vs.85).aspx" target="_blank">StrCmpLogicalW</a>. Since this is a native function we need to do a <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx" target="_blank">DllImport</a> to expose the function to our C# code:</font></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a76b4c22-33f6-434f-a143-335d9706a1a8" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">SafeNativeMethods</span></li>
<li style="background: #f3f3f3">{</li>
<li>    [<span style="color:#2b91af">DllImport</span>(<span style="color:#a31515">&quot;shlwapi.dll&quot;</span>, CharSet = <span style="color:#2b91af">CharSet</span>.Unicode)]</li>
<li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">extern</span> <span style="color:#0000ff">int</span> StrCmpLogicalW(<span style="color:#0000ff">string</span> psz1, <span style="color:#0000ff">string</span> psz2);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>Now we can use this function in our own string comparer:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bad149fa-4918-49af-ab4a-26345f04c18a" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">public</span> <span style="color:#0000ff">sealed</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">NaturalStringComparer</span> : <span style="color:#2b91af">IComparer</span>&lt;<span style="color:#0000ff">string</span>&gt;</li>
<li style="background: #f3f3f3">{</li>
<li>    <span style="color:#0000ff">#region</span> IComparer&lt;string&gt; Members</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span> Compare(<span style="color:#0000ff">string</span> x, <span style="color:#0000ff">string</span> y)</li>
<li style="background: #f3f3f3">    {</li>
<li>        <span style="color:#0000ff">return</span> <span style="color:#2b91af">SafeNativeMethods</span>.StrCmpLogicalW(x, y);</li>
<li style="background: #f3f3f3">    }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">    <span style="color:#0000ff">#endregion</span></li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>And use this comparer to sort our list:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d2069908-9f41-4c59-a4d8-13e3bfaea1bf" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;">
<li><span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">string</span>&gt; interopSort = GiveMeANewList();</li>
<li style="background: #f3f3f3">interopSort.Sort(<span style="color:#0000ff">new</span> <span style="color:#2b91af">NaturalStringComparer</span>());</li>
<li>interopSort.ForEach(<span style="color:#2b91af">Console</span>.WriteLine);</li>
</ol></div>
</p></div>
</p></div>
<p>And the result:</p>
<blockquote><p>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20</p>
</blockquote>
<p><font color="#555555">Much better no? </font></p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/T5qYfY5Oq-E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/03/19/net-natural-sort-a-possible-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/03/19/net-natural-sort-a-possible-solution/</feedburner:origLink></item>
		<item>
		<title>Dictionary&lt;TKey, TValue&gt; finding key: try/catch vs TryGetValue</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/_X1a6VsRSGc/</link>
		<comments>http://kristofmattei.be/2011/03/17/dictionarytkey-tvalue-finding-key-trycatch-vs-trygetvalue/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 11:26:50 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/?p=1064</guid>
		<description><![CDATA[Today I saw some code where the developer accessed a dictionary like this: void Foo(int keyToFindInDictionary) {     Dictionary&#60;int, string&#62; dictionary = new Dictionary&#60;int, string&#62;(); &#160;     try     {         DoSomethingWithTheResult(dictionary[keyToFindInDictionary]);     }     catch (KeyNotFoundException)     {         // fallback     } } &#160; private void DoSomethingWithTheResult(string &#8230; <a href="http://kristofmattei.be/2011/03/17/dictionarytkey-tvalue-finding-key-trycatch-vs-trygetvalue/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I saw some code where the developer accessed a dictionary like this:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ad78bf8b-328d-4ca7-8011-e4188f880cb5" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px; white-space: nowrap">
<li><span style="color:#0000ff">void</span> Foo(<span style="color:#0000ff">int</span> keyToFindInDictionary)</li>
<li style="background: #f3f3f3">{</li>
<li>    <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">int</span>, <span style="color:#0000ff">string</span>&gt; dictionary = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">int</span>, <span style="color:#0000ff">string</span>&gt;();</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>    <span style="color:#0000ff">try</span></li>
<li style="background: #f3f3f3">    {</li>
<li>        DoSomethingWithTheResult(dictionary[keyToFindInDictionary]);</li>
<li style="background: #f3f3f3">    }</li>
<li>    <span style="color:#0000ff">catch</span> (<span style="color:#2b91af">KeyNotFoundException</span>)</li>
<li style="background: #f3f3f3">    {</li>
<li>        <span style="color:#008000">// fallback</span></li>
<li style="background: #f3f3f3">    }</li>
<li>}</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li><span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> DoSomethingWithTheResult(<span style="color:#0000ff">string</span> s)</li>
<li style="background: #f3f3f3">{</li>
<li>    <span style="color:#008000">// do something</span></li>
<li style="background: #f3f3f3">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>As you can see he catches the exception to handle the fact that the given key was not found in the dictionary.</p>
<p>I told him that there was a better way, i.e. <a href="http://msdn.microsoft.com/en-us/library/bb347013.aspx" target="_blank">Dictionary&lt;TKey, TValue&gt;.TryGetValue</a>.</p>
<p>And then the curious part of me started thinking, how much better is it? Is it faster? Slower? </p>
<p>So I wrote the following small piece of code to test my statement that TryGetValue is better than catching the <a href="http://msdn.microsoft.com/en-us/library/system.collections.generic.keynotfoundexception.aspx" target="_blank">KeyNotFoundException</a>.</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ce97ad45-28f4-4966-ba1f-2f7390930994" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px; white-space: nowrap">
<li><span style="color:#0000ff">namespace</span> DictionaryTryGetValueVersusException</li>
<li style="background: #f3f3f3">{</li>
<li>    <span style="color:#0000ff">using</span> System;</li>
<li style="background: #f3f3f3">    <span style="color:#0000ff">using</span> System.Collections.Generic;</li>
<li>    <span style="color:#0000ff">using</span> System.Diagnostics;</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>    <span style="color:#0000ff">internal</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Program</span></li>
<li style="background: #f3f3f3">    {</li>
<li>        <span style="color:#0000ff">private</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> Main()</li>
<li style="background: #f3f3f3">        {</li>
<li>            <span style="color:#008000">// create a dictionary with some values</span></li>
<li style="background: #f3f3f3">            <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#0000ff">string</span>&gt; dictionary = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#0000ff">string</span>&gt;();</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            <span style="color:#008000">// add 10,000 items</span></li>
<li>            <span style="color:#0000ff">for</span> (<span style="color:#0000ff">int</span> i = 0; i &lt; 10000; i++)</li>
<li style="background: #f3f3f3">            {</li>
<li>                <span style="color:#0000ff">if</span> (i == 5000)</li>
<li style="background: #f3f3f3">                {</li>
<li>                    <span style="color:#008000">// skip</span></li>
<li style="background: #f3f3f3">                    <span style="color:#0000ff">continue</span>;</li>
<li>                }</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>                dictionary.Add(<span style="color:#0000ff">string</span>.Format(<span style="color:#a31515">&quot;Key{0}&quot;</span>, i), <span style="color:#0000ff">string</span>.Format(<span style="color:#a31515">&quot;Value{0}&quot;</span>, i));</li>
<li style="background: #f3f3f3">            }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            <span style="color:#2b91af">Stopwatch</span> stopwatch = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Stopwatch</span>();</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            stopwatch.Start();</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            <span style="color:#0000ff">const</span> <span style="color:#0000ff">string</span> keyToFindInDictionary = <span style="color:#a31515">&quot;Key5000&quot;</span>;</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            <span style="color:#0000ff">for</span> (<span style="color:#0000ff">int</span> i = 0; i &lt; 10000; i++)</li>
<li>            {</li>
<li style="background: #f3f3f3">                <span style="color:#008000">// 5000 does not exist</span></li>
<li>                <span style="color:#0000ff">try</span></li>
<li style="background: #f3f3f3">                {</li>
<li>                    <span style="color:#008000">// execute a method on the variable, so that he compiler doesn&#39;t optimize it.</span></li>
<li style="background: #f3f3f3">                    dictionary[keyToFindInDictionary];</li>
<li>                }</li>
<li style="background: #f3f3f3">                <span style="color:#0000ff">catch</span> (<span style="color:#2b91af">Exception</span>)</li>
<li>                {</li>
<li style="background: #f3f3f3">                    <span style="color:#008000">// ignore</span></li>
<li>                }</li>
<li style="background: #f3f3f3">            }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            stopwatch.Stop();</li>
<li>            <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Dictionary lookup with try/catch took: {0}&quot;</span>, stopwatch.ElapsedTicks);</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>            stopwatch.Restart();</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>            <span style="color:#0000ff">for</span> (<span style="color:#0000ff">int</span> i = 0; i &lt; 10000; i++)</li>
<li style="background: #f3f3f3">            {</li>
<li>                <span style="color:#0000ff">string</span> notFound;</li>
<li style="background: #f3f3f3">                <span style="color:#008000">// 5000 does not exist</span></li>
<li>                dictionary.TryGetValue(keyToFindInDictionary, <span style="color:#0000ff">out</span> notFound);</li>
<li style="background: #f3f3f3">            }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">            stopwatch.Stop();</li>
<li>            <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Dictionary lookup with TryGetValue took: {0}&quot;</span>, stopwatch.ElapsedTicks);</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>            <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Done, press enter to exit&quot;</span>);</li>
<li style="background: #f3f3f3">            <span style="color:#2b91af">Console</span>.ReadLine();</li>
<li>        }</li>
<li style="background: #f3f3f3">    }</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>As you can see we add 10,000 items to the dictionary, skipping the 5000th item, and then we do a lookup in the dictionary, 10,000 times, on the key that doesn’t exist, to measure the performance difference between the 2 previously discussed methods.</p>
<p>To run this test I did a Release Build in Visual Studio 2010, .NET 4.0, Any CPU, and this is the result:</p>
<p><a href="http://kristofmattei.be/wp-content/uploads/2011/03/image.png" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Results" border="0" alt="Results" src="http://kristofmattei.be/wp-content/uploads/2011/03/image_thumb.png" width="561" height="285"></a></p>
<blockquote><p>Dictionary lookup with try/catch took: 1069625<br />Dictionary lookup with TryGetValue took: 864<br />Done, press enter to exit</p>
</blockquote>
<p>As you can see there is a HUGE difference. The TryGetValue-way was about 1237 faster! </p>
<p>Exception throwing is very heavy! Don’t forget that. It doesn’t mean that you need to program without exceptions, but when you EXPECT that a Key is not present in a given dictionary, don’t use the exception. Use the TryGetValue. If you always expect it to be there then it’s normal to catch the exception, because it’s exceptional that the key was not found.</p>
<p>PS: I tried the code again by skipping the 9999th item, and the results were about the same, take or leave a few milliseconds. So searching in the dictionary is very performant (close to O(1), as written in the Remarks section on this <a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx" target="_blank">page</a>.)</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/_X1a6VsRSGc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/03/17/dictionarytkey-tvalue-finding-key-trycatch-vs-trygetvalue/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/03/17/dictionarytkey-tvalue-finding-key-trycatch-vs-trygetvalue/</feedburner:origLink></item>
		<item>
		<title>TemplateBinding, what is it?</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/_EHPOB_tj1Y/</link>
		<comments>http://kristofmattei.be/2011/01/30/templatebinding-what-is-it/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 16:57:15 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/2011/01/30/templatebinding-what-is-it/</guid>
		<description><![CDATA[I spent ages on trying to comprehend what it means. It’s just this: {TemplateBinding Content} Equals {Binding Content, RelativeSource={RelativeSource Path=TemplatedParent}} http://msdn.microsoft.com/en-us/library/ms742882.aspx MSDN FTW!]]></description>
			<content:encoded><![CDATA[<p>I spent ages on trying to comprehend what it means.</p>
<p>It’s just this:</p>
<pre>{TemplateBinding Content}</pre>
<p>Equals</p>
<pre>{Binding Content, RelativeSource={RelativeSource Path=TemplatedParent}}</pre>
<p><a title="http://msdn.microsoft.com/en-us/library/ms742882.aspx" href="http://msdn.microsoft.com/en-us/library/ms742882.aspx">http://msdn.microsoft.com/en-us/library/ms742882.aspx</a></p>
<p>MSDN FTW!</p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/_EHPOB_tj1Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/01/30/templatebinding-what-is-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/01/30/templatebinding-what-is-it/</feedburner:origLink></item>
		<item>
		<title>Stopping Casini before (re)building website in Visual Studio revisited</title>
		<link>http://feedproxy.google.com/~r/KristofsBlog/~3/PnZHIYmwqQc/</link>
		<comments>http://kristofmattei.be/2011/01/30/stopping-casini-before-rebuilding-website-in-visual-studio-revisited/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 16:40:17 +0000</pubDate>
		<dc:creator>Kristof</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://kristofmattei.be/2011/01/30/stopping-casini-before-rebuilding-website-in-visual-studio-revisited/</guid>
		<description><![CDATA[This is an update of an older post of mine on stopping Casini. The code posted in the previous article (yes, read it!) only stops Webdev.Webserver, which is included in Microsoft Visual Studio 2010. Now the problem is that when &#8230; <a href="http://kristofmattei.be/2011/01/30/stopping-casini-before-rebuilding-website-in-visual-studio-revisited/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is an update of an older post of mine on <a href="http://kristofmattei.be/2010/10/22/restarting-casini-before-building/" target="_blank">stopping Casini</a>.</p>
<p>The code posted in the previous article (yes, read it!) only stops Webdev.Webserver, which is included in Microsoft Visual Studio 2010.</p>
<p>Now the problem is that when you’re using Blend and accidently run the app from there (which I hate, I’d prefer it to switch to Visual Studio).</p>
<p>This shortcut will allow you to quickly kill the webdev server of Blend (and that of Visual Studio too). So just keep it as a shortcut.</p>
<pre>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Stop-Process -Name @('WebDev.WebServer*', 'Microsoft.Expression.Web*')</pre>
<p>Good luck <img src='http://kristofmattei.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<img src="http://feeds.feedburner.com/~r/KristofsBlog/~4/PnZHIYmwqQc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kristofmattei.be/2011/01/30/stopping-casini-before-rebuilding-website-in-visual-studio-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kristofmattei.be/2011/01/30/stopping-casini-before-rebuilding-website-in-visual-studio-revisited/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 1.016 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-02-09 01:30:14 --><!-- Compression = gzip -->

