<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" 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/"><channel><title>jaredpar's WebLog</title><link>http://blogs.msdn.com/jaredpar/default.aspx</link><description>Code, rants and ramblings of a programmer.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>VsVim Update Released (Version 0.7.0)</title><link>http://blogs.msdn.com/jaredpar/archive/2010/02/04/vsvim-update-released-version-0-7-0.aspx</link><pubDate>Thu, 04 Feb 2010 18:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958328</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9958328.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9958328</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9958328</wfw:comment><description>&lt;p&gt;I just released an update to VsVim for Visual Studio 2010 Beta2.&amp;nbsp; This should be available shortly from the extension manager in Visual Studio or it can be downloaded directly at the following link&lt;/p&gt;  &lt;p&gt;Link: &lt;a mce_href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329" href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329"&gt;http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;New Features&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Visual Mode Support      &lt;ul&gt;       &lt;li&gt;Character, Line and Block Mode &lt;/li&gt;        &lt;li&gt;All movement commands supported &lt;/li&gt;        &lt;li&gt;Basic insert-delete operations added &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Command Mode Commands - :substitute, :redo, &lt;/li&gt;    &lt;li&gt;Support for mark navigation to global marks &lt;/li&gt;    &lt;li&gt;Implemented almost all delete-insert operations in relevant modes &lt;/li&gt;    &lt;li&gt;Normal Mode Commands: CTRL-R, &lt;/li&gt;    &lt;li&gt;Expanded range parsing &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Notable Bug Fixes&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Issues with disabling conflicting key bindings on startup &lt;/li&gt;    &lt;li&gt;Issues with not properly intercepting standard VS commands and routing them to Vim &lt;/li&gt;    &lt;li&gt;Timing issue on startup that would cause certain components to not get created &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Source Code&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Source for this release is available at the following location.&amp;nbsp; It is released under the &lt;a mce_href="http://msdn.microsoft.com/en-us/library/cc707818.aspx" href="http://msdn.microsoft.com/en-us/library/cc707818.aspx"&gt;MS-PL&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a title="http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.7.0.zip" mce_href="http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.7.0.zip" href="http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.7.0.zip"&gt;http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.7.0.zip&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The next planned major update is version 0.8.0.&amp;nbsp; It will consist of the following updates&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Expanded set of commands supported in all modes &lt;/li&gt;    &lt;li&gt;The . operator in normal mode &lt;/li&gt;    &lt;li&gt;Ability for users to change settings.&amp;nbsp; Settings supported is baked into VsVim I just haven’t exposed it via command mode or any UI &lt;/li&gt;    &lt;li&gt;Better UI in general.&amp;nbsp; Right now I use MessageBox.Show for any UI I need to display other than status bar updates.&amp;nbsp; I would like to expand that to include a more configurable UI &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I also expect to release a 0.7.1 version once the Visual Studio RC goes public.&amp;nbsp; As part of releasing RC, all Beta2 extensions will be unpublished in the gallery and I’ll need to release an RC compatible version.&amp;nbsp; At that time I will provide a link to the Beta2 binaries for those not upgrading to the RC.&amp;nbsp; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;The usual caveats and expectations&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This extension is being released by me, not by Microsoft.&amp;nbsp; As such the support level for this extension is equivalent to the amount of free time I have to put into it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958328" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VsVim/default.aspx">VsVim</category></item><item><title>Having fun with events in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2010/02/03/having-fun-with-events-in-f.aspx</link><pubDate>Wed, 03 Feb 2010 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9956071</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9956071.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9956071</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9956071</wfw:comment><description>&lt;p&gt;Recently I ran into a situation where I needed to handle some events in F# in a special way.&amp;#160; In this particular case I wanted to be able to disable and re-enable my handler based on changes in the program.&amp;#160; Essentially the C# equivalent of continually adding and removing the handlers.&amp;#160; &lt;/p&gt;  &lt;p&gt;I started by using the F# Observable pattern.&amp;#160; Disposing of the handler when I was through with it and recreating it on demand.&amp;#160; This works great but after several uses I decided to write a full abstraction for it.&amp;#160;&amp;#160; For lack of a better name I call it ToggleHandler.&amp;#160; &lt;/p&gt;  &lt;pre class="code"&gt;[&amp;lt;AbstractClass&amp;gt;]
&lt;span style="color: blue"&gt;type internal &lt;/span&gt;ToggleHandler() =
    &lt;span style="color: blue"&gt;abstract &lt;/span&gt;IsHandling : bool
    &lt;span style="color: blue"&gt;abstract &lt;/span&gt;Add : unit &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;unit
    &lt;span style="color: blue"&gt;abstract &lt;/span&gt;Remove : unit &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;unit
   
    &lt;span style="color: blue"&gt;static member &lt;/span&gt;Create&amp;lt;'T&amp;gt; (source:System.IObservable&amp;lt;'T&amp;gt;) (func: 'T &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;unit) = ToggleHandler&amp;lt;'T&amp;gt;(source,func)
    &lt;span style="color: blue"&gt;static member &lt;/span&gt;Empty = 
        { &lt;span style="color: blue"&gt;new &lt;/span&gt;ToggleHandler() &lt;span style="color: blue"&gt;with 
            member &lt;/span&gt;x.Add() = ()
            &lt;span style="color: blue"&gt;member &lt;/span&gt;x.Remove() = () 
            &lt;span style="color: blue"&gt;member &lt;/span&gt;x.IsHandling = &lt;span style="color: blue"&gt;false &lt;/span&gt;}

&lt;span style="color: blue"&gt;and internal &lt;/span&gt;ToggleHandler&amp;lt;'T&amp;gt; 
    ( 
        _source : System.IObservable&amp;lt;'T&amp;gt;,
        _func : 'T &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;unit) =  
    &lt;span style="color: blue"&gt;inherit &lt;/span&gt;ToggleHandler()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;_handler : System.IDisposable option = None
    &lt;span style="color: blue"&gt;override &lt;/span&gt;x.IsHandling = Option.isSome _handler
    &lt;span style="color: blue"&gt;override &lt;/span&gt;x.Add() = 
        &lt;span style="color: blue"&gt;match &lt;/span&gt;_handler &lt;span style="color: blue"&gt;with
        &lt;/span&gt;| Some(_) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;failwith &lt;span style="color: maroon"&gt;&amp;quot;Already subcribed&amp;quot;
        &lt;/span&gt;| None &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;_handler &amp;lt;- _source |&amp;gt; Observable.subscribe _func |&amp;gt; Option.Some
    &lt;span style="color: blue"&gt;override &lt;/span&gt;x.Remove() =
        &lt;span style="color: blue"&gt;match &lt;/span&gt;_handler &lt;span style="color: blue"&gt;with
        &lt;/span&gt;| Some(actual) &lt;span style="color: blue"&gt;-&amp;gt; 
            &lt;/span&gt;actual.Dispose()
            _handler &amp;lt;- None
        | None &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;()&lt;/pre&gt;

&lt;p&gt;The design goal was to support my standard pattern for consuming events.&amp;#160; Typically I store all event handlers as let bindings within a type but the actual delegate handling the event is bound to a member.&amp;#160; Member declarations are not available in let bindings so creating an event handler becomes a 2 step process: defining in the let binding and then actually creating inside of a do binding. ToggleHandler facilitates this by providing a very easy let binding story. &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let mutable &lt;/span&gt;_clickHandler = ToggleHandler.Empty&lt;/pre&gt;

&lt;p&gt;The base class ToggleHandler is type independent so this will work for any event type.&amp;#160; Creating the real binding inside of the initial do binding is likewise as easy (and lacking explicit types).&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;do
    &lt;/span&gt;_clickHandler &amp;lt;- ToggleHandler.Create _button.Click this.OnButtonClick
    _clickHandler.Add()&lt;/pre&gt;

&lt;p&gt;Now I can toggle my event handler at any point in the application by calling Add/Remove.&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Full Sample:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;Form1() &lt;span style="color: blue"&gt;as &lt;/span&gt;this =
    &lt;span style="color: blue"&gt;inherit &lt;/span&gt;Form()

    &lt;span style="color: blue"&gt;let &lt;/span&gt;_button = &lt;span style="color: blue"&gt;new &lt;/span&gt;Button()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;_clickHandler = ToggleHandler.Empty

    &lt;span style="color: blue"&gt;do
        &lt;/span&gt;_clickHandler &amp;lt;- ToggleHandler.Create _button.Click this.OnButtonClick
        _clickHandler.Add()

    &lt;span style="color: blue"&gt;member private &lt;/span&gt;x.OnButtonClick (e:System.EventArgs) = 
        &lt;span style="color: green"&gt;// Handle Click 
        &lt;/span&gt;()

    &lt;span style="color: blue"&gt;member private &lt;/span&gt;x.ToggleHandler() =  
        &lt;span style="color: blue"&gt;if &lt;/span&gt;_clickHandler.IsHandling &lt;span style="color: blue"&gt;then &lt;/span&gt;_clickHandler.Remove()
        &lt;span style="color: blue"&gt;else &lt;/span&gt;_clickHandler.Add()&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9956071" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/F_2300_/default.aspx">F#</category></item><item><title>Easier script deployment in PowerShell 2.0</title><link>http://blogs.msdn.com/jaredpar/archive/2010/02/02/easier-script-deployment-in-powershell-2-0.aspx</link><pubDate>Tue, 02 Feb 2010 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9956063</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9956063.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9956063</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9956063</wfw:comment><description>&lt;p&gt;If you can’t tell from reading entries in my blog I’m a bit of a script junkie.&amp;#160; I loathe typing out the same command sequence more than once.&amp;#160; As such I go to great lengths to script as much as possible in life.&amp;#160; I also enjoy sharing my scripts with other members of my team.&lt;/p&gt;  &lt;p&gt;Unfortunately deployment of PowerShell scripts has long been a point of pain for me.&amp;#160; In the early 1.0 days PowerShell wasn’t installed by default on any OS so the only people with PowerShell were other scripter's.&amp;#160; Deploying to the masses simply wasn’t possible without installing PowerShell which is a significant adoption barrier.&amp;#160; Even if PowerShell was installed, the default setup forbids the execution of any script file.&amp;#160; Allowing scripts to run in 1.0 requires both Administrative privileges and a rather awkward command sequence: set-executionpolicy RemoteSigned.&amp;#160; &lt;/p&gt;  &lt;p&gt;These two combined to make it nearly impossible to distribute my scripts to the masses.&amp;#160; Other devs, after explanations, understood the problems I encountered.&amp;#160; But the masses simply did not.&amp;#160; The wanted a quick, one or two click solution to their problem.&amp;#160; Running a powershell script was simply too involved.&amp;#160; &lt;/p&gt;  &lt;p&gt;The best solution I found was to distribute both a .cmd and a .ps1 file.&amp;#160; The .cmd file’s job was to set the special registry key to allow script execution and then run the script.&amp;#160; This still hurt adoption because it required administrative privileges and an existing copy of powershell on the machine.&amp;#160; &lt;/p&gt;  &lt;p&gt;Thankfully the story is quite a bit better in 2.0&amp;#160; The primary improvement is simply adoption.&amp;#160; PowerShell is installed by default in Windows7 and certain flavors of W2K8.&amp;#160; Windows7 is very popular amongst my coworkers and hence I can depend on at least a PowerShell deployment when I write my scripts.&lt;/p&gt;  &lt;p&gt;The next improvement surrounds the execution of scripts.&amp;#160; The default is still to have script execution disabled but you no longer have to be an administrator to do so.&amp;#160; Additionally you can specify on the PowerShell command line what the execution policy can be.&amp;#160; This mean you can have a script execute without the user having to manually enable script execution.&amp;#160; &lt;/p&gt;  &lt;p&gt;The command line is still a bit awkward.&amp;#160; But it’s easily solved by deploying both a .cmd and .ps1 file.&amp;#160; The .cmd file simply calls the .ps1 file with the awkward command line.&amp;#160; Users are comfortable clicking on a .cmd file and it removes all knowledge of powershell from the equation.&amp;#160; &lt;/p&gt;  &lt;p&gt;For example: &lt;/p&gt;  &lt;p&gt;Fix-Setup.cmd&lt;/p&gt;  &lt;pre&gt;set SOURCE=%~dp0
set TARGET=%SOURCE%\Fix-Setup.ps1
powershell -ExecutionPolicy RemoteSigned %TARGET%&lt;/pre&gt;

&lt;p&gt;These small changes greatly increased the adoption rate of scripts within my group.&amp;#160;&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9956063" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/PowerShell/default.aspx">PowerShell</category></item><item><title>The many cases of ByRef</title><link>http://blogs.msdn.com/jaredpar/archive/2010/01/21/the-many-cases-of-byref.aspx</link><pubDate>Thu, 21 Jan 2010 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9892877</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9892877.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9892877</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9892877</wfw:comment><description>&lt;p&gt;One of the overlooked or simply misunderstood features of the VB language is calling a function which has a ByRef parameter.&amp;#160; Most languages support only a single method of passing parameters by reference [1], that is the scenarios directly supported by the CLR.&amp;#160; The CLR has a lot of restrictions on the type of values it supports for ByRef parameters and these restrictions get in the way of VB’s goal to be a flexible language that strives to get out of the way of the user.&amp;#160; Hence the compiler goes to great lengths to be flexible and support multiple avenues of ByRef passing, much beyond what the CLR natively allows.&lt;/p&gt;  &lt;p&gt;This article will explore these different mechanisms.&amp;#160; In order to reduce the code samples, I will be using the following 2 methods to explain the different mechanisms of ByRef Passing&lt;/p&gt;  &lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;Sub &lt;/span&gt;FunctionWithInt(&lt;span style="color: blue"&gt;ByRef &lt;/span&gt;p1 &lt;span style="color: blue"&gt;As Integer&lt;/span&gt;)
        p1 = &lt;span style="color: brown"&gt;42
    &lt;/span&gt;&lt;span style="color: blue"&gt;End Sub
    Sub &lt;/span&gt;FunctionWithObject(&lt;span style="color: blue"&gt;ByRef &lt;/span&gt;p1 &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;p2 &lt;span style="color: blue"&gt;As Object&lt;/span&gt;)
        p1 = p2
    &lt;span style="color: blue"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;CLR ByRef&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first is to simply use the CLR concept of passing by reference as defined by section 12.4.1.5.2 and 12.1.6.1 of the CLI specification.&amp;#160; Any variable which meets any of the following criteria, does not require a type conversion, and is passed to a ByRef parameter will be passed directly in the CLR.&amp;#160; &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Argument of the current method &lt;/li&gt;

  &lt;li&gt;Local variable &lt;/li&gt;

  &lt;li&gt;Member Field of an object &lt;/li&gt;

  &lt;li&gt;Static Field &lt;/li&gt;

  &lt;li&gt;Array Element &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No special code is needed or generated for this scenario.&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Copy Back ByRef&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the CLR method of passing ByRef is very flexible, it disallows a number of useful scenarios.&amp;#160; The most prominent of which is properties.&amp;#160; Properties do not meet the CLR requirements for ByRef because under the hood they are simply a pair of function calls.&amp;#160; The result of a function call cannot be directly passed by reference.&amp;#160; &lt;/p&gt;

&lt;p&gt;Without any language intervention this can be very confusing to users.&amp;#160; Properties are very often simple get/set wrappers around fields and have almost the exact same usage scenarios.&amp;#160; To the point that most users don’t see a functional difference between the two.&amp;#160; Auto-implemented properties blur this line even further.&amp;#160; Not being able to pass them ByRef creates an unacceptable inconsistency in their usage.&lt;/p&gt;

&lt;p&gt;VB removes this inconsistency and allows properties to be passed by reference.&amp;#160; This is implemented under the hood by means of a temporary variable.&amp;#160; Temporaries are just local variables and hence can be passed by reference.&amp;#160; The property value is assigned to a temporary which is then passed by reference and then after wards is copied back into the original property.&lt;/p&gt;

&lt;p&gt;For example, take the following code sample &lt;/p&gt;

&lt;pre style="width: 816px; height: 163px" class="code"&gt;    &lt;span style="color: blue"&gt;Class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C1
        &lt;/span&gt;&lt;span style="color: blue"&gt;Public Property &lt;/span&gt;P1 &lt;span style="color: blue"&gt;As Integer
        Public &lt;/span&gt;P2 &lt;span style="color: blue"&gt;As Integer
    End Class
    Sub &lt;/span&gt;CopyBackByRef()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;v1 = &lt;span style="color: blue"&gt;New &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C1
        &lt;/span&gt;FunctionWithInt(v1.P1)
    &lt;span style="color: blue"&gt;End Sub

&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This will result in essentially the following code being generated&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;Sub &lt;/span&gt;CopyBackByRef_Explained()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;v1 = &lt;span style="color: blue"&gt;New &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C1
        &lt;/span&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;vbTemp = v1.P1
        FunctionWithInt(vbTemp)
        v1.P1 = vbTemp
    &lt;span style="color: blue"&gt;End Sub

&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This type of ByRef passing is used in the following 2 scenarios&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The value is a Property containing both a getter and setter.&amp;#160; &lt;/li&gt;

  &lt;li&gt;Passing the value to the function requires a conversion. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first can be done with even the strictest Option settings.&amp;#160; However #2 can only be used with Option Strict Off because it requires an implicit narrowing conversion.&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don’t Copy Back ByRef&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far we’ve only looked at scenarios where the user wants to actually see the value returned from the ByRef parameter.&amp;#160; There are many scenarios where the language can infer the user does not care about the return value of the function.&amp;#160; For example, what if I just want to pass a constant value?&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;Sub &lt;/span&gt;DontCopyBackByRef()
        FunctionWithInt(&lt;span style="color: brown"&gt;42&lt;/span&gt;)
    &lt;span style="color: blue"&gt;End Sub

&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code is legal in VB and represents another method of passing by ref.&amp;#160; This is very similar to the copy back method of passing by reference.&amp;#160; The only difference is that it never copies the value back.&amp;#160; It essentially generates the following code&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;Sub &lt;/span&gt;DontCopyBackByRef_Explained()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;vbTemp = &lt;span style="color: brown"&gt;42
        &lt;/span&gt;FunctionWithInt(vbTemp)
    &lt;span style="color: blue"&gt;End Sub
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This type of ByRef is used in any scenario where the value being passed cannot be assigned to.&amp;#160; For example&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The result of function calls &lt;/li&gt;

  &lt;li&gt;Read Only Properties &lt;/li&gt;

  &lt;li&gt;Constant Values &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Maybe Copy Back ByRef&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Up until now we’ve examined cases where the compiler can examine both the value being passed and the parameter it is being passed to and make a determination about what direction the data needs to move in.&amp;#160; What about late binding?&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;Sub &lt;/span&gt;MaybeCopyBackByRef()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;v1 &lt;span style="color: blue"&gt;As Object &lt;/span&gt;= &lt;span style="color: blue"&gt;Me
        Dim &lt;/span&gt;v2 = &lt;span style="color: brown"&gt;13
        &lt;/span&gt;v1.FunctionWithInt(v2)
    &lt;span style="color: blue"&gt;End Sub
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here v1 is typed to object and hence FunctionWithInt is being accessed via late binding.&amp;#160; In this case the compiler doesn’t know the actual method being invoked runtime.&amp;#160; Hence it cannot know up front if the parameters are ByRef or ByVal and cannot make an up front decision on the variable passing mechanism.&amp;#160; &lt;/p&gt;

&lt;p&gt;In order to make late binding invokes flow as smoothly as normal method invokes, the compiler will generate code to conditionally update the original value based on the runtime information about the parameter.&amp;#160; The late binder communicates this information via an array of Boolean values, one for each parameter passed to the function.&amp;#160; The compiler will initialize this array with true for any values it knows are updatable and false for values that are not.&amp;#160; The late binder will then examine every parameter to the function and set the corresponding index in the array to false if the method parameter is ByVal.&amp;#160; If it is ByRef the returned value from the function will be copied back into the original parameter array.&lt;/p&gt;

&lt;p&gt;The resulting code looks a bit like this.&amp;#160; You can ignore all of the Nothing values as they are not important for this discussion.&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;Sub &lt;/span&gt;MaybeCopyBackByRef_Explained()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;v1 &lt;span style="color: blue"&gt;As Object &lt;/span&gt;= &lt;span style="color: blue"&gt;Me
        Dim &lt;/span&gt;v2 = &lt;span style="color: brown"&gt;13
        &lt;/span&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;parameters = &lt;span style="color: blue"&gt;New Object&lt;/span&gt;() {v2}
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;isByRef = &lt;span style="color: blue"&gt;New Boolean&lt;/span&gt;() {&lt;span style="color: blue"&gt;True&lt;/span&gt;}
        &lt;span style="color: #2b91af"&gt;NewLateBinding&lt;/span&gt;.LateCall(v1, &lt;span style="color: blue"&gt;Nothing&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;FunctionWithInt&amp;quot;&lt;/span&gt;, parameters, &lt;span style="color: blue"&gt;Nothing&lt;/span&gt;, &lt;span style="color: blue"&gt;Nothing&lt;/span&gt;, isByRef, &lt;span style="color: blue"&gt;True&lt;/span&gt;)
        &lt;span style="color: blue"&gt;If &lt;/span&gt;(isByRef(&lt;span style="color: brown"&gt;0&lt;/span&gt;)) &lt;span style="color: blue"&gt;Then
            &lt;/span&gt;v2 = &lt;span style="color: blue"&gt;CInt&lt;/span&gt;(parameters(&lt;span style="color: brown"&gt;0&lt;/span&gt;))
        &lt;span style="color: blue"&gt;End If
    End Sub
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;[1] Starting with version 4.0, C# now supports two versions of reference passing.&amp;#160; In addition to the one available since 1.0 the ref modifier is now optional when making an interop call to a COM object: &lt;a href="http://mutelight.org/articles/new-features-in-c-sharp-4.html"&gt;http://mutelight.org/articles/new-features-in-c-sharp-4.html&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9892877" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category></item><item><title>VsVim Update Released (Version 0.6.0)</title><link>http://blogs.msdn.com/jaredpar/archive/2010/01/04/vsvim-update-released-version-0-6-0.aspx</link><pubDate>Mon, 04 Jan 2010 06:44:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9943259</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9943259.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9943259</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9943259</wfw:comment><description>&lt;p&gt;I just released an update to VsVim for Visual Studio 2010 Beta2.&amp;#160; This should be available shortly from the extension manager in Visual Studio or it can be downloaded directly at the following link&lt;/p&gt;  &lt;p&gt;Link: &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329"&gt;http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;New Features&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Normal Mode Commands: Y,Delete, Arrow Keys, gP, gp, z&amp;lt;CR&amp;gt;,zt, z., zz, z-, zb&lt;/li&gt;    &lt;li&gt;Command Mode Commands: d[elete], &amp;lt;,&amp;gt;, j[oin], y[ank], p[ut], &lt;/li&gt;    &lt;li&gt;Insert Mode: Fixed Intellisense window dismissal issue (see below)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Notable Bug Fixes&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;In previous versions intellisense didn’t play well with insert mode.&amp;#160; In particular attempting to dismiss intellisense with the Escape key would exit insert mode but leave intellisense showing.&amp;#160; This release fixes the behavior to instead dismiss intellisense and leave you in Insert Mode&lt;/li&gt;    &lt;li&gt;Normal Mode O now enters insert mode upon completion&lt;/li&gt;    &lt;li&gt;Count was not being applied to certain edit commands (x) in Normal Mode&lt;/li&gt;    &lt;li&gt;Normal Mode “dw” was deleting the line break if the word was at the end of the line &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Source Code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Source for this release is available at the following location.&amp;#160; It is released under the &lt;a href="http://msdn.microsoft.com/en-us/library/cc707818.aspx"&gt;MS-PL&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a title="http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.6.0.zip" href="http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.6.0.zip"&gt;http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.6.0.zip&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Future Plans&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The next planned major update is version 0.7.0.&amp;#160; The two main features of this release will be the addition of Visual Mode and the :substitute command.&amp;#160; Additionally I plan to flesh out a lot of the areas in normal, command and visual mode.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The usual caveats and expectations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This extension is being released by me, not by Microsoft.&amp;#160; As such the support level for this extension is equivalent to the amount of free time I have to put into it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9943259" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VsVim/default.aspx">VsVim</category></item><item><title>Making F# type inference friendly for C#</title><link>http://blogs.msdn.com/jaredpar/archive/2009/12/15/making-f-type-inference-friendly-for-c.aspx</link><pubDate>Tue, 15 Dec 2009 19:53:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9937228</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9937228.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9937228</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9937228</wfw:comment><description>&lt;p&gt;One of my current hobby projects, &lt;a href="http://blogs.msdn.com/jaredpar/archive/2009/12/14/vsvim-update-released-version-0-5-4.aspx"&gt;VsVim&lt;/a&gt;, requires me to make a lot of calls between F# and C# projects.&amp;#160; The core Vim engine is a pure F# solution based on Visual Studio’s new editor.&amp;#160; It additionally has a small hosting layer and a large test bed both written in C#.&amp;#160; &lt;/p&gt;  &lt;p&gt;When working with the exposed core Vim engine API, I’ve found a number of generated F# constructs which are not easily accessible from C#.&amp;#160; The problem stems from the manner in which native F# types are exposed.&amp;#160; Many of them are generic and&amp;#160; lack type inference friendly helper methods that force awkward usage patterns in C#.&amp;#160; Most painful is the FSharpOption&amp;lt;T&amp;gt; type because it’s a type I frequently expose in APIs.&amp;#160; &lt;/p&gt;  &lt;p&gt;FSharpOption&amp;lt;T&amp;gt; is the exposed type for the native F# &lt;a href="http://msdn.microsoft.com/en-us/library/dd233245(VS.100).aspx"&gt;option&lt;/a&gt; construct representing a value which may or may not be present. It’s similar to C#’s nullable type except that it applies to all types of values.&amp;#160; The primary operations you want to do with an FSharpOption&amp;lt;T&amp;gt; are &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create an option with a value &lt;/li&gt;    &lt;li&gt;Create an option without a value &lt;/li&gt;    &lt;li&gt;Determine if it has a value &lt;/li&gt;    &lt;li&gt;Determine if it does not have a value &lt;/li&gt;    &lt;li&gt;Access the value &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;In F# using an option is an inherent part of the language and the hence the resulting code is very elegant. &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;OptionExample = 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;optionWithValue = Some(&lt;span style="color: brown"&gt;42&lt;/span&gt;)
    &lt;span style="color: blue"&gt;let &lt;/span&gt;optionWithoutValue = None
    &lt;span style="color: blue"&gt;let &lt;/span&gt;isSome = Option.isSome optionWithValue
    &lt;span style="color: blue"&gt;let &lt;/span&gt;isNone = Option.isNone optionWithoutValue
    Option.get optionWithValue&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Unfortunately the equivalent C# code is not nearly so nice.&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static int &lt;/span&gt;OptionExample() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;optionWithValue = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: brown"&gt;42&lt;/span&gt;);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;optionWithoutValue = &lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.None;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isSome = &lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.get_IsSome(optionWithValue);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isNone = &lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.get_IsNone(optionWithValue);
    return optionWithValue.Value;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Too many explicit types!!!&amp;#160; Using any explicit type with F# related code just feels wrong.&amp;#160; &lt;/p&gt;

&lt;p&gt;In C#, and most other .Net languages, 4 out of the 5 operations you want to do on FSharpOption require an explicit type parameter.&amp;#160; This resulting code is a bit tedious with a simple type like int but once you get to more complex generics it can get extremely verbose.&amp;#160; In the case of anonymous types, it’s simply not possible to use the FSharpOption&amp;lt;T&amp;gt; without a few wrappers.&amp;#160; &lt;/p&gt;

&lt;p&gt;Luckily most of these can be solved by using the familiar pattern of using a non-generic class with static generic methods.&amp;#160; These allow C# users to take advantage of the languages type inference capabilities to reduce the verbosity of the code.&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption &lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;T&amp;gt; Create&amp;lt;T&amp;gt;(T value) {
        &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;T&amp;gt;(value);
    }
    &lt;span style="color: blue"&gt;public static bool &lt;/span&gt;IsSome&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;T&amp;gt; opt) {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;T&amp;gt;.get_IsSome(opt);
    }
    &lt;span style="color: blue"&gt;public static bool &lt;/span&gt;IsNone&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;T&amp;gt; opt) {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;T&amp;gt;.get_IsNone(opt);
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now we can rewrite the original sample a bit cleaner &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static int &lt;/span&gt;OptionExample() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;optionWithValue = &lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;.Create(&lt;span style="color: brown"&gt;42&lt;/span&gt;);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;optionWithoutValue = &lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.None;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isSome = optionWithValue.IsSome();
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isNone = optionWithoutValue.IsNone();
    &lt;span style="color: blue"&gt;return &lt;/span&gt;optionWithValue.Value;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice we still haven’t fixed the None case.&amp;#160; Fixing this is a beyond the scope of what I want to write here but it is possible in certain scenarios.&amp;#160; You can take a look at how in one of my previous blog articles: &lt;a href="http://blogs.msdn.com/jaredpar/archive/2008/10/06/functional-c-providing-an-option.aspx"&gt;Function C# Providing an Option&lt;/a&gt;.&amp;#160; &lt;/p&gt;

&lt;p&gt;This pattern is not just limited to the FSharpOption class but can be applied to many of the generic constructs F# exports to wrap their native types.&amp;#160; In particular FSharpFunc&amp;lt;T,Result&amp;gt; and the various FSharpChoice&amp;lt;&amp;gt; types can be made a bit friendlier with a few wrappers.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9937228" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Type+Inference/default.aspx">Type Inference</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/F_2300_/default.aspx">F#</category></item><item><title>VsVim Update Released (Version 0.5.4)</title><link>http://blogs.msdn.com/jaredpar/archive/2009/12/14/vsvim-update-released-version-0-5-4.aspx</link><pubDate>Mon, 14 Dec 2009 18:26:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9936662</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9936662.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9936662</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9936662</wfw:comment><description>&lt;p&gt;I just released an update to VsVim for Visual Studio 2010 Beta2.&amp;#160; This should be available shortly from the extension manager in Visual Studio or it can be downloaded directly at the following link&lt;/p&gt;  &lt;p&gt;Link: &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329"&gt;http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Changes&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This is mainly a minor bug fix release.&amp;#160; Primarily there was a bug in the command mode jump implementation that made it flaky in big buffers.&amp;#160; Additionally I implemented the normal mode commands r and O.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Source Code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Source for this release is available at the following location.&amp;#160; It is released under the &lt;a href="http://msdn.microsoft.com/en-us/library/cc707818.aspx"&gt;MS-PL&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.5.4.zip"&gt;http://cid-dc25b20f65f628f8.skydrive.live.com/self.aspx/Public/VsVim/VsVim-0.5.4.zip&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The usual caveats and expectations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This extension is being released by me, not by Microsoft.&amp;#160; As such the support level for this extension is equivalent to the amount of free time I have to put into it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9936662" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VsVim/default.aspx">VsVim</category></item><item><title>The File System is unpredictable</title><link>http://blogs.msdn.com/jaredpar/archive/2009/12/10/the-file-system-is-unpredictable.aspx</link><pubDate>Thu, 10 Dec 2009 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9934789</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9934789.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9934789</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9934789</wfw:comment><description>&lt;p&gt;One of the more frequent questions I answer on StackOverflow is a variation of the following.&amp;#160; &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;I’m doing XXX with a file, how can I know if the file exists?&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The variations include verify no one else has the file open, if the file is in use, the file is not writable, etc ….&amp;#160; The answer to all of these questions is unfortunately the same.&amp;#160; Simply put you can’t.&amp;#160; The reason why is the fundamental nature of the file system prevents such predictive operations.&amp;#160; &lt;/p&gt;  &lt;p&gt;The file system is a resource with multiple levels of control that is shared between all users and processes in the system.&amp;#160; The levels of control include but are not limited to file system and sharing permissions.&amp;#160; At &lt;strong&gt;any&lt;/strong&gt; point in time any entity on the computer may change a file system object or it’s controls in any number of ways.&amp;#160; For example &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The file could be deleted &lt;/li&gt;    &lt;li&gt;A file could be created at place one previously did not exist &lt;/li&gt;    &lt;li&gt;Permissions could change on the file in such a way that the current process does not have access &lt;/li&gt;    &lt;li&gt;Another process could open the file in such a way that is not conducive to sharing &lt;/li&gt;    &lt;li&gt;The user remove the USB key containing the file &lt;/li&gt;    &lt;li&gt;The network connection to the mapped drive could get disconnected &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Or in short &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The file system is best viewed as a multi-threaded object over which you have no reliable synchronization capabilities&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Many developers, and APIs for that matter, though treat the file system as though it’s a static resource and assume what’s true at one point in time will be true later.&amp;#160; Essentially using the result of one operation to predict the success or failure of another.&amp;#160; This ignores the possibility of the above actions interweaving in between calls.&amp;#160; It leads to code which reads well but executes badly in scenarios where more than one entity is changing the file system. &lt;/p&gt;  &lt;p&gt;These problems are best demonstrated by a quick sample.&amp;#160; Lets keep it simple and take a stab at a question I’ve seen a few times.&amp;#160; The challenge is to write a function which returns all of the text from a file if it exists and an empty string if it does not.&amp;#160; To simplify this problem lets assume permissions are not an issue, paths are properly formatted, paths point to local drives and people aren’t randomly ripping out USB keys.&amp;#160; Using the System.IO.File APIs we may construct the following solution.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static string &lt;/span&gt;ReadTextOrEmpty(&lt;span style="color: blue"&gt;string &lt;/span&gt;path) {
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.Exists(path)) {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.ReadAllText(path); &lt;span style="color: green"&gt;// Bug!!!
    &lt;/span&gt;} &lt;span style="color: blue"&gt;else &lt;/span&gt;{
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Empty;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code reads great and at a glance looks correct but is actually fundamentally flawed.&amp;#160; The reason why is the code changes depends on the call to File.Exist to be true for a large portion of the function.&amp;#160; It’s being used to predict the success of the call to ReadAllText.&amp;#160; However there is nothing stopping the file from being deleted in between these two calls.&amp;#160; In that case the call to File.ReadAllText would throw a FileNotFoundException which is exactly what the API is trying to prevent! &lt;/p&gt;

&lt;p&gt;This code is flawed because it’s attempting to use one piece of data to make a prediction about the future state of the file system.&amp;#160; This is simply not possible with the way the file system is designed.&amp;#160; It’s a shared resource with no reliable synchronization mechanism.&amp;#160; File.Exists is much better named as File.ExistedInTheRecentPast (the name gets much worse if you consider the impact of permissions).&amp;#160; &lt;/p&gt;

&lt;p&gt;Knowing this, how could we write ReadTextOrEmpty in a reliable fashion?&amp;#160; Even though you can not make predictions on the file system the failures of operations is a finite set.&amp;#160; So instead of attempting to predict successful conditions for the method, why not just execute the operation and deal with the consequences of failure?&amp;#160;&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static string &lt;/span&gt;ReadTextOrEmpty(&lt;span style="color: blue"&gt;string &lt;/span&gt;path) {
    &lt;span style="color: blue"&gt;try &lt;/span&gt;{
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.ReadAllText(path);
    } &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DirectoryNotFoundException&lt;/span&gt;) {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Empty;
    } &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;FileNotFoundException&lt;/span&gt;) {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Empty;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This implementation provides the original requested behavior.&amp;#160; In the case the file exists, for the duration of the operation, it returns the text of the file and if not returns an empty string.&amp;#160; &lt;/p&gt;

&lt;p&gt;In general I find the above pattern is the best way to approach the file system.&amp;#160; Do the operations you want and deal with the consequences of failure in the form of exceptions.&amp;#160; To do anything else involves an unreliable prediction in which you still must handle the resulting exceptions.&amp;#160; &lt;/p&gt;

&lt;p&gt;If this is the case then why have File.Exist at all if the results can’t be trusted?&amp;#160; It depends on the level of reliability you want to achieve.&amp;#160; In production programs I flag any File.Exist I find as a bug because reliability is a critical component.&amp;#160; However you’ll see my personal powershell configuration scripts littered with calls to File.Exsit.&amp;#160; Simply put because I’m a bit lazy in those scripts because critical reliability is not important when I’m updating my personal .vimrc file.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9934789" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/API+Design/default.aspx">API Design</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>VsVim Update Released (Version 0.5.3)</title><link>http://blogs.msdn.com/jaredpar/archive/2009/12/07/vsvim-update-released-version-0-5-3.aspx</link><pubDate>Mon, 07 Dec 2009 07:44:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9933303</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9933303.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9933303</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9933303</wfw:comment><description>&lt;p&gt;I just released an update to VsVim for Visual Studio 2010 Beta2.&amp;#160; This should be available shortly from the extension manager in Visual Studio or it can be downloaded directly at the following link&lt;/p&gt;  &lt;p&gt;Link: &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329"&gt;http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Changes&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;p&gt;Normal Mode Block Cursor&lt;/p&gt;      &lt;p&gt;This removes the red cursor in favor of a Vim block style cursor for normal mode. This is implemented as a simple adornment in the editor and in the end required much less work than I anticipated (also allowed the removal of a lot of hacky code).&amp;#160; Thanks a lot to &lt;a href="http://blogs.msdn.com/noahric/"&gt;Noah&lt;/a&gt; for helping me out here. &lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;Basic range support in command mode.&lt;/p&gt;      &lt;p&gt;Currently limited to line number ranges, % and . (current line) &lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;Command Mode Infrastructure Changes&lt;/p&gt;      &lt;p&gt;This infrastructure change allowed my to correctly implement the few command mode commands I previously supported and laid the ground work for quick support of new commands&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;New Normal Mode Commands: gJ&lt;/p&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This release is small on features and ordinarily wouldn’t qualify for an update.&amp;#160; But the cursor change was so pleasant for myself that I decided to release a quick update just for that.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What’s Next?&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Add command mode support for all normal mode commands that have a command mode equivalent&lt;/li&gt;    &lt;li&gt;Resolve normal mode edits around a selection.&amp;#160; Currently selection is ignored and it produces unexpected behavior.&amp;#160; &lt;/li&gt;    &lt;li&gt;More normal mode commands: In particular, Delete, gt, gT, global mark navigation &lt;/li&gt;    &lt;li&gt;Lifetime issues around buffers and Vim’s internal object model&lt;/li&gt;    &lt;li&gt;Navigation in a buffer which contains multiple font sizes&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;The usual caveats and expectations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This extension is being released by me, not by Microsoft.&amp;#160; As such the support level for this extension is equivalent to the amount of free time I have to put into it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9933303" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VsVim/default.aspx">VsVim</category></item><item><title>Dev Connection Talk Slides and Code</title><link>http://blogs.msdn.com/jaredpar/archive/2009/12/02/dev-connection-talk-slides-and-code.aspx</link><pubDate>Wed, 02 Dec 2009 21:21:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931627</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9931627.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9931627</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9931627</wfw:comment><description>&lt;p&gt;Thanks to everyone who attended my sessions at Dev Connections.&amp;#160; I’ve posted the material for both of my talks on my SkyDrive account.&amp;#160; This includes the slides and projects.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://cid-dc25b20f65f628f8.skydrive.live.com/browse.aspx/Public/DevConnections2009"&gt;http://cid-dc25b20f65f628f8.skydrive.live.com/browse.aspx/Public/DevConnections2009&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Please let me know if you have any questions or problems with the materials or any additional feedback about the sessions.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931627" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category></item><item><title>VsVim Update Released (Version 0.5.2)</title><link>http://blogs.msdn.com/jaredpar/archive/2009/12/01/vsvim-update-released-version-0-5-2.aspx</link><pubDate>Tue, 01 Dec 2009 06:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9930598</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9930598.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9930598</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9930598</wfw:comment><description>&lt;p&gt;I just released an update to VsVim for Visual Studio 2010 Beta2.&amp;#160; This should be available shortly from the extension manager in Visual Studio or it can be downloaded directly at the following link&lt;/p&gt;  &lt;p&gt;Link: &lt;a title="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329" href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329"&gt;http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Changes&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;p&gt;Removal of conflicting key bindings&lt;/p&gt;      &lt;p&gt;On startup VsVim will now look for any key bindings which conflict with implemented Vim commands. It will then provide a message box allowing you to remove the key bindings for this session of Visual Studio. Right now this is an all or nothing removal, future versions may make this a more granular process&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;Normal Mode no longer intercepts all keystrokes (such as Cut and Paste)&lt;/p&gt;      &lt;p&gt;This bug in the previous release was a result of the command routing changes that occured between Beta1 and Beta2. I believe I know have them all worked out and VsVim should only be intercepting commands it intends to process&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;New Normal Mode Commands&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Mark setting: m[a-zA-Z]&lt;/li&gt;      &lt;li&gt;Mark Jump: `[a-z], ‘[a-z] &lt;/li&gt;      &lt;ul&gt;       &lt;li&gt;This is currently limited to local marks &lt;/li&gt;     &lt;/ul&gt;      &lt;li&gt;Page Up, Page Down: CTRL-U,CTRL-D&lt;/li&gt;      &lt;li&gt;Join: j&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;What’s Next?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The biggest area I want to focus on for the next release is Command mode and rounding, more normal mode behavior (especially mark support) and basic range support.&lt;/p&gt;  &lt;p&gt;At a minimum I want to expand all of the normal mode commands I have implemented to have their corresponding command mode version implemented.&amp;#160; Join, mark, etc …&amp;#160; This shouldn’t be a big work item.&amp;#160; Command mode is just something I’ve been neglecting up to this point in favor of infrastructure.&amp;#160; &lt;/p&gt;  &lt;p&gt;The other big item I want to work on is ranges.&amp;#160; Many command mode commands operate on ranges and it’s something I’ve yet to implement at a core level.&amp;#160; The basic mark support I added in this release starts the process but I have a ways to go.&amp;#160; &lt;/p&gt;  &lt;p&gt;Also, any suggestions users have are very welcome.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The usual caveats and expectations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This extension is being released by me, not by Microsoft.&amp;#160; As such the support level for this extension is equivalent to the amount of free time I have to put into it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9930598" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VsVim/default.aspx">VsVim</category></item><item><title>Vim Emulator Editor For Beta2 Released</title><link>http://blogs.msdn.com/jaredpar/archive/2009/11/16/vim-emulator-editor-for-beta2-released.aspx</link><pubDate>Tue, 17 Nov 2009 04:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9923415</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9923415.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9923415</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9923415</wfw:comment><description>&lt;p&gt;This is essentially the same release as the &lt;a href="http://blogs.msdn.com/jaredpar/archive/2009/09/08/vim-emulator-editor-extension-released.aspx"&gt;original&lt;/a&gt; but updated for some changes that occurred in the APIs between Beta1 and Beta2.&amp;#160; &lt;/p&gt;  &lt;p&gt;Link: &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329"&gt;http://visualstudiogallery.msdn.microsoft.com/en-us/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The biggest change came in the way in which Visual Studio routes commands.&amp;#160; Vim, as you can imagine, needs to participate in command routing and these changes took awhile to sort out.&amp;#160; I believe I’ve sorted out the issues but please send me a mail / comment if you find any bugs.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Caveats and Expectations&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This extension is being released by me, not by Microsoft.&amp;#160; As such the support level for this extension is equivalent to the amount of free time I have to put into it. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;What’s next?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Right now the Visual Studio command system is the biggest inhibitor to implementing new features.&amp;#160; Each command in Visual Studio is bound to a specific key stroke and often times this conflicts with key strokes my Vim emulator needs to process.&amp;#160; &lt;/p&gt;  &lt;p&gt;It’s easy to implement these commands in the core Vim engine.&amp;#160; However the actual key strokes get intercepted by Visual Studio and are not processed.&lt;/p&gt;  &lt;p&gt;My next feature will be essentially removing these commands so that they can make it to the Vim layer.&amp;#160; It will require a bit of a UI since I’ll be essentially changing key bindings but hopefully I can get something basic out the door so that I can start focusing on real features again.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9923415" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VsVim/default.aspx">VsVim</category></item><item><title>Type safety issue when assigning CComPtr&lt;T&gt; instances</title><link>http://blogs.msdn.com/jaredpar/archive/2009/11/04/type-safety-issue-when-assigning-ccomptr-t-instances.aspx</link><pubDate>Wed, 04 Nov 2009 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9916908</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9916908.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9916908</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9916908</wfw:comment><description>&lt;p&gt;Recently while making a bug fix to our selection tracking code I discovered an unexpected behavior with CComPtr&amp;lt;T&amp;gt; instances.&amp;#160; The crux of the fix included creating a new tracking mechanism exposed via COM in the type ISelectionTracking.&amp;#160; The old interface, lets call it IOldTracking, was a completely unrelated interface in terms of inheritance hierarchies.&amp;#160; &lt;/p&gt;  &lt;p&gt;As part of the fix I changed the type of a field (m_spTracking) from CComPtr&amp;lt;IOldTracking&amp;gt; to CComPtr&amp;lt;ISelectionTracking&amp;gt;.&amp;#160; I searched for assignments of m_spTracking and converted them to call the new API I added as part of the fix.&amp;#160; I didn’t search terribly hard because I was depending on the compiler to catch any places I missed.&amp;#160; ISelectionTracking and IOldTracking are incompatible types so any places I missed will show up as compilation errors.&amp;#160; &lt;/p&gt;  &lt;p&gt;Or so I thought … &lt;/p&gt;  &lt;p&gt;I made my fix, ran our core check-in suites without error, checked in and moved onto the next bug.&amp;#160; A couple hours later one of our other devs emailed me and informed me my check-in was breaking our larger, slower, suite bed run because m_spTracking was NULL. After some quick debugging I found myself looking at the following chunk of code which was apparently NULL’ing out m_spTracking in the suite.&lt;/p&gt;  &lt;pre class="code"&gt;  CComPtr&amp;lt;IOldTracking&amp;gt; spOldTracking;
&lt;span style="color: blue"&gt;  if &lt;/span&gt;( SUCCEEDED(CreateOldSelectionTracking(&amp;amp;spOldTracking)) ) {
    m_spTracking = spOldTracking;
  }
 &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Me and the other dev were quite shocked that this compiled at all.&amp;#160; How is it possible to assign between CComPtr&amp;lt;ISelectionTracking&amp;gt; and CComPtr&amp;lt;IOldTracking&amp;gt;???&amp;#160; My first thought was I must have accidentally used a CComQIPtr somewhere (quickly verified that was not the case).&amp;#160; After a bit of searching we found the cause was one of the operater=&amp;#160; instances available on CComPtr&amp;lt;T&amp;gt;.&amp;#160; Here is the definition&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;template &lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;typename &lt;/span&gt;Q&amp;gt;
T* &lt;span style="color: blue"&gt;operator&lt;/span&gt;=(_In_ &lt;span style="color: blue"&gt;const &lt;/span&gt;CComPtr&amp;lt;Q&amp;gt;&amp;amp; lp) &lt;span style="color: blue"&gt;throw&lt;/span&gt;()
{
    &lt;span style="color: blue"&gt;if&lt;/span&gt;( !IsEqualObject(lp) )
    {
        &lt;span style="color: blue"&gt;return static_cast&lt;/span&gt;&amp;lt;T*&amp;gt;(AtlComQIPtrAssign((IUnknown**)&amp;amp;p, lp, &lt;span style="color: blue"&gt;__uuidof&lt;/span&gt;(T)));
    }
    &lt;span style="color: blue"&gt;return &lt;/span&gt;*&lt;span style="color: blue"&gt;this&lt;/span&gt;;
}&lt;/pre&gt;

&lt;p&gt;This templated operator allows for assignments between CComPtr instance no matter what the type is for the left and right side.&amp;#160; The effect is that instead of doing compile type C++ type conversion rules, it will instead rely on runtime COM polymorphic assignment rules via IUnknown::QueryInterface.&amp;#160; This moves assignment errors from compile time to runtime for unrelated interfaces.&amp;#160; &lt;/p&gt;

&lt;p&gt;This is further complicated because it only applies to assignment between CComPtr’s (and derived instances).&amp;#160; If the right hand side of the assignment is a non-smart pointer, compile time C++ conversions will apply.&amp;#160; To demonstrate …&lt;/p&gt;

&lt;pre class="code"&gt;        CComPtr&amp;lt;ISelectionTracking&amp;gt; spTracking;
        CComPtr&amp;lt;IOldTracking&amp;gt; spOld;
        ...
        spTracking = spOld;  &lt;span style="color: green"&gt;// Fails at runtime
        &lt;/span&gt;spTracking = (IOldTracking*)spOld;  &lt;span style="color: green"&gt;// Compilation Error
&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;What surprised me though was talking to other developers about this issue.&amp;#160; Most agreed with me that this is a bug in CComPtr&amp;lt;T&amp;gt;, or at least very unexpected behavior.&amp;#160; A surprising number though did not expect this behavior but still considered it acceptable.&amp;#160; The difference comes down whether you view CComPtr&amp;lt;T&amp;gt; as a simple smart pointer responsible for AddRef/Release semantics or as that plus an enabler of QueryInterface style conversions.&amp;#160; I personally view CComPtr&amp;lt;T&amp;gt; as a simple smart pointer with know real understanding of QueryInterface style conversions and CComQIPtr&amp;lt;T&amp;gt; as a smart pointer which respects QueryInterface style conversions.&amp;#160;&amp;#160; As such this behavior is completely unexpected.&amp;#160; &lt;/p&gt;

&lt;p&gt;The fix in this case was pretty straight forward (use the new API) but I was still worried about how to prevent this type of problem in the future.&amp;#160; In particular how to get the failure back to a compile time error.&amp;#160; In the end we settled on using a stripped down version of CComPtr we already had in our code base going forward called CComPtrEx.&amp;#160; I’ve previously blogged about about the need for this type &lt;a href="http://blogs.msdn.com/jaredpar/archive/2008/02/22/multiple-paths-to-iunknown.aspx"&gt;here&lt;/a&gt;.&amp;#160; It’s different from CComPtr in the following ways&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Does not have the templated version of operator= and instead relies on compile time C++ conversions checks for assignment &lt;/li&gt;

  &lt;li&gt;Allows for interfaces which have multiple paths to IUnknown (can cause a compile time error in CComPtr). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also for purposes of rigor, we temporarily commented out the CComPtr&amp;lt;T&amp;gt; operator, recompiled our code base and verified no new errors popped up.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9916908" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Gotcha/default.aspx">Gotcha</category></item><item><title>Speaking at Dev Connections in Las Vegas Next Week</title><link>http://blogs.msdn.com/jaredpar/archive/2009/11/03/speaking-at-dev-connections-in-las-vegas-next-week.aspx</link><pubDate>Tue, 03 Nov 2009 16:51:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9916831</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9916831.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9916831</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9916831</wfw:comment><description>&lt;p&gt;Next week I will be speaking at &lt;a href="http://www.devconnections.com/shows/FALL2009VS/default.asp?s=136"&gt;Dev Connections&lt;/a&gt; in Las Vegas.&amp;#160; I will be running the following sessions&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;VMS02: Future Directions for Visual Basic&lt;/li&gt;    &lt;li&gt;VMS04: Microsoft Visual Basic IDE Tips and Tricks&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Both of these talks will spend a bit of time talking about all of the progress and exciting new features we’ve added in Dev10.&amp;#160; Given I primarily work on the IDE these days, expect a bit of IDE content to work it’s way into the Future Directions talk as well.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9916831" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category></item><item><title>Using F# Discriminated Unions in C# (Beta2)</title><link>http://blogs.msdn.com/jaredpar/archive/2009/10/27/using-f-discriminated-unions-in-c-beta2.aspx</link><pubDate>Tue, 27 Oct 2009 12:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9913363</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9913363.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9913363</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9913363</wfw:comment><description>&lt;p&gt;While updating my VsVim editor extensions for Beta2 [1] I got hit by a change in the way F# exposed discriminated unions in metadata.&amp;#160; My extension consists of a core F# component with a corresponding set of unit tests written in C#.&amp;#160; It’s mostly API level testing and as such I use a lot of F# generated types in my C# test assembly.&lt;/p&gt;  &lt;p&gt;In Beta1 all information which could be extracted from a discriminated type union was immediately available on the value.&amp;#160; The underlying type presentation was less than desirable but these details were hidden by type inference and the very accessible API.&amp;#160;&amp;#160; The type wasn’t perfect because given a particular instance only the subset of the properties relevant to the union value type were valid.&amp;#160; All others threw exceptions.&amp;#160; But the code use of these methods and properties flowed very well.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;For instance take the following F# definition&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;ActionKind =
    | Mouse = &lt;span style="color: brown"&gt;1
    &lt;/span&gt;| Keyboard = &lt;span style="color: brown"&gt;2

&lt;/span&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;ActionResult =
    | Complete &lt;span style="color: blue"&gt;of &lt;/span&gt;(ActionKind * int)
    | Error &lt;span style="color: blue"&gt;of &lt;/span&gt;string
    | NeedMore &lt;span style="color: blue"&gt;of &lt;/span&gt;(char &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;ActionResult)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The use case in C# was quite simple&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;TestActionBeta1(){
    &lt;span style="color: blue"&gt;var &lt;/span&gt;res = GetResult();
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(res.IsComplete());
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: #2b91af"&gt;ActionKind&lt;/span&gt;.Mouse, res.Complete1.Item1);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: brown"&gt;42&lt;/span&gt;, res.Complete1.Item2);
}&lt;/pre&gt;

