April 19, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using Code Snippets in Visual Studio 2005, Page 2

  • May 16, 2005
  • By Paul Kimmel, Paul Kimmel
  • Send Email »
  • More Articles »

Listing 3 shows a declarations section. Listing 4 shows a complete example demonstrating how to create a code snippet that generates a strongly typed collection.

Listing 3: Example Declarations Section for a Code Snippet

<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>

Listing 4: Complete Code Snippet That Will Stub a Strongly Typed Collection

<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/
                            2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Create a typed collection</Title>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.dll</Assembly>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>System.Collections</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>CLASS</ID>
          <Default>Customer</Default>
        </Literal>
      </Declarations>
      <Code Language="VB"><![CDATA[Public Class $CLASS$Collection 
        Inherits System.Collections

      Public Default Property Item(ByVal Index As Integer) As $CLASS$
  Get
    Return CType(List(Index), $CLASS$)
  End Get
  Set(ByVal Value As $CLASS$)
    List(Index) = Value
  End Set
  End Property

  Public Function Add(ByVal Value As $CLASS$) As Integer
    Return List.Add(Value)
  End Function
End Class]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

I created the XML snippet file in Listing 4 by entering the code in Listing 5 in the editor tab of the snippets editor.

Listing 5: Using the Snippets Editor Is Easier Than Writing the XML from Scratch

Public Class $CLASS$Collection 
  Inherits System.Collections

  Public Default Property Item(ByVal Index As Integer) As $CLASS$
    Get
      Return CType(List(Index), $CLASS$)
    End Get
    Set(ByVal Value As $CLASS$)
      List(Index) = Value
    End Set
  End Property

  Public Function Add(ByVal Value As $CLASS$) As Integer
    Return List.Add(Value)
  End Function
End Class

As you can see, using the editor is much simpler than writing the XML (in Listing 4). When Visual Studio 2005 ships, you will be able to select code from your projects and create a snippet directly from the code editor.

Manage Snippets

The Tools|Code Snippets Manager is a centralized manager for organizaing code snippets. You can add additional directories; import, add, and remove snippets; and search for snippets online.

Keep in mind that VS 2005 is still in beta, so some elements of the beta Code Snippets Manager may be a bit cantankerous. For now, all you can do is use the temporary code snippets editor and save your snippets to C:\Documents and Settings\yourusername\My Documents\Visual Studio\Code Snippets\Visual Basic\My Code Snippets. As long as you save the files with a .snippet extension and give them titles, they will show up in the IntelliSense snippets dropdown menu (see Figure 1).



Click here for a larger image.

Figure 1: The VB Code Snippets Editor





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel