Re: How to use Code Snippets
Pretty straightforward, upon reading your tutorial I have come up with this for adding singleton to my form objects.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Singleton</Title>
<Shortcut>single</Shortcut>
<Description>Code snippet for Singleton</Description>
<Author>Rodelio M. Rodriguez</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>form</ID>
<ToolTip>The form</ToolTip>
<Default>Form1</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[ private static $form$ instance = null;
public static $form$ Instance()
{
if (instance == null)
{
instance = new $form$();
}
return instance;
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
The thing is, is there a way that would automatically fill-up the Literal form using the form name where the snippet is generated? I don't mind filling up the Literal but it would be better if it can take the name of the form where it is generated.
Re: How to use Code Snippets
Hey there,
I am glad that it was helpful to you.
I see what you are trying to do, but I am not aware of being able to do it. I have just done a quick google, but nothing immediately jumped out.
I will keep looking though, and if I find anything I will post back.
Gary
Re: How to use Code Snippets
Hey,
I have done some digging aroung, and I think what you are asking for is possible using Code Snippet Functions.
Have a look at the below, is that what you are after?
Code:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Singleton</Title>
<Shortcut>single</Shortcut>
<Description>Code snippet for Singleton</Description>
<Author>Rodelio M. Rodriguez</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>classname</ID>
<ToolTip>The form</ToolTip>
<Function>ClassName()</Function>
<Default>ClassNamePlaceholder</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[ private static $classname$ instance = null;
public static $classname$ Instance()
{
if (instance == null)
{
instance = new $classname$();
}
return instance;
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Hope that helps!!!
Gary
Re: How to use Code Snippets
Wow, that's it! Thank you so much for taking time to look for it, it really is handy! Can't thank you enough but couldn't give you another rep.
Re: How to use Code Snippets
Ha ha, don't worry about it, I was happy to help, and it was something that I didn't know was possible either, so it was good to find it.
Gary
Re: How to use Code Snippets
Hello,
For additional useful Code Snippets, you might want to take a look at the following:
http://blogs.msdn.com/b/dohollan/arc...1-release.aspx
Very interesting to see that all of these Code Snippets are StyleCop compliant.
Gary
Re: How to use Code Snippets
I'd like to add that there is an excellent code snippet editor available as an extension to VS2010. It's very easy to create your own snippets right within Visual Studio itself, no need to edit the XML manually :) I can't remember the exact name, but searching for 'code snippet editor' in the Extension Manager should be enough, I could only find one when I was looking for it.
Re: How to use Code Snippets
Hello Nick,
Yes, this is very true. The one that I use is this one:
http://visualstudiogallery.msdn.micr...B-FAEE50F68392
This article was written a few years ago now, before Snippet Editors had made much of an impact, and it was necessary to do some things manually. It is still good to know about "how" things are done, but the available editors makes it much easier and quicker to get up and running with snippets.
Gary