&lt;p&gt;Notice how no type information is necessary and the code flows quite naturally.&amp;#160; C# type inference works great here and allows me to do what I need to do without fussing around with little stuff.&amp;#160; The type in this case is a detail I don’t need to know about.&amp;#160; It simply adds no value.&amp;#160; &lt;/p&gt;

&lt;p&gt;Discriminated Unions in Beta2 changed substantially in this area.&amp;#160; Instead of generating the set of all values on the exposed type, there is now an inner type generated for every discriminated union value and the properties relevant to that union value are stored on the inner type.&amp;#160; The outer type now contains only properties to determine which type of value it is (certainly an upgrade from methods!) [2]&lt;/p&gt;

&lt;p&gt;For instance in the case of ActionResult there are 3 generated inner classes: Complete, Error and NeedMore.&amp;#160; Each one contains a single property Item which contains the associated value(s).&amp;#160; This means to get to the value portion a cast to the inner type must be inserted!&amp;#160; &lt;/p&gt;

&lt;p&gt;Lets take a a look at how the above test code has to change to deal with the Beta2 generation of ActionResult.&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;TestActionBeta2() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;res = GetResult();
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(res.IsComplete);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: #2b91af"&gt;ActionKind&lt;/span&gt;.Mouse, ((&lt;span style="color: #2b91af"&gt;ActionResult&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Complete&lt;/span&gt;)res).Item.Item1);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: brown"&gt;42&lt;/span&gt;, ((&lt;span style="color: #2b91af"&gt;ActionResult&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Complete&lt;/span&gt;)res).Item.Item2);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice the explicit casts which must be added to access the values.&amp;#160; This makes it impossible to rely soley on C# type inference.&amp;#160; I must now understand the underlying type structure of discriminated unions in order to use them.&amp;#160; This extra cast adds no real value to my code.&amp;#160; &lt;/p&gt;

