Search code examples
c#visual-studiopropertiescode-snippets

Creating Custom Code Snippet in VS2013 for C#


Creating Custom Code Snippet doesn't helped much to me. My question is specific to my requirement.

I want to write a custom snippet for my Property. The situation is normally when we write prop and double tab we will get the output

public int MyProperty { get; set; }

and when we write propfull we get

private int myVar;

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

as soon as we change the variable name it automatically reflects everywhere

Now I want to write my own snippet like this

public int MyProperty
{
    get
    {
        return GetValue(() => MyProperty);
    }
    set
    {
        SetValue(() => MyProperty, value);
    }
}

I have got Creating a Code Snippet from MSDN

This is what I have tried

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propmy</Title>
        </Header>
        <Snippet>
            <Code Language="csharp"><![CDATA[public int MyProperty
        {
                get { return GetValue(() => MyProperty); }
                set { SetValue(() => MyProperty , value); }
        }
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

But when I write propmy in VS IDE nothing show up in the list and it truns to prop in the first tab and on the second tab it create the property like normal one. I don't know how to proceed?


Solution

  • You can try to add

    <Shortcut>propmy</Shortcut>
    

    in the header section of the your XML. I am sure this will do the trick

    Edit:

    I have created the complete XML for you. Just copy paste and it will gonna help you.

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>propmy</Title>
                <Shortcut>propmy</Shortcut>
                <Description>Automatically implemented property</Description>
                <Author>BugFree</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>
                </Declarations>
                <Code Language="csharp"><![CDATA[public $type$ $property$
            {
                    get { return GetValue(() => $property$); }
                    set { SetValue(() => $property$ , value); }
            }
    $end$]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>