The “DRY” principle describes an element of programming efficiency: Don’t Repeat Yourself.
One of the tools available to help you with this are Code Snippets. ArcGIS 9.3 products ship with hundreds of these snippets you can quickly and easily find and use, but how can you create them, add to this repository, and share them with others?
Back on July 14th, Don Kemlage from the ArcGIS SDK team posted a video here on the blog to describe what snippets are, how to get to the ones we provide with ArcGIS, and how to use the Snippet Finder. At about 10:50 minutes into that video, Don mentions briefly that developers can make their own snippets too. In this post here, we wanted to dig a little more into that, and provide some tips on how to do it in more detail.
Frameworks such as Visual Studio and Eclipse provide several ready-to-use Code Snippets. You can even add your own snippets into the library, organizing them into a directory structure by the subject or task they accomplish. Creating your own code snippets helps you and your team members have snippets which are greatly customized for your development.
Code snippets are defined in XML format and are stored with the *.snippet file extension. You can use your favorite XML or text editor or Visual studio or Eclipse to create and maintain them.
Creating Code Snippets in Visual Studio 2005/2008
MSDN has a nice concise "How To" article on creating code snippets.
In short, using Visual Studio 2005/2008 start with the File > New > File > XML File menu item. That allows you to create the new file and save it in place, ready for editing. Then the next step is to plug in the required information to make it an individual code snippet. Let’s show you an example code snippet, then further below we’ll explore the tags one by one.
Let’s walk through it, top to bottom.
CodeSnippets
The <CodeSnippets> element is the root element of the code snippet XML schema and has xmlns attribute value.
CodeSnippet
The <CodeSnippet> element is used to create an individual code snippet and this is where all declarations on your snippet must reside.
Header, Title, Shortcut, Description, Author, SnippetTypes
The <CodeSnippet> tag has a <Header> section that contains information needed by the IDE in these additional tags. Visual Studio gives you a description of what each one does when you hover your mouse on these tags.
Snippet
The <Snippet> tag is a little bit more complex. It contains both the <References> and <Code> tags.
References
The <References> tag is used by the IDE to add references to the project when the snippet is inserted. In this example, the code snippet adds a reference to ESRI.ArcGIS.esriSystem.dll when the snippet is inserted. Note that Visual C# code snippets do not support the References section, so a reference to ESRI.ArcGIS.esriSystem.dll must be added to the project manually.
Code
The <Code> tag contains the specific code to insert as <Code Language="VB"> that says that the language of the code snippet is Visual Basic. All snippet code must be placed between <![CDATA[ and ]]> brackets. You can also mention (in comments) what ArcGIS Products and which versions (9.2, 9.3) can use this snippet and any other namespaces that you wish the user imports before adding.
Ok, so after doing this a couple of times they are pretty simple to create. And whether you write them using Visual Studio or a plain text editor, give each one a *.snippet file extension and use the Tools > Code Snippets Manager menu item to ensure the IDE knows where these folders are.
Code Snippets not only provide a wide variety of productivity-enhancing solutions and their real power lies in their extensibility. Give them a shot.
Contributed by Sirisha Karamchedu from ESRI Educational Services