C# API Try Catch Finally Snippet

I have been doing some work with C# recently and  decided that despite what people say C# is as equally verbose as VB.NET. It is true if you look at the number of characters in a VB program compared to the equivalent C# program there will be a greater number but you seem to get a lot more automatic inserts with VB than you do with C#. C# seems to have a lot more punctuation that VB too adding up to a lot more typing than I was used to.

One common task I found myself repeating regularly was the closing down of objects and releasing memory. I made a point of releasing the top level object not only by doing a closedown and setting them to null (Nothing in VB) but also by releasing the memory in COM. I would call the following method:

Public Sub TidyCom(ByVal o As Object)

   If o IsNot Nothing Then

      System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
      System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o)
   End If

End Sub

That would ensure that any lingering memory is released (there is no doubt a better technical explanation for it but I will leave you to find out exactly what is going on)

Instead of regularly writing this code for each and every object I used, I instead created a try catch snippet that included the lines of code in the finally clause. The snippet below should be saved to C:\Users\David\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets and (unless your name is “David” and you are using Vista) change the  path to suit your Windows set up.

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        RE Top Level Object try, exception and finally
      </Title>
      <Author>David Zeidman</Author>
      <Description>The snippet inserts a try clause and closes the object properly</Description>
      <Shortcut>tryre</Shortcut>
    </Header>

    <Snippet>
      <Declarations>
        <Literal>
          <ID>TopLevelObject</ID>
          <ToolTip>Top level object</ToolTip>
          <Default>constit</Default>
        </Literal>
        <Literal>
          <ID>ReleaseComFunction</ID>
          <ToolTip>The function to call to release the com object</ToolTip>
          <Default>_Reinit.ReleaseCom</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[try
            {

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if ($TopLevelObject$!=null)
                {
                    $TopLevelObject$.CloseDown();
                    $ReleaseComFunction$($TopLevelObject$);
                    $TopLevelObject$ = null;
                }
            }]]>

      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Firstly you will need to define your top level object e.g. CRecord constit; This can then be used in the snippet above. You also have to define your “tidying” function too but you can change the code above so that it defaults to whatever you usually use. To use the snippet just type in “tryre” followed by a tab and it will be inserted.

Funnily enough I tried the same snippet in VB.NET (converting the code to VB of course) and Visual Studio crashed. Not sure really why. Anybody get this to work in VB?