I'm attempting to supplement the help features for my code by providing other developers with code snippets. These produce skeletons of code which demonstrate how to use/call my classes or methods.
I've created a .snippet file and have placed it in the "%Visual Studio Folder%\Code Snippets\Visual Basic\My Snippets" folder. I've used the Code Snippets Manager and ensured that it included this folder so that I can access the snippets.
Everything works well when I have 1 CodeSnippet tag within the root CodeSnippets tag....
When I add more than one CodeSnippet tag to the file (each with their own title, and their own code example) I'm experiencing something strange.
The first CodeSnippet I've added contains code for adding something to my system, the second contains code for editing something in my system, and the third deleting something from the system.
When I use the code snippet by right clicking and selecting "Insert Code Snippet", only the first code snippet in the file shows up as an option. When I select it, the code in the first CodeSnippet is inserted....but so is the code within the other CodeSnippet tags.
Do you have to have a separate XML .snippet file for each code snippet you want to make available?
After reading through MSDN about creating Code Snippets I was under the impression that this could all be done within one file.
It seems that I'm not understanding something very basic here and would love to find the answer but apparently Code Snippets are under used so finding the answer has proven to be a little trickier than I first thought it would be.
Thanks,
-Frinny
Do you have to have a separate XML .snippet file for each code snippet you want to make available? After reading through MSDN about creating Code Snippets I was under the impression that this could all be done within one file.
I create and use snippets all the time. I wanted to do exactly what you intend. But, I have never gotten multiple snippets in one file to work. I also was under the impression you could do this. I even found an article, based on the beta version of Visual Studio, that demonstrated how to do this, but it didn't work. Moreover, the documentation states:
CodeSnippet Optional element. - Parent element for all code snippet data. There may be zero or more CodeSnippet elements in a CodeSnippets element.
I eventually gave up and created separate files. I have not tried multiple snippets in one file with VS2k8 yet.
I cannot get VB to support multiple snippets in one file using VS2k8. I used the following snippet as a test case. Snippet taken from http://msdn.microsoft.com/en-us/library/ms165394.aspx. Only the first CodeSnippet
is read/loaded/used. I also counted all CodeSnippet
elements in all snippet files (1143) and no file has more than one. Moreover, this snippet duplicates the behavior you noted in your question. Using the snippet inserts both message box statements.
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>
SECOND Snippet
</Title>
</Header>
<Snippet>
<Code Language="VB">
<![CDATA[MsgBox("SECOND SNIPPET")]]>
</Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>
FIRST Snippet
</Title>
</Header>
<Snippet>
<Code Language="VB">
<![CDATA[MsgBox("FIRST SNIPPET")]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
I got to thinking that this has to be a bug that slipped through because Microsoft never put multiple snippets in the provided snippet files that come with Visual Studio, or VB.NET doesn't support multiple snippets due to difficulties integrating with the enhanced statement completion capabilities of the editor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With