&lt;p&gt;My C# test assembly has literally hundreds of test cases which use this pattern on F# types.&amp;#160; I didn’t know the return type of every method and found myself hitting “Goto Def” on a lot of “var” instances to discover the static type, going back to the original file and inserting the cast.&amp;#160; It was a tedious and slow process.&lt;/p&gt;

&lt;p&gt;Eventually I settled on a different solution.&amp;#160; For every type I exposed in F# I added a set of extension methods in the form of AsXXX where XXX represented the name of the generated inner types.&amp;#160;&amp;#160; For example&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ActionResult&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Complete &lt;/span&gt;AsComplete(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ActionResult &lt;/span&gt;res) {
    &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ActionResult&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Complete&lt;/span&gt;)res;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The advantage of this approach is 2 fold &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Removes the need to explicitly name types in code and hence gets back the advantages of type inference&lt;/li&gt;

  &lt;li&gt;I can now use . on any of the values and let Intellisense help me find the appropriate method to use&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This extension method allows me to get closer to the Beta1 style code &lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;TestActionBeta2() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;res = GetResult();
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(res.IsComplete);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: #2b91af"&gt;ActionKind&lt;/span&gt;.Mouse, res.AsComplete().Item.Item1);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: brown"&gt;42&lt;/span&gt;, res.AsComplete().Item.Item2);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;With these methods and a quick series of Find / Replace calls, I was back in business.&amp;#160; &lt;/p&gt;

&lt;p&gt;[1] It’s coming I promise!&amp;#160; &lt;/p&gt;

&lt;p&gt;[2] It also contains a handy set of factory methods for generating values but it’s not relevant to this discussion.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9913363" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Extension+Methods/default.aspx">Extension Methods</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Type+Inference/default.aspx">Type Inference</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/F_2300_/default.aspx">F#</category></item></channel></rss>