Hack 15. Use and Share Code Snippets![]() people's code and share some of your own.Visual Studio 2005 introduces a new feature called Code Snippets that allows you to insert a piece of example code directly into the class you are working with. At the time of this writing, this feature is available only for Visual Basic. 2.11.1. Insert Code SnippetsYou can quickly insert example pieces of code by right-clicking in the area where you want to insert code, then clicking the Insert Snippet item on the context menu that appears. A list of code categories appears, as shown in Figure 2-39. Figure 2-39. Code Snippet categories![]() of examples that you can automatically insert, as shown in Figure 2-40. Figure 2-40. Accessing Data example list![]() columns and a primary key" snippet, and the following code will be inserted into the class: Private Sub CreateTable( )Of course, this code probably does not do exactly what you want it to do, but it gives you a great start to work from. Code Snippets are a great way to incorporate examples into your everyday coding, but wouldn't it be great if you could build your own library of code snippets? 2.11.2. Add Your Own Code SnippetsA large number of code snippets are already loaded into Visual Studio, but you can also add your own. This could be very useful if you want to create snippets for common pieces of code you use or code that is specific to your project or architecture. You will also be able to download and add code snippets from community sites as well; I have a feeling that it will become common for people to post snippet files on their sites and blogs. To add or modify code snippets, you need to first create a snippet file. For Visual Basic, this is a structured file with the extension of .vbsnippet.The creation and editing for the .vbsnippet files is not complete in the Beta 1 release of Visual Studio 2005, so hopefully this will be easier in the future. For now, the best way to create new snippet files is to start with an existing snippet file. These can be found in the Visual Studio Directory\Vb\Snippets\1033 directory. First, you need to make a copy of an existing snippet file, then open the copy in Visual Studio. At the time of this writing, Visual Studio will treat this file as a solution file and provide a number of property windows to set the name of the snippet as well as the author and other metadata. After entering your own snippet, you can save the modified file and then add it to Visual Studio using the Code Snippets Manager located under the Tools menu in Visual Studio 2005.There won't be a complete editor included in the final version of Visual Studio 2005, but the team plans to release a separate application at or around the release of Visual Studio 2005 that will provide a rich code snippet editing experience . |