Current .Net Version: 2.0.50727
Home
Articles
C# & .Net Framework Reference
Practice C# Online
XML Web Services
RSS Feeds
Code Snippets
T-SQL Scripts
Videos
Valuable Links
Contact Us
Mike's C#, VS.NET and SQL Blog




ROR XML Info.
What's New in Visual C# 2005 - Code Snippets
Author: Michael G.     Last Updated:10/8/2005 9:20:45 PM
Category(ies): Visual C# 2005
Description: Learn how to save time by reusing code in VS.NET 2005 with code snippets.
  Add & View Comments About this Article

Tested with .Net 2.0.50215, Visual Studio 2005 Beta 2 8.50215.44

Code snippets are XML files that contain snippets of code you can insert into the VS.NET Code Editor workspace. Code snippets are especially useful in situations where you frequently implement the same code blocks. You can save a potentially great amount of time by using code snippets that contain frequently used code.

Table of Contents

  • Using Code Snippets
  • Creating Code Snippets
  • Code Snippet Properties
  • Adding Code Snippets to VS.NET
  • A Collection of Free Code Snippets
  • Using Code Snippets

    Code snippets are accessible by a variety of methods available in the VS.NET IDE (Integrated Devlopment Environment). These are shown below.

    Ctrl-K-X: Inserts the Code Snippet at the Current Line in the Code Editor

    Ctrl-K-S: Surrounds the Current Code with the Code Snippet at the Current Location in the Code Editor

    Shortcut: If the code snippet file contains a Shortcut element, you can access the snippet via the shortcut: type the name of the Shortcut in the VS.NET code editor, or select it from the IntelliSense menu, at the point in the code where you want the code snippet to appear; then press the [Tab] key.

    Right-click: Right-click at the point in the code where you want to add the code snippet and then choose either Insert Snippet... or Surround with....

    Creating Code Snippets

    The code snippet XML file follows the structure shown below:

    The Beginning of the XML Code Snippet File: <?xml version="1.0" encoding="utf-8" ?> <CodeSnippets> <CodeSnippet Format="1.0.0"> The Header: <Header> <Title>Select All Records from SQL Server Database</Title> <Shortcut>selallsql</Shortcut> <Description>Test Code snippet.</Description> <Author>C# .Net Fun - http://www.dotnetfun.com - Michael G.</Author> </Header> The Beginning of the Snippet: <Snippet> The Beginning of the Declarations: <Declarations> Literal Declarations: <Literal> <ID>strConn</ID> <Type>System.String</Type> <ToolTip>Your SQL connection string.</ToolTip> <Default>"Data Source=HOSTNAME; User ID=userid; _ Password=password; Initial Catalog=Database;"</Default> </Literal> <Literal> <ID>strSQL</ID> <Type>System.String</Type> <ToolTip>Your SQL command string.</ToolTip> <Default>"SELECT * FROM MyTable;"</Default> </Literal> <Literal> <ID>TableName</ID> <Type>System.String</Type> <ToolTip>The name of the DataSet/database table.</ToolTip> <Default>"MyTable"</Default> </Literal> Object Declarations: <Object> <ID>DataSet</ID> <Type>System.Data.DataSet</Type> <ToolTip>The DataSet object that will hold an in-memory _ representation of your table.</ToolTip> <Default>new DataSet()</Default> </Object> The End of the Declarations: </Declarations> The Code of the Snippet: <Code Language="CSharp"> <![CDATA[ try { DataSet ds = $DataSet$; using (SqlConnection sqlConn = new SqlConnection($strConn$)) { using (SqlDataAdapter sqlAdp = _ new SqlDataAdapter($strSQL$, sqlConn)) { sqlAdp.fill(ds, $TableName$); } } } catch (SqlException e) { throw e; } catch (Exception e) { throw e; } ]]> </Code> The End of the Snippet: </Snippet> The End of the XML Code Snippet File: </CodeSnippet> </CodeSnippets> The IntelliSense Results (Ctrl-K-X): try { DataSet ds = new DataSet(); using (SqlConnection sqlConn = new SqlConnection("Data Source=HOSTNAME; _ User ID=userid; Password=password; Initial Catalog=Database;")) { using (SqlDataAdapter sqlAdp = _ new SqlDataAdapter("SELECT * FROM MyTable;", sqlConn)) { sqlAdp.fill(ds, "MyTable"); } } } catch (SqlException e) { throw e; } catch (Exception e) { throw e; } Note: an underscore (_) character in this example signifies the code continues to the following line.

    Code Snippet Properties

    File naming convention: all code snippet files must be XML-compliant and should have the extension .snippet.

    Header: this area contains information about your snippet.

  • Title: contains the name of the snippet. The Title shows up in the list of snippets to chose from when you issue a code snippet command in the VS.NET IDE.
  • Shortcut: contains a keyword that you can use to insert the code snippet by name and by pressing the [Tab] key, rather than by selecting it from a menu (which is explained above).
  • Description: contains a ToolTip type description that appears when the input focus is placed on a code snippet appearing in an IntelliSense menu.
  • Author: contains information about the author of the code snippet.
  • Literal Declaration: contains information about the literal variables used in the code snippet.

  • ID: The ID of a literal maps to the equivalent variable somewhere in the code of the code snippet. The name of the ID is like the name of a variable. The value of the ID is in the Default element of the Literal element (ID = Default). To use the Default value of the variable, you need to surround the name of the ID with dollar signs. For example, if the ID is strSQL, you would use $strSQL$ in the code portion of the code snippet. When the code snippet gets added to your code in the code editor, instead of the $strSQL$ appearing, the Literal Default value for strSQL appears. If the Default element contained the literal "SELECT * from MyTable" then whereever the $strSQL$ appears is where you would see this literal. So, think of ID the name of a variable you can use in your code which holds the literl value set in the Default element.
  • Type: the fully qualified type name of the ID variable. For example, System.Data.SqlClient.ConnectionString.
  • ToolTip: contains a ToolTip type description that appears when the input focus is placed on a code snippet appearing in an IntelliSense menu.
  • Default: contains the value of the ID variable. This is the value that actually shows up in the code of the code snippet.
  • Object Declaration: contains information about the object variables used in the code snippet.

    The properties of an Object declaration are the same as those for Literal declarations, which the exception of course being that an Object declaration is for objects.

    Code: contains the actual code of the code snippet.

    Make sure that you surround your code with the CDATA element: <![CDATA[ ...code... ]]>.

    Adding Code Snippets to VS.NET

    Code Snippets Manager: Use this tool to add code snippets to folders or to add code snippet folders the Snippet Inserter looks in when searching for code snippets to insert. You would use the Import... button to add a code snippet to a selected folder. You would use the Add... button to add a folder that contains code snippets to search.


    Comments Click Here to Add your Comment
     
    COPYRIGHT/DISCLAIMER | CONTACT US