1. Home
  2. Computing & Technology
  3. Visual Basic

How To Use (And Create) Code Snippets - Part I

Randomizing Drinking with Code Snippets

By Dan Mabbutt, About.com

Jul 8 2007

In the "Drinking Binge" project the takeADrink_Click subroutine created a random percentage of a drink using the code:

Randomize()
SaturdayNight.Drink(Rnd())

To be totally honest, I couldn't remember the exact syntax for random numbers and I had to look up some example code before I wrote it. It took me a few minutes of searching on the web to find some example code and another few minutes to modify it so it would work for me. Let's see how that process works with Code Snippets.

I created some blank lines in my code where the Code Snippet will go and entered a "?" and then pressed the Tab key. This brings up the snippet inserter. The snippet inserter is what you will see using the other methods for using Code Snippets. (We'll look at five ways to insert Code Snippets next.) In this case, I selected the Math library - one of the libraries that comes "in the box" with VB.NET.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

In the Math library, I found this these Microsoft-supplied (Visual Basic Express) snippets:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Clicking the "Random Number" entry gives me my sample code. The green, highlighted areas are replacement fields with their own tooltip help.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

These fields take full advantage of Intellisense so that values can be suggested just like other Intellisense objects. Highlight a field and select Ctrl-Space to see the Intellisense suggested choices. In this case, the suggestions aren't very helpful, but in other cases, they can save a lot of time.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

For the Drinking Binge program, I needed a decimal number between 0 and 1. The code as supplied gives an integer between any two specified integers. But a few simple modifications quickly fixes it up.

Dim generator As New Random
Dim randomValue As Decimal
randomValue = generator.Next(0, 100) / 100
SaturdayNight.Drink(randomValue)

So ... what's the box score on "the old way" versus Code Snippets?

The code I created myself is simpler. It's not necessary to create a Random object, but the Code Snippet code is more robust and will handle virtually any random number task. But the real clincher is that it took me much less time to insert and modify the Code Snippet ... maybe only ten percent as much time. And that's what Code Snippets mainly do -- they save time.

  1. right-click and choose "Insert Snippet ..."
  2. type ? followed by the Tab key
  3. select Edit > Intellisense > Insert Snippet ...
  4. enter the Snippet shortcut and press the Tab key
  5. type Ctrl-K followed by Ctrl-X
  6. drag and drop the .snippet file into your code

You can also use a combination of two methods above. Enter a few letters in the code editor followed by ? and the Tab. Intellisense will display a list of shortcuts to choose from.

(Some articles say that you can insert a "surround with" Code Snippet. Alas, they usually don't mention that VB doesn't support it.)

You can also use a combination of two methods above. Enter a few letters in the code editor followed by ? and the Tab. Intellisense will display a list of shortcuts to choose from.

The last method, "drag and drop the .snippet file into your code," brings up a whole new subject. What is a ".snippet file" and where are they?

A .snippet file is the programming unit for Code Snippets. It's an XML file that you can find in ...

C:\Program Files\Microsoft Visual Studio 8\<language>\1033\<category>

... for VB.NET Pro and in ...

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VBExpress\Snippets\1033\<category>

... for VB.NET Express Edition. If you code your own, you can put it anywhere ... if you're willing to put up with some trouble ... or you can put it in ...

My Documents\Visual Studio 2005\Code Snippets\Visual Basic\My Code Snippets

... with a lot fewer problems. I'll have more about this when we cover coding your own snippets.

Although these are XML files, when you drag and drop them into VB code using Visual Studio or VB Express, they're converted into code in exactly the same way as they would be if you used the Snippet Inserter dialog.

<a href="http://visualbasic.about.com/od/usingvbnet/a/snippets02.htm">Click here to go to Part II!</a> In Part II, you'll learn about the Code Snippet Manager and consider the question of code snippet editors and how to code your own code snippets.

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. How To Use (And Create) Code Snippets - Part I

©2008 About.com, a part of The New York Times Company.

All rights reserved.