Code Snippets in Visual Studio 2005
page 4 of 7
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35101/ 46

A Code Snippet

In this section, I will demonstrate on a ready-made code snippet that ships with Visual Studio 2005.

Listing 1 below shows the property code snippet that ships with Visual Studio 2005.

<?xml version="1.0" encoding="utf-8" ?>

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

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>prop</Title>

      <Shortcut>prop</Shortcut>

      <Description>Code snippet for property and backing field</Description>

      <Author>Microsoft Corporation</Author>

      <SnippetTypes>

        <SnippetType>Expansion</SnippetType>

      </SnippetTypes>

    </Header>

    <Snippet>

      <Declarations>

        <Literal>

          <ID>type</ID>

          <ToolTip>Property type</ToolTip>

          <Default>int</Default>

        </Literal>

        <Literal>

          <ID>property</ID>

          <ToolTip>Property name</ToolTip>

          <Default>MyProperty</Default>

        </Literal>

        <Literal>

          <ID>field</ID>

          <ToolTip>The variable backing this property</ToolTip>

          <Default>myVar</Default>

        </Literal>

      </Declarations>

      <Code Language="csharp">

<![CDATA[private $type$ $field$;

public $type$ $property$

{

  get { return $field$;}

  set { $field$ = value;}

}

$end$]]>     

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

In the Header section, you can see the informative information about the code snippet including a title, a shortcut, a description, an author, and snippet types.

The Snippet section includes two sections, the Declarations and the Code section.

As mentioned above, the Declarations section contains definitions for the replicable values inside the code snippet. In this example, the prop code snippet should define three replicable values, the private field name, the property name, and finally the property data type.

The mandatory code section includes the usual code for a property. You can see the replicable values surrounded by the $ sign. Those values have been defined above in the Declarations section. It is the developer using the code snippet who would replace those values with proper values.


View Entire Article

User Comments

Title: developer   
Name: sunil
Date: 2009-10-27 9:23:04 AM
Comment:
very nice exmpl
Title: developer   
Name: nanda
Date: 2007-08-06 2:00:31 AM
Comment:
nice article..
Title: Developer   
Name: Stefan Leoni
Date: 2006-12-13 7:18:51 AM
Comment:
concise. Excactly what i was looking for.

Thanks.
Title: Re:   
Name: Bilal Haidar
Date: 2006-01-20 3:38:15 PM
Comment:
You can now, discuss, comment, and ask me questions related to this article at this forum:

http://bhaidar.net/cs/forums/10/ShowForum.aspx

Regards
Title: Solution Developer   
Name: Mostafa
Date: 2006-01-07 3:05:53 AM
Comment:
Good Article.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 3:33:56 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search