Geeks With Blogs

News INETA Community Speakers Program
GeeksWithBlogs.net: WTFNext's hosting!

View Stacy Vicknair's profile on LinkedIn


WTF Next? Dev ramblings from a master of nothing.

Here we are at part two of the Custom Code Snippet Series. In the previous part we covered how to make your own snippet by creating an XML .snippet file and adding to it the necessary content to get a custom snippet up and running.

This time around, we’re going to cover the “Easy” method of creating custom code snippets: through a GUI. Thankfully, the MSDN itself has a Code Snippet Editor for both Visual Basic 2005 and 2008 available at the following links. This tutorial will make use of the Visual Basic 2005 Code Snippet Editor.

To get the 2005 version:
http://msdn.microsoft.com/en-us/vbasic/ms789085.aspx

to get the 2008 version:
http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx

So, after installing the Code Snippet Editor, we’re ready to get started.

 

Add the path to My Code Snippets

Right off the bat, you can tell we are going to have a small issue. Looking through the tree in the snippet editor does not show the custom code snippets! That’s alright, we’ll just add our path.

Right-click in the tree view area of the program and select Add Path…. Browse to the path we discussed for saving snippets in the previous part:My Documents > Visual Studio YYYY > Code Snippets > Language of Choice > My Code Snippets.

image

Now we can see our custom code snippets in the editor. If you’ve done the previous part in this series you should see the cmdWrite snippet show up in the tree under the My Code Snippets folder:

image

 

Create new snippet in My Code Snippets

Why do I stress putting the new snippets in our My Code Snippets folder? If you want a centralized location that is somewhat protected from other users of the computer, in your My Documents folder makes sense. You might even want to make your own location that you point Visual Studio to, that way you can implement version control to make sure changes benefit your code and rollback if they do not.

To create a new snippet, just right click the folder and select Add New Snippet. Name your snippet, today we’re going to make a simple For loop, so I’ll name this one ForLoop. Obviously, you’ll make snippets that matter more.

Once you create the snippet, double click on it. Here you’ll see a GUI representation of the XML document. Fairly straightforward.

To start, let’s fill in our info. Here’s what I put:

image

 

Now let’s put in our VB code into the editor at the top. Here’s what I put:

Dim count as integer = 0
Dim countMax as integer = 100

For count = 0 to countMax
    'Something to do
Next

Now we’ll transform this code into a dynamic snippet. I want count to be a choice for the variable name, so highlight count and on the Replacements tab at the bottom hit the yellow plus sign.

count will become surrounded by $ symbols, and become a decadent orange color. At the bottom, you’ll see that most information has been filled in for us. For type, we’ll leave it blank, and for tooltip, we’ll just describe the replacement. Now, do the same for countMax.

We’re practically done! Flip over to the Test tab and hit the big test button. Did it work okay? If so, we’re ready to find it in VS and start using it, just make sure to save your snippet!

 

Check it out in Visual Studio

Go ahead and close the Snippet Editor and open up a new console project in Visual Studio, VB of course.

If you’ve set up the same snippet as me, go ahead and type ForLoop? and hit Tab twice and you should see your new snippet come to life!

image

Posted on Monday, October 20, 2008 10:58 AM VB .NET , How-To | Back to top


Comments on this post: Custom Code Snippets Series Part 2 of 3: The “Easy” Way

# re: Custom Code Snippets Series Part 2 of 3: The “Easy” Way
Requesting Gravatar...
This article is too simple and too good. Explained everything in simple way.
Want to read part 3 of 3 .

Best Regards!
Matt
Left by free ebooks download on Oct 25, 2008 1:20 AM

Your comment:
 (will show your gravatar)


Copyright © Stacy Vicknair | Powered by: GeeksWithBlogs.net