Using the ClassName() snippet function – an operator declaration snippet example


A previous tip of the week mentioned how to use the SimpleTypeName() snippet function.  Here’s how to use the SimpleTypeName() function alongside the ClassName() function.  The ClassName() function will automatically substitute the name of the outer class. 

For example, if you had

class Class1

{

}

You could make a snippet that used the ClassName function to automatically generate

public static Class1 operator +(Class1 one, Class1 two)

{

   throw new System.NotImplementedException();

}

Here’s the snippet to make the operator declaration used above:

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

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

  <CodeSnippet Format="1.0.0">

    <Header>

<Title>Addition operator</Title>

<Shortcut>addop</Shortcut>

<Description>Code snippet to create an addition operator for a class</Description>

<Author>Microsoft Corporation</Author>

<SnippetTypes>

<SnippetType>Expansion</SnippetType>

</SnippetTypes>

</Header>

<Snippet>

<Declarations>

<Literal Editable="false">

<ID>EnclosingClass</ID>

<Function>ClassName()</Function>

</Literal>

<Literal Editable="false">

<ID>NotImplementedException</ID>

<Function>SimpleTypeName(global::System.NotImplementedException)</Function>

</Literal>

</Declarations>

<Code Language="csharp"><![CDATA[

public static $EnclosingClass$ operator +($EnclosingClass$ first, $EnclosingClass$ second)

{

throw new $NotImplementedException$("Method not yet implemented");

}]]>

      </Code>

</Snippet>

</CodeSnippet>

</CodeSnippets>

Tags: VSTips
Suggest a Tip!

Comments (3)

  1. Re: "The previous tip of the week mentioned how to use the SimpleTypeName() snippet function."

    Where? Can you put a link to the relevant tip of the week.

    Thank you!!

  2. saraford says:

    My bad, i’ve updated the blog entry with the appropriate link.  The link is

    http://blogs.msdn.com/saraford/archive/2006/05/24/606577.aspx

  3. Jim says:

    Do you know of anywhere that describes how we can create our own functions for use within the snippets?