What is Visual Studio .NET snippets? Part two.

    This is the second part of the article about snippets. The first gave an explanation of what kind of mechanism it was, why it was needed, and how to apply it. In the second part, we will talk about how to create snippets.

    For example, take a very simple generalized situation: the need to store hidden temporary data in the body of the page. Previously, hidden fields were used for this, but asp.net has a ViewState mechanism. I will not touch on the topic of using ViewState, its impact on performance and page size. Obviously, you need to use it carefully. So, let's create a snippet for such a code construction:
      public int ViewStatePropperty {get {object o = ViewState ["ViewStatePropperty"]; return (o! = null)? (int) o: 0; } set {ViewState ["ViewStatePropperty"] = value; }} * This source code was highlighted with Source Code Highlighter .


    The simplest property, the value of which will not be stored in a private field, as usual, but in the ViewState. Note that there are three mutable elements in this code: type, name, and default value. Moreover, the type string in the code is used twice, and the name string three times. Now let's define finally a snippet. In fact, a snippet is an xml file defined in a special way, and here is an implementation for our example:

    schemas.microsoft.com/VisualStudio/2005/CodeSnippet »>
      
        

          propv
          propv
          A simple snippet example
          Xaoc, for the Habrahabr project
          
            Expansion
          

        

        
          
            
              type
              Property type
              int
            

            
              property
              Property name
              Myproperty
            

            
              default
              Default value
              0
            

          

           {
      get
      {
        object o = ViewState["$property$"];
        return (o != null)? ($type$)o: $default$;
      }
      set
      {
        ViewState["$property$"] = value;
      }
    }$end$]]>
          

        

      

    * This source code was highlighted with Source Code Highlighter .

    So, what is the description of this snippet? The main tags are as follows: Header, which describes the outer part rather, and Snippet, which describes the logic.

    Header defines tags with values: the name of the snippet, shortcut (this is the name by which you can quickly call the snippet), description, author, and type of snippet. The type of snippet can have three meanings: SurroundsWith, Expansion, and Refactoring. The first one indicates that the snippet will wrap the selected text (the standard region snippet does this), the second indicates that the snippet text is simply inserted, and the third is used for the refactoring mechanism built into the studio and cannot be used in custom snippets.

    Literals are declared in Snippet, the value of which can be edited using the snippet, and the snippet code itself. Pay attention to how the literal is defined: its identifier, tooltip and default value are indicated. The literal identifier is then used in the body of the snippet code. To highlight the literal, it is between the two dollar signs "$". In addition to defining the snippet code, the Code tag also contains several attributes: Language, which indicates the target language of the snippet and can take the values ​​VB, CSharp, VJSharp, or XML; Delimiter, which allows you to redefine the dollar sign to another symbol; and Kind, which allows you to specify the scope of the snippet and takes the values ​​“method body”, “method decl”, “type decl”, file, or any. In our case, Kind is omitted, but it can be specified as "type decl".

    After creating the snippet file, you need to somehow embed it in the studio. To do this, use the standard “Code Snippets Manager” dialog in the Tools menu of the main menu. If you don’t have it there, then your enviroment is so configured and you can find the menu item in Tools \ Customize - Commands - Tools. You can make it simpler and call the dialog shortcut ctrl + k, ctrl + b. In the window you can specify which section to save the snippet in and click the import button to import your snippet into the studio. After that, you can type propv in the editor and press tab. You should add text like this:
    public int MyProperty {get {object o = ViewState ["MyProperty"]; return (o! = null)? (int) o: 0; } set {ViewState ["MyProperty"] = value; }} * This source code was highlighted with Source Code Highlighter .


    That's all. For completeness, I will give a link to the description of the snippet xml-file scheme:
    https: //msdn.microsoft.com/ru-ru/library/ms171418 (en-us, vs. 80) .aspx (unfortunately, because of the brackets in url, Habr incorrectly selects a hyperlink, copy it to the browser as is). There you can gain additional knowledge and learn how to make even more complex and useful snippets.

    PS: “there are no errors in the article!” - unfortunately it is impossible to say this, but I will be glad if you pay my attention to the error you found.

    Also popular now: