Welcome to MSDN Blogs Sign in | Join | Help

Sara Ford's Weblog

My adventures embracing open source on CodePlex and at Microsoft

News

    • Did you know... All author proceeds go directly to sending Hurricane Katrina survivors to college.

      Microsoft Visual Studio Tips book

      Recent Entries

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:
Suggest a Tip!

Posted: Wednesday, May 31, 2006 4:22 PM by saraford

Comments

Charles Vereker said:

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!!
# June 5, 2006 6:53 AM

saraford said:

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
# June 5, 2006 12:57 PM

Jim said:

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

# July 17, 2006 10:45 AM
New Comments to this post are disabled
Page view tracker