How to build your own code segment

Source: Internet
Author: User

Some code segments or tool functions are common in different projects. It is often used to search for a previous code segment to open a large project. After a long wait, copy and paste the code, which affects the efficiency.

You can use the code segment manager of VS to add your own code for future calls.

 

For example, a function for creating a DataTable is as follows:

Code

The following steps are required to join the code snippet Manager:

1. create the code snippet file GetDemoDataTable. snippet. create an xml file in vs, use the combined shortcut key CTRL + K + X in the XML file, select the Snippet code segment, and press the Tab key, you will find that Code Snippets also writes Code segments for creating Code segments.

<? Xml version = "1.0" encoding = "UTF-8"?>

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

<Header>

<Title> Title </Title>

<Author> Author </Author>

<Shortcut> Shortcut </Shortcut>

<Description> Description </Description>

<SnippetTypes>

<SnippetType> SurroundsWith </SnippetType>

<SnippetType> Expansion </SnippetType>

</SnippetTypes>

</Header>

<Snippet>

<Declarations>

<Literal>

<ID> name </ID>

<Default> value </Default>

</Literal>

</Declarations>

<Code Language = "XML">

<! [CDATA [<test>

<Name> $ name $ </name>

$ Selected $ end $ </test>]>

</Code>

</Snippet>

</CodeSnippet>

 

To:

<? Xml version = "1.0" encoding = "UTF-8"?>

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

<CodeSnippet Format = "1.0.0">

<Header>

<Title> GetDemoDt </Title>

<Author> zbl </Author>

<Shortcut> GetDemoDt </Shortcut>

<Description> Create a demo able </Description>

<SnippetTypes>

<SnippetType> SurroundsWith </SnippetType>

<SnippetType> Expansion </SnippetType>

</SnippetTypes>

</Header>

<Snippet>

<Code Language = "csharp">

<! [CDATA [

Public DataTable GetDemoDt ()

{

DataTable dt = new DataTable ();

Dt. Columns. AddRange (new DataColumn [] {new DataColumn ("student ID"), new DataColumn ("name ")});

For (int I = 1; I <20; I ++)

{

DataRow dr = dt. NewRow ();

Dr ["student ID"] = I. ToString ("0000 ");

Dr ["name"] = "name" + I. ToString ();

Dt. Rows. Add (dr );

}

Return dt;

}

]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets>

After comparing the two files, you can find out where the changes are made and find the meaning of each node on msdn. Save the modified xml file as the. Snippet file.

2. Import the code snippet File

Tool-code snippet Manager-import-select C #

3. Use a code segment File

Right-click the project and choose external code, for example:

 

 

 

Have a good reference article http://www.cnblogs.com/tannaiyin/archive/2009/04/04/1429316.html

However, csharp was written as cshap, and it took me five minutes to find this error, but thanks to the blogger.

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.