Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017 propfull snippet but with .this

Hello i use the propfull snippet in VS a lot but i always have to add .this every time since the shortcut does not include it. Any idea where i can find a snippet that does that(for VS 2017 without resharper). I searched but the best i could find was a resharper version and that does not work for me. Sorry if this was posted somewhere but i could not find an answer.

Examples:

Normal propfull:

    private int myVar;

    public int MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }

Desired propfull:

    private int myVar;

    public int MyProperty
    {
        get { return this.myVar; }
        set { this.myVar = value; }
    }
like image 712
Stoyan Grigorov Avatar asked Oct 22 '25 00:10

Stoyan Grigorov


1 Answers

You can do this by using the snippet manager that you can find in Visual Studio, under Tools -> Code snippet manager...

enter image description here

I took for example the propfull implementation of the Microsoft snippet, and just changed it slightly so that it will add this in front of the properties, like so:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propfull with this</Title>
      <Shortcut>thispropfull</Shortcut>
      <Description>Code snippet for property and backing field</Description>
      <Author>Icepickle</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 this.$field$;}
    set { this.$field$ = value;}
  }
  $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

You can save this file as your own snippet (extension .snippet), and then use the import button to add your own snippet. Then you can write the shortcut thispropfull and it would create the desired snippet

enter image description here

like image 133
Icepickle Avatar answered Oct 23 '25 15:10

Icepickle