So here's yet another entry that's not explicitly about speech, but it IS a feature that could be useful in developing on MSS 2007, so I'll post it here anyway. One of the features that came with Visual Studio 2005 (unfortunately, not supported in C++) was code snippets. Code snippets are supposed to make development faster and easier by automating the creation of "boilerplate" code, and basically work as an extension of intellisense. While it might seem at first that these are of dubious usefulness (I know how to complete a for loop thank you very much), creating your own shippets can actually be very useful.
For example, I had a C# project where I wanted to insert the following block of code into a class:
string m_S;
bool m_SCanModify=false;
public string S{ get { return m_s; } set{ if(m_SCanModify==true) {m_s=value;} } }
public bool SCanModify{ get{return m_sCanModify;} set {m_sCanModify=value;}}
This code, which contains properties for a private member that can be flagged as read-only, had to be repeated for a large number of members in my application. Not being able to come up with a way around repeating the code over and over, I originally was going to return to the old "Find+replace" method that I'd used in the past. Instead I remembered what I'd read about code snippets, and decided to check out what they could do, since I'd heard that in addition to the preloaded code snippets, you could create your own shippets and integrate them into the IDE.
It turns out that a code snippet is defined in an XML file with a .snippet extension. Creating one is simply a matter of writing the XML, and then importing it into the IDE by clicking Tools, Code Snippet Manager, and the Import button, and then browsing for the .snippet file. For the block of code shown above, here's the corresponding snippet file:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Permission Member Snippet</Title>
<Description>Snippet for member with permission/properties</Description>
<Author>Chris Bardon</Author>
<Shortcut>icePermission</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>ObjName</ID>
<ToolTip>Object to be set</ToolTip>
<Default>Object name</Default>
</Literal>
<Literal>
<ID>ObjType</ID>
<ToolTip>Object Type</ToolTip>
<Default>string</Default>
</Literal>
<Literal>
<ID>Default</ID>
<ToolTip>Default Value</ToolTip>
<Default>""</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<!--Code Snippet that creates a block with a member variable, a corresponding flag-->
<!--for readonly access, and the properties to access them-->
<![CDATA[
$ObjType$ m_$ObjName$=$Default$;
bool m_$ObjName$CanModify=true;
public $ObjType$ $ObjName$ { get { return m_$ObjName$; } set { if (m_$ObjName$CanModify == true) { m_$ObjName$ = value; } } }
public bool $ObjName$CanModify { get { return m_$ObjName$CanModify; } set { m_$ObjName$CanModify = value; } }
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Of course, some of this XML merits explanation. The header contains some basic fields describing the snippet that show up in the snippet manager, as well as a <shortcut> element. This element is important, as it contains the string you can type into the editor to insert the snippet. The string will show up in the intellisense dropdown, and you can complete it by hitting TAB twice. Once imported, you can also right click in the IDE and select "insert snippet", or choose "insert snippet" from the tools menu, but those both require using a mouse, and if you're writing code, your hands are already on the keyboard. This is why shortcuts are useful. This snippet also contains a declarrations section, which defines substitution text for the snippet, and represented by wrapping the ID in $ characters in the code. When the snippet is inserted, the fields to be modified are highlighted in green, and you can TAB between them. The IDE will also change ALL occurrances of a field once the first one is set, so in my case, defining $ObjType$ as string will change both instances. The documentation in MSDN for code snippets is actually pretty good, and there's a good section to explain what the XML schema for the snippet file is.
I should also note that this is really a first attempt at using Code Snippets, and I've probably broken a few design rules in the process, but I think it's a decent first stab at things. Want to take a shot at using code snippets without writing your own? The next time you're writing C#, when you type "for", and see it show up in the intellisense dropdown, hit TAB twice, and see what happens.