Try/Catch and Try/Finally macros for VB.NET

I recently started playing around with macros in Visual Studio .NET 2003 and wrote up two I find very helpful: Wrap Try/Catch and Wrap Try/Finally. Basically, they take the selected text and wrap it in a try/catch block or a try/finally block.

This version supports empty selections (to create a new block) and both full-line and partial selections. It places the insertion point inside the try block for an empty selection and inside the catch block for a text selection.

Enjoy!

Imports EnvDTE
Imports System.Diagnostics

Public Module CodeModifiers

  Sub WrapTryCatch()
    WrapTrySomething("Catch ex As Exception")
  End Sub

  Sub WrapTryFinally()
    WrapTrySomething("Finally")
  End Sub

  Private Sub WrapTrySomething(ByVal middleText As String)

    If DTE.ActiveDocument Is Nothing Then
      Return
    End If

    Dim selection As TextSelection
    Dim startPoint As EditPoint
    Dim endPoint As EditPoint
    Dim insertPoint As EditPoint
    Dim empty As Boolean

    selection = DTE.ActiveDocument.Selection()
    startPoint = selection.TopPoint.CreateEditPoint()
    endPoint = selection.BottomPoint.CreateEditPoint()

    empty = startPoint.EqualTo(endPoint)

    DTE.UndoContext.Open("Wrap Try")
    Try

      Dim insertEndingCrLf As Boolean
      If endPoint.AtEndOfLine Then
        insertEndingCrLf = endPoint.AtStartOfLine
      Else
        insertEndingCrLf = True
      End If

      If Not endPoint.AtStartOfLine OrElse _
        endPoint.EqualTo(startPoint) Then
        endPoint.Insert(ControlChars.CrLf)
      End If

      endPoint.Insert(middleText + ControlChars.CrLf)

      If Not empty Then
        insertPoint = endPoint.CreateEditPoint()
      End If

      endPoint.Insert(ControlChars.CrLf + "End Try")

      If insertEndingCrLf Then
        endPoint.Insert(ControlChars.CrLf)
      End If

      ' insert try
      If Not AtStartOfLineIgnoringWhitepsace(startPoint) Then
        startPoint.Insert(ControlChars.CrLf)
      End If
      startPoint.Insert("Try" + _
                ControlChars.CrLf)

      If empty Then
        insertPoint = startPoint.CreateEditPoint()
      End If

      selection.MoveToPoint(insertPoint)
      selection.Indent()

    Finally
      DTE.UndoContext.Close()
    End Try
  End Sub

  Private Function AtStartOfLineIgnoringWhitepsace( _
                    ByVal point As EditPoint) As Boolean

    If point.AtStartOfLine Then
      Return True
    End If

    Dim p As EditPoint = point.CreateEditPoint()

    Do
      p.CharLeft()
      If Not Char.IsWhiteSpace(p.GetText(1), 0) Then
        Return False
      End If

      If p.AtStartOfLine Then
        Return True
      End If
    Loop

  End Function

End Module

This macro will only work for VB.NET. It can be translated to work with C# code or you can use the wonderful resharper plugin which unfortunately isn’t available for VB.NET.

NeoSwiff Public Beta – C# to SWF Solution

This has been blogged by others, but since I also got a chance to play with this product early on and am a big fan of C# I thought I’d make a mention. NeoSwiff is a C# to SWF solution. Although there’s been press about Xamlon, NeoSwiff takes a different approach. I can’t really comment on which approach is better, I haven’t played much with Xamlon but NeoSwiff is pretty well thought out and is already pretty usable(The bytecode of generated by the NeoSwiff compiler looks to be very solid, haven’t looked at Xamlon).

Although GlobFX isn’t mainly targeting this at the Flash Community as it looks like they interested in providing an alternative for the .Net/C# developers, this is great news for the SWF platform as a whole and any Flash geek should give it a wirl.

FITC 2005 this weekend, presenting on “Mission Critical Applications and Flash”

This year’s FlashintheCan is shaping up really nicely. They have an excellent speaker lineup, everyone I know is going to be there, and I’m looking forward to giving my presentation on Mission Critical Application Architecture and Flash“. Early on Shawn from the organizing crew asked me if I would speak again this year and when presenting them with this topic they were open to the idea. Not many conferences are usually open to new/non-standard subject matter and I think that’s one of the great things about FITC. This session will be interesting to present as it will be the first time where I get to show some of the work that I do in public and go through the different parts (both Flash and Backend). The session format will be different than my other presentations as the code isn’t the core of the presentation but the overall picture is more important.

Session Description:
Flash has come a long way since its early days of being only an animation tool, but being used in mission-critical applications? In this session Chafic will detail his experience on developing a state-based application architecture for a mission-critical product in use at many University Hospitals. The session will include guidelines on when such an architecture is appropriate, real world insight into implementation complexities, and what recovery/redundancy mechanisms are possible.

The session dissects such an architecture in a real-life application that successfully implements a real-time examination system to automate multi-million dollar hardware, provide transparent fail-over and recovery, simplifies long-term maintenance, and is very scalable

I arrive in Toronto Thursday night and look forward to meeting up with everyone. If you’re going to be there make sure to say hi!

New PrimalScript Release: CFMX7, CSS, and Double-Extensions and more!

Sapien put out a nice update to their IDE, PrimalScript with a few big additions.

  • ColdFusion MX 7. The PrimalSense files have been updated with all of the new CFMX7 syntax.
  • CSS. PrimalScript now gives PrimalSense on CSS files–it lists a drop-down of all valid CSS property names (use the toolbar button or CTRL-Space).
  • Double-Extensions. This is generic for all server-side code, but is really geared towards CF developers as it’s primarily a CF pratice. PrimalScript now recognizes files that have two extensions and treats the file based on the type of the first extension, not the last. So if you want to protect your css files via CF but still want PrimalScript help while editing them, you can use a name like “site.css.cfm” and PrimalScript will recognize the “.css.cfm” double-extension and treate the file as a CSS file. This is espeically useful for Fusebox developers who use double-extensions for their controller files such as “fusebox.xml.cfm” and “circuit.xml.cfm”.

If you haven’t checked out PrimalScript for CFML and AS development, we’d strongly suggest trying it out.