Join Walt Ritscher for an in-depth discussion in this video Working with code snippets, part of Visual Studio 2010 Essential Training.
A few years back, Visual Studio introduced the idea of code snippets. Snippets are small sections of code that can be automatically generated and pasted into your code. They show up in IntelliSense and are customizable, so that you can modify the injected snippet. Let me show you how to use a C# snippet. I am inside Visual Studio, and I have opened this solution called UsingCodeSnippets. I have two projects in here: a C# project and a Vb project. I'll start by opening this Program.cs file, and then I'll add my first snippet. I will start by typing in the word "if".
Now if you look in my IntelliSense dropdown, you will see the word "if" and to the immediate left of it is a symbol that looks like a piece of paper with some writing on it. That is the symbol for a code snippet. Now if I press the Tab key twice, once will dismiss that dialog, and the second time I press Tab, it stubs in the code snippet. In my case, you see it wrote a little bit of code on the line and also wrote two curly brackets for me. Some of the code snippets are much bigger than this. For my next snippet, I am going to use a for snippet.
It's going to be slightly larger than the if statement. I will type in "for" and then press the Tab key twice. Now what I want you to see here is that the letter I is in their three places, and notice how each one of them has some gray around it. So I am going to type in a new word for i. I am going to type in the word "counter". Now when I press the Tab key, it's going to move me to the next area, which is length. Also notice what happened to those i's. They turned into the word counter. So they are linked together. If I go back here and make a change and press the Tab key again, you see that it's constantly monitoring that first item and changing the other two items. That's very handy.
Next, I will show you an even larger one. This is a property for WPF. I am going to type in "prop dp". That's stands for dependency property. Now I will press Tab twice, and you can see it's stubbed in about eight or nine lines of code. Again, I've got these placeholders. So I can type in the word "Boolean" here or "bool", and then Tab, "IsReady", and you'll see that everywhere where it says MyPropertyProperty now, when I press the Tab key now, those change to say IsReadyProperty.
This property down here is changed to be Boolean, to match what I typed in up here. So did this one. Now those clever people over in the Visual Basic team, they are working really hard to simplify code entry. They have created about 200 snippets, which is more than the C# team did. So I am going to switch over to this Module1.vb file and show you how to add a snippet here. The trigger in Visual Basic is to type the question mark and then the Tab key. Now, I see several folders. Each one of these folders has lots of snippets in it.
I encourage you to explore these snippets. I only have time to show you one. So I'm going to go down here and show you some Office Development code snippets. I am going to open this one up and then choose Word and then Formatting, and here are some of the things I can put in if I'm doing some automation and adding items to the Word object model. Like I am going to choose this Highlight a Range, and you can see that it stubbed this piece in. So I encourage you to spend sometime looking at the vb snippets. I'd rather spend the time writing our own code snippets. To do that, I need to write some code, and I need to generate an xml file.
To make it easier, there is a free tool for you to use, called Snippet designer. I have already installed it on my machine. Let me show you where it is. I can go to Tools > Extension Manager, and there you can see that I have installed extensions. I have this one called Snippet Designer. You're not going to see this on your computer probably, unless you've already installed it. On your machine, you're going to want to click on the Online Gallery, which takes you out to a Microsoft web site and shows you all of the available extensions. I have a separate movie in this title that deals with adding your own extensions and using extensions.
Later in this course, there's a movie showing you how to write your own extensions and how to use these pre built extensions. For right now, just follow these instructions. Type the word "snippet" over here in the Online Gallery search engine, then pick snippet designer, and then you are going to click on a download. I don't have a download button, because I've already installed it, but you will have one. Click on that, and about 10 seconds later you will have Snippet Designer on your machine. Now you need to restart Visual Studio.
Once that's done, I can go find some code. I've got some C# code. So I am going to switch back to my Program.cs file, and I am going to uncomment these four lines of text, Ctrl+K, Ctrl+U for uncomment. Now I'm going to right-click on them and choose Export as Snippet, which will send them to that extension tool I just showed you. Here is that Snippet Designer. Now what I want to have happen when you enter this snippet is I want you to be able to type in a new value here and replace it on line two and on line three and on line five.
So what I do is I right-click on this and say Make Replacement. That puts a token in there called myList, and that tells snippet engine to use that replaceable syntax. So now I am ready. I am going to close this. It says, "Would you like to save the changes." I am going to say yes. I need to save this snippet file out in the location where Visual Studio looks for these snippets. That would be in Documents > Visual Studio 2010 > Code Snippets > C#, and of course, here's My Code Snippets folder.
I will open this one up and then click Save. For some reason, on this computer I keep getting this error dialog. I'm not sure if you are going to see it or not, but the snippet seems to work correctly. So I am going to click OK. Now in order for Visual Studio to load that snippet, I need to restart Visual Studio. Then I want to create a brand-new project. I am going to make sure it's a C# project. I am going to put it out in my movie folder. I'm in the chapter 05, in movie 7. And if I did everything correctly, I should be able to use my code snippet. Right-click. Choose Insert Snippet.
There's my Code Snippets Folder, there's my Snippet File number1, and there's my list. Then again, if I did everything correctly, I should be able to type in a new name here, fileNames, and press the Tab key. The Tab key is not working for me. How about if I just clicked down here? That worked. That is Awesome! Look at that! The file name changed in all four places. So as you have seen, code snippets are incredibly useful, and they are so easy to create. Have fun exploring and see what you can do with code snippets.
- Creating a Visual Studio project
- Building the user interface
- Binding to an RSS feed
- Coding with IntelliSense
- Creating rich Internet applications with Silverlight
- Building Windows applications with Windows Forms
- Integrating with SQL Server
- Working with Microsoft Office applications
- Understanding extensibility in Visual Studio
- Working with data, ADO.NET and datasets
- Using source control