Snipping databound properties

6 08 2010

Long time no see Internet! Hope your summer has been as good as mine and that you’re at least as caffeinated and motivated as I am.

1280965856882Snippets are almost as tasty as bacon, and less calories

I just learned about snippets and wanted to share this wonderful coding aid with everyone. Let’s Go!

What are snippets? They sound delicious!

Snippets are a form of coding aid in Visual Studio. They’re XML based and if you’ve every typed “prop”, “if” or “propd” and then double-tabbed, you’ve already used them. Snippets give us a way to create big amounts of pre-defined code with a mere shortcut. This is great for reusable parts of programs.

Instead of going through every part of a snippet, I’m just going to show one that I made.

Let’s have a look before we begin (720p resolution please):

Let’s have it then..


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

  <CodeSnippet Format="1.0.0"> 
    <Header> 
      <Title> 
        propb 
      </Title> 
      <Shortcut> 
        propb 
      </Shortcut> 
    </Header> 

This initial area isn’t much fun, very copy+pastable. The two things you might want to change here are the title and shortcut. The shortcut represents the commando you would use in VS2010 to create the codeblock.


    <Snippet> 
      <Imports> 
        <Import> 
          <Namespace>System.ComponentModel</Namespace> 
        </Import> 
      </Imports> 

Above in the <imports> tag, I’ve decided to import the namespace System.ComponentModel every time I insert this codeblock since it’s dependant on the namespace.


      <Declarations> 
        <Literal> 
          <ID>Type</ID> 
          <ToolTip>Replace with an object type.</ToolTip> 
          <Default>string</Default> 
        </Literal> 
        <Literal> 
          <ID>Name</ID> 
          <ToolTip>Replace with initial-uppercase Name.</ToolTip> 
          <Default>MyObject</Default> 
        </Literal> 
        <Literal> 
          <ID>name</ID> 
          <ToolTip>Replace with initial lowercase name.</ToolTip> 
          <Default>myObject</Default> 
        </Literal> 
      </Declarations> 

Here I declare three literals. These are used in the code below to allow the user to customize the code. Using one or two literals really makes for powerful code generation.


      <Code Language="CSharp"> 
        <![CDATA[ 
        private $Type$ $name$; 
        public $Type$ $Name$ 
        { 
            get 
            { 
                return $name$; 
            } 
            set 
            { 
                $name$ = value; 
                OnPropertyChanged("$Name$"); 
            } 
        } 
        ]]> 
      </Code> 
    </Snippet> 
  </CodeSnippet> 
</CodeSnippets>

Everything inside “<![CDATA[ “ is just regular C# code with my literals inside of “$” signs. This snippet will generate a databindable property for me in no-time.

Zowie! How do I use this?

You’ll have to create an xml document in VS2010 and save it as someName.snippet. Then either use Visual Studio’s Snippet Manager under “Tools” to import them or just add your snippet to “My Snippets” folder.

Probably under “C:\Users\your name here\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets”

then you should be ready to go. Easy as pie!

Would you like to know more? Microsoft has a good guide on snippets at http://msdn.microsoft.com/en-us/library/ms165392%28VS.80%29.aspx

hot_apple_pie

So hungry…

Advertisements

Actions

Information

One response

6 08 2010
Keir Davis

You should try our Code Barrel product. It integrates directly with Visual Studio and has many more features than the built-in manager. http://www.codebarrel.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: