BLOG.CSHARPHELPER.COM: Make a macro in the Visual Studio IDE that lets you easily insert standard comments into your code
Make a macro in the Visual Studio IDE that lets you easily insert standard comments into your code
This entry explains how you can write a macro to automatically add a standard comment to your code. For example, suppose you want a comment similar to the following in front of any method.
// Author: Rod Stephens
// Created: 4/1/2011
// Purpose: To pick the winning football team given a set of statistics.
// Method: Perform statistical analysis of ...
// Issues: It doesn't work.
You can write a macro to create a comment template and insert it into the code. It can even fill in your name and the current date.
To record a macro, open the Tools menu's Macros submenu and pick the Record Temporary Macro command. Type whatever code you want in Visual Basic and it will be recorded into the macro. When you're done, click the Macro Recorder's stop button. Use the Macro Explorer to edit the macro and rename it if you want to keep it.
To open the Macro Explorer, open the Tools menu's Macros submenu and pick the Macro Explorer command.
To create a macro from scratch, open the Macro Explorer, right-click on MyMacros, and select New Module to add a new macro module. Give it a good name, double-click it to open it, and type in your code.
To edit a macro (including the temporary macro), find it in the Macro Explorer, right-click it, and select Edit.
To run a macro, double-click it in Macro Explorer.
The following example shows a macro that adds the previous code comment. Note that this code is in Visual Basic for Applications (VBA), the macro language used by Microsoft products such as Visual Studio, Excel, or Word.
That's nice but to use the macro you need to open the Macro Explorer, find the macro, and double-click it. That's pretty awkward. Fortunately you can make using the macro much easier.
Back in the normal Visual Studio IDE, open the Tools menu and select Customize. The steps you follow next depend on your version of Visual Studio (because Microsoft keeps fiddling with this). In Visual Studio 2008:
Select the Commands tab.
Pick the Macros category.
Find your macro. For example, it might be called MyMacros.StandardComments.AddStandardMethodComment.
Click and drag the macro onto an IDE toolbar or into an IDE menu.
Right-click the new tool or menu item and use the dropdown menu on the right to customize the item. For example, you can make it display text or an image, and you can edit the image it displays.
Now when you're writing code, you can click the tool to insert the standard comment with your name and the current date filled in.
To remove the tool or menu item, enter customize mode again. Then click and drag the tool onto the customize dialog or anywhere else it cannot be placed in the IDE.
(Note that there is no download for this entry because the customization is contained in Visual Studio not a particular example.)
Thanks a lot... works like a charm!!
Reply to this