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.

 

Comments

# James Manning 2006-01-21 08:27
In VS2005, make sure to check out Code Snippets

http://www.google.com/search?q=visual+studio+code+snippets
# JM 2008-02-19 14:33
These lines will prevent the document from losing focus in VS2008:
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocum ent.Selection.L ineDown(False, 9) 'the 9 comes from the numbers of lines in propertyProcedure
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocum ent.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.
# Hoque Md.Nazmul 2010-05-28 09:35
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, "{", "}")

Comments are now closed for this entry

 

Start generating your .NET documentation now!
DOWNLOAD
Free, fully functional trial