Helixoft

Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

Macro for Adding New Property

Visual Studio 2002 and 2003 contains many sophisticated tools but it misses one of the most important - new procedure/property builder. MZ Tools add-in makes a great job here.

If you only need to create new property, I'll show you a simple macro which will help you. It will ask you for property name and type and then it will place property definition at current cursor position.

Button for Inserting Property

Insert Property Dialog

My example creates following VB .NET property but the macro can be easily modified to generate property for other language as well, e.g. C#:

    Private m_myProperty As String
    '''<summary></summary>
    Public Property myProperty() As String
        Get
            Return m_myProperty
        End Get
        Set(ByVal Value As String)
            m_myProperty = Value
        End Set
    End Property

Here is the macro:

    '''<summary>
    '''This macro inserts complete property definition
    '''including private field.
    '''</summary>
    Sub InsertProperty()
        DTE.UndoContext.Open("InsertProperty")
        Try
            Dim txt As TextSelection
            txt = DTE.ActiveDocument.Selection

            Dim line As String = InputBox("Name Type", "New Property")
            Dim lines() As String
            lines = Split(line, " ")

            Dim variableName, publicName, dataType, propertyProcedure As String
            publicName = lines(0)
            dataType = lines(1)
            variableName = "m_" & publicName

            propertyProcedure = _
                String.Format( _
                    "Private {3} As {2}{0}" _
                    & "'''<summary></summary>{0}" _
                    & "Public Property {1} As {2}{0}" _
                    & "    Get{0}" _
                    & "        Return {3}{0}" _
                    & "    End Get{0}" _
                    & "    Set(ByVal Value As {2}){0}" _
                    & "        {3} = Value{0}" _
                    & "    End Set{0}" _
                    & "End Property", vbCrLf, publicName, _
                    dataType, variableName)

            txt.Insert(propertyProcedure, _
                vsInsertFlags.vsInsertFlagsInsertAtEnd)
            txt.SmartFormat()
        Catch ex As System.Exception
            'MsgBox("There was an error while constructing Property code: " & _
            '    ex.ToString & ".", MsgBoxStyle.Critical, "New Property")
        End Try
        DTE.UndoContext.Close()
    End Sub

See how to create and run macro if you don't know how. You can of course create a toolbar or menu button or assign keyboard shortcut to it.

3 Responses to “Macro for Adding New Property”

  1. James Manning:

    In VS2005, make sure to check out Code Snippets

    http://www.google.com/search?q=visual+studio+code+snippets

  2. JM:

    These lines will prevent the document from losing focus in VS2008:
    DTE.ActiveDocument.Selection.StartOfLine(0)
    DTE.ActiveDocument.Selection.LineDown(False, 9) ‘the 9 comes from the numbers of lines in propertyProcedure
    DTE.ActiveDocument.Selection.EndOfLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Activate() ‘ else the cursor is gone or so, a little bit strange (focus is lost)
    DTE.UndoContext.Close()

    It also sets the mouse cursor beneath the property which is very usefull when adding a lot of prop’s.

    Thanks for the macro.

  3. Hoque Md.Nazmul:

    Now writing properties get become easier. I use c#, so I changed the line as follows,
    String.Format( _
    “private {2} {3};{0}” _
    & “/// {0}” _
    & “public {2} {1} {4}{0}” _
    & ” get {4} {0}” _
    & ” return {3} ;{0} ” _
    & ” {5}{0}” _
    & ” set {4} {0}” _
    & ” {3} = value ;{0}” _
    & ” {5} {0}” _
    & “{5}”, vbCrLf, publicName, _
    dataType, variableName, “{”, “}”)

Leave a Reply

authimage

Can't read the code? Try reloading the page before adding your comment to get a new code image.