Friday, May 18, 2007

Code snippet

Code snippet is a nice feature Microsoft provided in Visual Studio 2005. But surprisingly there is still not a lot people know how to take advantage of it. Many people think it is no different from copying and pasting existing code. Well, there is difference. The code snippet is right at your mouse click while you have to search the code to find the one you want to copy over. Also don’t you have had those moments when you know .NET can do something, but you don’t know exact name of the class or method. Code snippet also can help you with that. This is just one of the many features Visual Studio has to help developers to become more efficient. You may find more information at http://msdn2.microsoft.com/en-us/library/ms165392(VS.80).aspx


How to use existing code snippet


1. In the code editor, place cursor at the place you want to put the code snippet and right click. Then click on “Insert Snippet” on the pop-up menu.



2. A pop-up menu with code snippet category will show up. You may choose a category, then choose the code snippet from the list. Here I select one of my favorite – the property



3. It will automatically insert the following code. I can then tab through the green highlighted fields to make any change. For example, when I change “myVar” to “customerName”, VS will automatically change all of them for me.


How to create your own code snippet and share with others


1. Create an xml file in the following format. Here I use a code snippet I created for my project. It is very straightforward. You name it with the .snippet file name extension. Save it to a folder you want to put all your code snippets.


<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>
Create FindByXXX in Mapping layer
</Title>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>ReturnType</ID>
<ToolTip>Replace with return type.</ToolTip>
<Default>DomainObjectBase</Default>
</Literal>
<Literal>
<ID>MethodName</ID>
<ToolTip>Replace with method name.</ToolTip>
<Default>MethodName</Default>
</Literal>
<Literal>
<ID>ParameterType</ID>
<ToolTip>Replace with parameter type.</ToolTip>
<Default>string</Default>
</Literal>
<Literal>
<ID>ParameterName</ID>
<ToolTip>Replace with parameter name.</ToolTip>
<Default>p</Default>
</Literal>
<Literal>
<ID>VariableName</ID>
<ToolTip>Replace with variable name.</ToolTip>
<Default>var</Default>
</Literal>
<Literal>
<ID>TableWrapperName</ID>
<ToolTip>Replace with table wrapper name.</ToolTip>
<Default>xxxDO</Default>
</Literal>
<Literal>
<ID>TableWrapperMethodName</ID>
<ToolTip>Replace with table wrapper method name.</ToolTip>
<Default>GetListAsDataReaderXXX</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
public $ReturnType$ $MethodName$($ParameterType$ $ParameterName$)
{
$ReturnType$ $VariableName$ = null;
$TableWrapperName$ tableWrapper = new $TableWrapperName$(base._connectionName);
if (this.ActiveUnitOfWork != null)
{
tableWrapper.ActiveUnitOfWork = this.ActiveUnitOfWork;
}
System.Data.IDataReader dr = null;

try
{
dr = tableWrapper.$TableWrapperMethodName$($ParameterName$);
if (dr.Read())
{
$VariableName$ = new $ReturnType$();
$VariableName$ = ($ReturnType$)base.Load($VariableName$, (IDataRecord)dr, dr.GetSchemaTable());
}
}
finally
{
if (dr != null && !dr.IsClosed) dr.Close();
}

return $VariableName$;
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


2. Go to “Tool”->”Code Snippets Manager”. Click on “Add…” to add your folder. Now it is ready for you to use.





There are a lot of useful code snippets on the Internet, you can download and add to your collection.


By the way, I just learned a little trick about code copying. You can drag and drop any code block to the Toolbox, then drag and drop it back to anywhere you want. You can even give it a name. If you don’t know this trick, you may want to check it out.


No comments:

Post a Comment