smithvoice.com
 Y'herd thisun? 

“Seen the funny and GREAT movie "the Big Dish?" Heading to Cali for a vaca? You have to visit the Goldstone array complex. Check this to see why”

from Visit to Goldstone by Smith

A couple useful VS Snippets

TaggedCSharp

Of late I work with folks from c backgrounds who fundamentally have the perception that VB syntax makes code "bad" and brackets and braces makes C# "better". Whatever, VB folks are far and away most often the bilinguals of the .Net universe so when in Rome.

Thing is that C#'s IDE just ain't as smart as VB's; due in large part to the VB "language" keywords not only being more self-documenting but also that they are hints to the IDE for what you want to get done, and as such snippets in VB are task oriented while the core snippets in C# are mostly there to give a dev an almost-vb keyword experience.

So while I'm in Rome, I'm really gettin' to be a fan of making snippets for VS's C# dumb editor. And just making minor alterations to existing ones make all the difference in the world to my workaday coding.

F'rinstance, coming from a decade of VB, "msgBox" is what my fingers keep typing whenever I want a dialog. It's so much faster and natural for me than "M..e..s..s..a..g..e..B..o..x. [tab]S..h..[tab]".

There is a C# snippet to save the typing, it's "mbox", yeah that's shorter, but my fingers still go right to the "g" instead of the "b". Thanks to how easy it is to make a snippet this is no longer a problem.

Drill your way into the C# snippet folder (by default it's in C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# ). Create a new text file, name it something obvious (I use "msgbox.snippet") aned paste in the following:

<?xml version="1.0" encoding="utf-8" ?>
 <CodeSnippet Format="1.0.0">
<Header>
<Title>c# msgbox</Title>
<Shortcut>msgbox</Shortcut>
<Description>Code snippet for MessageBox.Show</Description>
<Author>smith via MS</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>string</ID>
<ToolTip>String to display</ToolTip>
<Default></Default>
</Literal>
<Literal Editable="false">
<ID>SystemWindowsFormsMessageBox</ID>
<Function>SimpleTypeName(global::System.Windows.Forms.MessageBox)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[$SystemWindowsFormsMessageBox$.Show("$string$");$end$]]>
</Code>
</Snippet>
 </CodeSnippet>
</CodeSnippets>

Save the file and open up the C# IDE. Put your caret whereever and type "msg..." up will pop the intellisense with your msgbox item, [tab] and there you go.

All this is is the code from the "mbox" snippet file with a couple of minor alterations, such as the Shortcut element value being "msgbox" so that text will appear in the intellisense list, and a little tweak to put the quotes outside of the dynamic text area instead of having them be part of the dynamic text and instantly deleted when you start typing... which I go back and forth on because sometimes you just want to stick an x.ToString() or similar in there, I can bet that the mbox author flipped a coin on that too.

Here's another one that comes in handy: A tweaked class snippet.

80% of the time my classes get refactored into the same regions, "declarations", "public properties", "private property puts" (for internal business rule checking, keeping internal values raw and external representations formatted, etc), "public methods" and "private methods". Over time I just got used to creating a new class and zoning out while my fingers rattled those off... a snippet saves the nanoseconds.

I did the same as above but named the new file "rcsclass.snippet" and inside put this text:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0">
<Header>
<Title>rcsclass</Title>
<Shortcut>rcsclass</Shortcut>
<Description>Code snippet for class</Description>
<Author>smith adapted from MS</Author>
<SnippetTypes>
 <SnippetType>Expansion</SnippetType>
 <SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
 <Literal>
 <ID>name</ID>
 <ToolTip>Class name</ToolTip>
 <Default>MyClass</Default>
 </Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[class $name$
{
 $selected$$end$#region declarations
 
 #endregion
 
 #region public properties
 
 #endregion
 
 #region private property puts
 
 #endregion
 
 #region public methods
 
 #endregion
 
 #region private methods
 
 #endregion
 
}]]>
</Code>
</Snippet>
</CodeSnippet>
 </CodeSnippets>

Save and open VS, pick a spot and start typing "rcs...", pick the "rcsclass" from the list and tab. There's the new class with the regions.

Again it's just a minor alteration of the Microsoft class.snippet file. And that brings to mind the obvious question: 'why not just adapt the exiting ones?'. It's up to you and you can do that, to me it's for options and makes it easier for other devs to come over to my machines and use the core snippets if that's what they're more used to.

Hope it helps

 



home     who is smith    contact smith     rss feed π
Since 1997 a place for my stuff, and it if helps you too then all the better smithvoice.com