Click here to Skip to main content
15,891,431 members
Articles / Operating Systems / Windows

VS.NET Macro Creation

Rate me:
Please Sign up or sign in to vote.
4.39/5 (16 votes)
2 Mar 2004CPOL3 min read 53.3K   500   27   1
An article on how to create VS.NET Macros and automate repetitive coding tasks

Image 1

Introduction

Sure, I have heard macros are great and save you a lot of time in development, but to be honest I never had the time to develop any because I was too busy developing. Well today I was met with a rare development lull and decided to invest in learning how to create macros in Visual Studio .NET and save some time in the future.

The macro that we are going to create is one that will automatically create our member variables as well as their corresponding public properties. To demonstrate how a macro can do different things dependant upon user input, we will include a message box asking whether or not the members should be sorted alphabetically.

Using the code

Macro Requirements:

The macro needs to turn this

VB.NET
CustomerName String
AccountBalance Double
CustomerPhone String
CustomerAddress String
CustomerID Integer

into this:

VB.NET
Private mAccountBalance as Double
Private mCustomerAddress as String
Private mCustomerID as Integer
Private mCustomerName as String
Private mCustomerPhone As String

Public Property AccountBalance As Double
    Get
        Return mAccountBalance
    End Get
    Set(ByVal Value As Double)
        mAccountBalance = Value
    End Set
End Property
Public Property CustomerAddress As String
    Get
        Return mCustomerAddress
    End Get
    Set(ByVal Value As String)
        mCustomerAddress = Value
    End Set
End Property
Public Property CustomerID As Integer
    Get
        Return mCustomerID
    End Get
    Set(ByVal Value As Integer)
        mCustomerID = Value
    End Set
End Property
Public Property CustomerName As String
    Get
        Return mCustomerName
    End Get
    Set(ByVal Value As String)
        mCustomerName = Value
    End Set
End Property
Public Property CustomerPhone As String
    Get
        Return mCustomerPhone
    End Get
    Set(ByVal Value As String)
        mCustomerPhone = Value
    End Set
End Property

Getting Started:

To create our macro we will need to enter the Macro IDE (Tools -> Macros -> Macro IDE or Alt+F11). From within the Macro IDE right click an existing macro project or create a new one and add a macro module. You can have a number of methods within each macro but for now we only need one called ConvertProperties(). This method will take the text that is currently selected, process it and replace it with member and property declarations.

Accessing the Selected Text:

The EnvDTE name space allows us to get access to what text the user has selected.

VB.NET
Dim txt As TextSelection
txt = DTE.ActiveDocument.Selection

Using RegEx:

Regular Expressions will come in handy to identify the text that is selected and allow us to step through the property names and data types successfully. The first word it comes across will be the varname and the second will be the typename.

VB.NET
Dim r As Regex
r = New Regex( _
"\s*(?<varname>\S*)\s*(?<typename>\S*)", _
RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)

Line by Line Processing:

By doing a split on the selected text via a line feed we end up with an array of lines that we can walk through. We see if the line matches our regular expression, and if it does, we build the member syntax as well as the property syntax using the varname and typename as parameters. After the syntax is dynamically generated we can insert it into the document. Note that the code below is stripped down for simplicity.

VB.NET
Dim line, originalCode As String
Dim lines() As String = Split(txt.Text, vbLf)
For Each line In lines
    line = line.Trim
    If Not line = "" Then
        Dim mtch As Match
        mtch = r.Match(line)
        If mtch.Success Then
            publicName = mtch.Groups("varname").Value.Trim
            dataType = mtch.Groups("typename").Value.Trim
            memberName = String.Concat("m", publicName)
            propertyProcedure = _
                String.Format("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, memberName)
            txt.Insert(vbCrLf & propertyProcedure)
        End If
    End If
Next

User Input:

You may have a need to allow for the user to dictate how the macro behaves. To demonstrate this in our macro, we will cause the macro to throw up a dialog box that asks the user if the members should be sorted alphabetically.

VB.NET
Dim sortAZ As MsgBoxResult
sortAZ = MsgBox("Sort Alphabetically", MsgBoxStyle.YesNo, "Sort Order")
If sortAZ = MsgBoxResult.Yes Then
    System.Array.Sort(lines)
End If

Using Our Macro:

Now that we have created our macro, we now need a way of using it. This can be done by selecting our text that has a property name and data type on each line and then double clicking our macro method name in the macro explorer. We can also assign a short cut key such as Alt-M by going into Tools -> Options -> Environment -> Keyboard and assigning the Macros.MerlinMacros.Merlin.ConvertProperties to the Alt-M key.

Points of Interest

There is a product called QuickCode .NET (http://www.dvxp.com/en/QuickCode.aspx ) which has some cool features for basic macro type functionality. I quickly met the limitations of the product when trying to process more than 1 line at a time, but it is this reason I looked into creating my first macro in the first place.

History

  • 2/25/2004 - article submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Independent Consultant
United States United States
___________________________
J A M E S C O L E M A N
Director, Technical Services
Linked-In: http://www.linkedin.com/in/jameswcoleman
Blog: ledtalks.wordpress.com

Comments and Discussions

 
GeneralUpgrade... Pin
Terence Wallace21-Dec-04 5:11
Terence Wallace21-Dec-04 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.