Introducing Visual Studio 2005 IntelliSense Code Snippets
On the left side of the dialog window, click the checkbox next to "My Code Snippets" and click the Finish button (see Figure 14).
You now can open the "My Code Snippets" node and highlight the c3header snippet (see Figure 15). This will show some of the general attributes that you assigned to the snippet in the XML file.
Figure 15: Open the "My Code Snippets" Node and Highlight the c3header Snippet
Now, you can go to a C# class file and test out your new snippet. Figure 16 shows the output.
Figure 16: Output from Testing Out Your New Snippet
It may not seem like a lot, but the fact that you can type the shortcut and insert your own header is only the beginning of what you will be able to do.
Even though that was not a lot of steps, the fact that you dealt with raw XML might be a little more than you want to handle. So, the next two sections show you how I mass-produce my snippets, as well as an open source project that allows you to create and test your snippets.
I found myself creating code snippets for a lot of the same methods that I was overriding. So, I created a Visual Studio Macro to create a snippet out of the code that I selected. Download the macro and the test project. Load the macro in Visual Studio, and then highlight the code out of which you want to create a snippet (see Figure 17).
Figure 17: Highlight the Code Out of Which You Want to Create a Snippet
From the macro explorer, double-click the CreateSnippet macro (see Figure 18).
Figure 18: Double Click the CreateSnippet Macro
The screen will wiggle, and then you will have the output in a new file in the solution explorer. The file content from what you selected above will look like Figure 19.
Figure 19: The File Content from Your Selections
It's still a little raw, but you should be able to create your own templates to help generate snippets pretty quickly.
Visual Basic Code Snippet Editor
The open source project I mentioned at the end of the previous section is the Visual Basic Code Snippet Editor, a tool that enables you to create, edit, and test your code snippets. Take the time to download the Visual Studio 2005 edition from the GotDotNet Workspace. The enhancements from the Beta 2 version are well worth it. It is still not completed, but the team working on this is doing a great job. Figure 20 shows a screen shot.
Figure 20: Screenshot of Visual Basic Code Snippet Editor
Your Own Snippet Library
Take the Visual Basic Code Snippet Editor for a spin and try some of the tips in this article and the Visual Studio 2005 BOL. You will have a nice library of your own snippets in no time. I hope this tutorial gets you to explore the new features of IntelliSense code snippet technology in Visual Studio 2005.
Download the Code
Click here to download the snippet macro and the test project.
About the Author
Patrick Gallucci is a Sr. Consultant in the Communications Sector at Microsoft. He has more than thirteen years experience in information technology. Patrick's area of focus is developing operational efficiencies using development tools and technologies such as .NET, SQL Server, BizTalk Server, Content Management Server, and Commerce Server. His certifications include MCSE, MCDBA and MCT. Patrick is a recent instructor of MSF Design, Microsoft C#, ASP.NET, and other .NET classes. You can contact Patrick at firstname.lastname@example.org.
Page 3 of 3