Section 2.5. Use Code Snippets to Save Typing


2.5. Use Code Snippets to Save Typing

There are a number of patterns that you use all the time (for example, for statements).Visual Studio 2005 makes it easy to insert these in your code.

2.5.1. How do I do that?

For example, returning to the previous lab, say you want to add some conditional code to actually test if the dog is overweight. Begin by highlighting the code within the TestDogOverweight method, and then right-click it. Choose Surround With: and the Surround With menu pops up, as shown in Figure 2-17.

Figure 2-17. The Surround With menu with the if statement highlighted


Choose the if statement (be careful not to choose #if), and an if block is created around your block of code. The if condition is initialized true and is highlighted for you to type in new code. IntelliSense helps you complete the code, as shown in Figure 2-18.

Figure 2-18. Filling in the if statement


2.5.2. What just happened?

With a couple of keystrokes, you were able to surround a block of code with a well-formed if statement. The variables used in the if statement were offered by IntelliSense, enabling you to make sure you used the correct spelling and capitalization.

2.5.3. What about...

...using expansions without surrounding existing code?

Rather than using expansion surround, just use the code expansion snippet. The expansions in IntelliSense allow you to quickly create any of a wide variety of coding structures. For example, if you just type the word foreach followed by a tab, a foreach loop is created with each editable field highlighted in yellow. You can tab through the fields with the Tab key, and when you are done, pressing Enter or Esc will return the editor to normal.

2.5.4. Where can I learn more?

For more information on creating and using code snippets, keep an eye on GODotnet.com. Over time users will be creating code snippets that you'll be able to import into Visual Studio 2005 to expand your repertoire of reusable code.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net