Flylib.com

Books Software

 
 
 

Chapter 2. The Development Environment


Chapter 2. The Development Environment

Introduction

Recipe 2.1.  Discovering and Using a Code Snippet

Recipe 2.2.  Creating a New Snippet

Recipe 2.3.  Sharing Snippets

Recipe 2.4.  Adding Snippet Files to Visual Studio

Recipe 2.5.  Getting an Application's Version Number

Recipe 2.6.  Letting Visual Studio Automatically Update an Application's Version Number

Recipe 2.7.  Setting the Startup Form for an Application

Recipe 2.8.  Setting the Startup to a Sub Main Procedure

Recipe 2.9.  Getting an Application's Command Line

Recipe 2.10.  Testing an Application's Command Line

Recipe 2.11.  Obfuscating an Application

Recipe 2.12.  Determining if an Application Is Running in the Visual Studio Environment

Recipe 2.13.  Accessing Environment Variables

Recipe 2.14.  Accessing the Registry

Recipe 2.15.  Getting System Information

Recipe 2.16.  Getting the User's Name



Introduction

Did you know that the Visual Basic 2005 compiler is available to you free of charge? You can download the .NET Framework with all included compilers directly from Microsoft's web site, and start using it immediately to develop and distribute your own .NET applications, all without shelling out a single penny.

Well, there are a few caveats. The main one is that you will have to use a tool such as Notepad to write all of your source code. And you will need to hand-type the statements that start the compilation process through the Windows Command Prompt. But other than that, it's a piece of cake. And it's still free.

If you're not that bold, you should probably fork over a little cash to obtain Visual Studio, the programming environment of choice for .NET application development. Although it's not free, you definitely get what you pay for.(Actually, Visual Basic 2005 Express Edition is free, so you get even more than you pay for.) Visual Studio is stuffed with features and support tools and visual designers and behind-the-scenes automatic code generation wizards, all of which let you concentrate on developing great code without having to worry about the picky details of setting up the compiler and deployment options.

This chapter discusses some of the snazzy features included with Visual Studio 2005. As with all the chapters in this book, we have concentrated on Visual Studio 2005 Professional Edition. However, most, if not all, recipes in this book should work with any edition of Visual Studio.



Recipe 2.1. Discovering and Using a Code Snippet

Problem

You know that Visual Studio came with a bunch of prewritten "snippets" that you can use in your applications, but you don't know where to find them in the vast Visual Studio menu system.

Solution

Code snippets are among the IntelliSense features included with Visual Studio. To find and insert a snippet, use the different snippet- related menus and keyboard sequences.

Discussion

To insert a code snippet into your source code, right-click at the desired location with the mouse, choose Insert Snippet from the shortcut menu (Figure 2-1), and navigate to the snippet you want to use.

Figure 2-1. Inserting a snippet with the mouse

An even faster method is to type a question mark ( ?) anywhere in the source code and then press the Tab key. The more formal location of this same command within the Visual Studio menu system is at Edit IntelliSense Insert Snippet. If you are in any way mouse-phobic when developing source code, you can use the default Visual Basic keyboard shortcut of Control-K followed by Control-X to get to the snippet picker.

Using any of these methods to access snippets presents the top-level set of snippet folders, as shown in Figure 2-2.

Figure 2-2. Primary snippet categories

To negotiate the hierarchy, use the mouse or arrow keys to select a folder or item in the pop-up list, or type a partial list name followed by the Enter key. Selecting a snip-pet folder updates the list with the items and subfolders in that selected folder. For example, in Figure 2-2, selecting "Math" with the mouse or typing "Math" from the keyboard followed by the Enter key, will open the "Math" snippet folder and display any folders or items contained within that folder. Selecting an item inserts the chosen snippet.

Each snippet contains a useful block of prewritten code, but many also include some intelligence. Some snippets include "fill in the blank" templates that provide areas for you to supply your custom values. For instance, the Data Typesdefined by Visual Basic Convert a Number to a Hexadecimal String snippet includes a field for the source value, moving the insertion point to that field immediately upon pasting the snippet in the code:

Dim hexString As String = Hex(

48

)

Some snippets place multiple lines of source code in the code editor, sometimes with multiple replacement fields. The Common Code Patterns Types Define a Structure snippet defines this multiline structure:

Structure

MyStructure

Public

ValueOne

As

Integer

Public

ValueTwo

As

Boolean

End Structure

Some snippets add code to various places in your source-code file and may make other updates to your project. The Security Encrypt a String snippet not only adds code to the active procedure but also adds Imports statements to the top of the source-code file if references to the namespaces it uses are not already there.

Snippets are somewhat location-dependent. Most are written to be used inside a sub-routine, function, or property accessor, while a few are designed for placement out-side of routines or classes. If you insert a snippet at the top of a source-code file, outside of any class context, it will be riddled with errors.

Snippets are actually specially formatted XML files, with attributes containing the special insertion rules for each snippet.

See Also

Recipes 2.2, 2.3, and 2.4 provide additional information on code snippets.