Udostępnij za pośrednictwem


How to: Create a New Snippet with Imports and References

If you create a Visual Basic code snippet, you have the ability to specify the necessary references and Imports statements required for your code to run. When the code snippet is inserted, Visual Studio will add the specified references and Imports statements to the project.

Note

The Imports and References elements can only be used with Visual Basic code snippets.

Adding Imports and References

These procedures assume that you have followed the procedures explained in the How to: Create a Basic Code Snippet topic.

To specify references

  1. Locate the Snippet element of the code snippet.

  2. Add a References element as a child of the Snippet element. The References element is used to group the necessary references for the code snippet.

  3. Add a Reference element as a child of the References element. The Reference element specifies an individual reference.

  4. Add an Assembly element as a child of the Reference element. Make the text value of the element the name of the assembly that you want added as a project reference. For more information, see Assembly Element (IntelliSense Code Snippets).

  5. Optionally, you can add a Url element as a child of the Reference element. The text value of the Url element specifies a URL that provides more information about the referenced assembly.

To specify Imports statments

  1. Locate the Snippet element of the code snippet.

  2. Add an Imports element as a child of the Snippet element. The Imports element is used to group the necessary Imports statements for the code snippet.

  3. Add an Import element as a child of the Imports element. The Import element specifies an individual namespace to import.

  4. Add a Namespace element as a child of the Import element. Make the text value of the element the name of the namespace that you want to import.

Example

The following code example shows a code snippet with both the Imports and References elements. When this code snippet is inserted, Visual Studio will add a reference to System.Data.dll, and following line of code at the beginning of the file:

Imports System.Data

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

            <!-- Add Header information here -->

        </Header>
        <Snippet>
            <References>
                <Reference>
                    <Assembly>System.Data.dll</Assembly>
                </Reference>
            </References>
            <Imports>
                <Import>
                    <Namespace>System.Data</Namespace>
                </Import>
            </Imports>

            <!-- Add rest of code snippet here -->

        </Snippet>
    </CodeSnippet>
</CodeSnippets>

See Also

Tasks

How to: Create a Basic Code Snippet

Reference

Creating and Using IntelliSense Code Snippets

Snippet Element (IntelliSense Code Snippets)

Imports Element (IntelliSense Code Snippets)

Import Element (IntelliSense Code Snippets)

Namespace Element (IntelliSense Code Snippets)

References Element (IntelliSense Code Snippet)

Reference Element (IntelliSense Code Snippets)

Assembly Element (IntelliSense Code Snippets)

Url Element (IntelliSense Code Snippets)