Udostępnij za pośrednictwem


How to: Create a New Snippet with Replacements

You may have portions of code snippets that you want to be replaced by the person inserting them. This is useful if you want to reference a specific variable or object, and want the user to be able to replace the variable or object with one in his or her project. IntelliSense Code Snippets provide this ability with the Literal and Object elements.

Once a code snippet is inserted, you can access all the possible replacement objects in your code by clicking on the replacement and pressing CTRL+Space.

Creating a Literal Replacement

The Literal element is used to identify a replacement for a piece of code that is entirely contained within the snippet, but will likely be customized after it is inserted into the code. For example, literal strings, numeric values, and some variable names should be declared as literals.

This procedure assumes that you have followed the procedures explained in the How to: Create a Basic Code Snippet topic.

To create a literal replacement

  1. Locate the Snippet element of the code snippet.

  2. Add a Declarations element as a child of the Snippet element. The Declarations element is used to group replacement declarations.

  3. Add a Literal element as a child of the Declarations element. The Literal element specifies an individual literal. A code snippet may have more than one literal replacement.

  4. Add an ID element as a child of the Literal element. The text value of this element specifies the name that you will use to reference the literal in the Code element.

  5. Add a Default element as a child of the Literal element. The text value of the Default element specifies the default value of the literal when you insert the code snippet.

  6. Optionally, add the Function and/or ToolTip elements.

Creating an Object Replacement

The Object element is used to identify an item that is required by the code snippet but is likely to be defined outside of the snippet itself. For example, Windows Forms controls, ASP.NET controls, object instances, and type instances should be declared as objects. Object declarations require that a type be specified.

This procedure assumes that you have followed the procedures explained in the How to: Create a Basic Code Snippet topic.

To create an object replacement

  1. Locate the Snippet element of the code snippet.

  2. Add a Declarations element as a child of the Snippet element. The Declarations element is used to group replacement declarations.

  3. Add an Object element as a child of the Declarations element. The Object element specifies an individual object. A code snippet may have more than one object replacement.

  4. Add an ID element as a child of the Object element. The text value of this element specifies the name that you will use to reference the object in the Code element.

  5. Add a Type element as a child of the Object element The text value of the Default element specifies the type of the object.

  6. Add a Default element as a child of the Object element. The text value of the Default element specifies the default value of the object when you insert the code snippet.

  7. Optionally, add the Function and/or ToolTip elements.

Referencing Replacements

Now that you have created literals and objects, you need a way to use them in the code that will be inserted by the code snippet. You reference the literals and objects you have declared in the Declarations element by placing $ symbols at the beginning and end of the value in the literal or object's ID element

To reference a literal or object in a Code element

  • Place $ symbols at the beginning and end of the literal or object's ID element value. For example, if a literal has an ID element that contains the value MyID, you would reference that literal in the Code element with $MyID$.

Example

The following code example shows a code snippet with both the Literal and Object elements. The SqlConnString literal is referenced in the Code element with $SqlConnString$, and the SqlConnection object is referenced with $SqlConnection$.

<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/CodeSnippet">
    <CodeSnippet>
        <Header>

            <!-- Add Header information here -->

        </Header>
        <Snippet>

            <!-- Add additional Snippet information here -->

            <Declarations>
                <Literal>
                    <ID>SqlConnString</ID>
                    <ToolTip>Replace with a SQL connection string.</ToolTip>
                    <Default>"SQL connection string"</Default>
                </Literal>
                <Object>
                    <ID>SqlConnection</ID>
                    <Type>System.Data.SqlClient.SqlConnection</Type>
                    <ToolTip>Replace with a connection object in your application.</ToolTip>
                    <Default>dcConnection</Default>
                </Object>
            </Declarations>
            <Code Language="CSharp">
                <![CDATA[
                    daCustomers = new SqlClient.SqlDataAdapter();
                    selectCommand = new SqlClient.SqlCommand($SqlConnString$);
                    daCustomers.SelectCommand = selectCommand;
                    daCustomers.SelectCommand.Connection = $SqlConnection$;
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

See Also

Tasks

How to: Create a Basic Code Snippet

Reference

Creating and Using IntelliSense Code Snippets

Declarations Element (IntelliSense Code Snippets)

Object Element (IntelliSense Code Snippets)

Literal Element (IntelliSense Code Snippets)

ID Element (IntelliSense Code Snippets)

Type Element (IntelliSense Code Snippets)

ToolTip Element (IntelliSense Code Snippets)

Function Element (IntelliSense Code Snippets)

Default Element (IntelliSense Code Snippets)