Print | Contents | Close |

Productivity Enhancements in Visual Studio 2005

Learning objective

After completing this topic, you should be able to identify various developer-productivity enhancements in Visual Studio 2005.

1. Productivity enhancements

Visual Studio 2005 includes developer productivity enhancements that assist with coding optimization, form design, application testing, and application management.

New features in Visual Studio 2005 make it easier to

edit code
Visual Studio 2005 introduces color-coded, vertical revision lines, which make editing code easier. No lines mark code that already exists when you first open a file. A yellow line identifies new, unsaved code, and a green line identifies code you've typed and saved during the current session.

You can personalize source code formatting by modifying braces, spacing, indentation, wrapping, and alignment. Alternatively, the IDE can format all or some code for you.

When you use the Find and Replace window to replace code items, Visual Studio 2005 now searches hidden text by default.
obtain and use code snippets
Visual Studio 2005 provides over 500 code snippets, which provide useful code templates for performing common tasks. It also provides the Code Snippets Manager, which makes it easy to locate code snippets and to insert them in your code.

Once you've inserted a code snippet, Visual Studio 2005 highlights areas of the snippet that require customization. If you hover your cursor over a highlighted area, a tool tip displays further information about what's required. You navigate between highlighted areas using the Tab key.

Updating a single variable in a code snippet automatically updates all instances of the variable in the snippet.
test applications
In C# 2005, the Object Test Bench feature enables object-level testing. Using this feature, you can create class instances, invoke methods, and view results in a test environment. You can call static methods without instantiating them, using the Invoke Static Method menu option.

Visual Studio 2005 also supports Design-Time Expression Evaluation (DTEE), which lets you execute and test pieces of code – rather than a full application – at design time.
manage classes and types
Visual Studio 2005 uses designers, which are windows that graphically display coding objects.

The Class Designer creates a graphic of classes and types. When you change the graphic, the associated code reflects the change. Similarly, if you add a class to the code, the graphic will update in order to include it.

You access the Class Designer via the Solution Explorer, which enables you to navigate application components and resources using a hierarchical directory structure.
design forms
In forms, Visual Studio 2005 includes snaplines – dynamic vertical and horizontal guides that help you align new controls with those already on a form.

When you extract an interface, an Extract Interface form opens and displays the new interface name, generated name, and filename, as well as a list of public members you can choose to add to the interface.

C# 2005 includes many new refactoring capabilities, which automate common tasks. These can help improve and simplify complex code structures.

Supplement

Selecting the link title opens the resource in a new browser window.

Launch window

View the new refactoring capabilities in C# 2005.

Using the new refactoring capabilities you can reorder parameters, extract methods and properties, and create overloads.

You're able to reverse conditionals, simplify expressions, and encapsulate fields. Initializations can be moved to, or split from, declarations, and declarations moved near references.

You can use the Surrounds With, Introduce Local and Introduce Constant capabilities, as well as Incline Temp and Replace Temp with Constant. You can also split temporary variables.

To access refactoring options, you can right-click the code to open a shortcut menu and then select the Refactor option from the toolbar.

struct Module1 
{

static void Main(string[] args)
{
//typeName will store the type of variable
Type typeName;
//assemblyName will store the actual information of the assembly
Assembly assemblyName;
//Evid will store the evidence
Evidence Evid;
IEnumerator enumerator;

ObjectTypeName = Type.GetType("System.Object");
assemblyName = Assembly.GetAssembly(typeName);
Evid = assemblyName.Evidence;
enumerator = Evid.GetEnumerator();
Console.WriteLine();
//Cast Evid count to a string for output.
Console.WriteLine("Total Evidence Enumerator members : " + (Evid.Count).ToString);
Console.WriteLine();
enumerator.MoveNext();
//output current index contents of the enumerator

Console.WriteLine(enumerator.Current);
}
}

To change the name of the variable typesName, you select the Rename option from the Refactor menu.

Imports System.Reflection

In the Rename dialog box you specify the variable's new name and click OK.

After refactoring, the variable's name is automatically changed throughout the code.

class Program
{

static void Main(string[] args)
{
//typeName will store the type of variable
Type ObjectTypeName;
//assemblyName will store the actual information of the assembly
Assembly assemblyName;
//Evid will store the evidence
Evidence Evid;
IEnumerator enumerator;

ObjectTypeName = Type.GetType("System.Object");
assemblyName = Assembly.GetAssembly(ObjectTypeName);
Evid = assemblyName.Evidence;
enumerator = Evid.GetEnumerator();
Console.WriteLine();
//Cast Evid count to a string for output.
Console.WriteLine("Total Evidence Enumerator members : " + (Evid.Count).ToString);
Console.WriteLine();
enumerator.MoveNext();
//output current index contents of the enumerator

Console.WriteLine(enumerator.Current);
}
}

Suppose you want to extract a method, another tool of refactoring. Here you want to take a block that will print out the count property of the Evid Evidence object and the contents of whatever type of object will reside in enumerator (after moving it to the next node) and turn it into its own separate method.

class Program
{

void Main()
{
//typeName will store the type of variable
Type typeName;
//Evid will store the evidence
Evidence Evid;
IEnumerator[] enumerator;
//assemblyName will store the actual information of the assembly
Assembly assemblyName;
assemblyName = Assembly.GetAssembly(typeName);
Evid = assemblyName.Evidence();
enumerator = Evid.GetEnumerator();
Console.WriteLine();
//Cast Evid count to a string for output.
Console.WriteLine("Total Evidence Enumerator members : " + (Evid.Count).ToString());
Console.WriteLine();
enumerator.MoveNext();
//output current index contents of the enumerator
Console.WriteLine(enumerator.Current);
Console.ReadLine();
}

To use refactoring to extract this method, you first select the statements you want to reside in the method, then right-click to access the shortcut menu. You then select the Refactor - Extract Method option from the menu.


In the Extract Method dialog box, you provide the name for the new method and click OK.

After refactoring, the code contains a new method. The call for this method is automatically placed where the refactored code originally was located and it is passed the required parameters.

struct Module1 
{

void main()
{
Evidence Evid;
IEnumerator enumerator;
Assembly assemblyName;
assemblyName = Assembly.GetAssembly(typeName);
Evid = assemblyName.Evidence;
enumerator = Evid.GetEnumerator();
Console.WriteLine();
CastEvidCountToStringForOutput(Evid, enumerator);
}

private void CastEvidCountToStringForOutput(Evidence Evid, IEnumerator enumerator)
{
Console.WriteLine("Total Evidence Enumerator members : " + (Evid.Count).ToString);
Console.WriteLine();
enumerator.MoveNext();
Console.WriteLine(enumerator.Current);
Console.ReadLine();
}
}

Visual Studio 2005's code snippets enable you to add predefined code easily. Suppose you want to add a code snippet that will add a sorted dictionary to your code. To do this, you first need to check the shortcut for the code snippet using the Code Snippets Manager.

You can open the Code Snippets Manager either by

- right-clicking your code and selecting Insert Snippet from the shortcut menu that displays
- selecting Edit - Intellisense - Insert Snippet
- selecting Tools - Code Snippets Manager

public class Form1 
{

private void Button1_Click(object sender, System.EventArgs e)
{
}
}

You decide to open the Code Snippets Manager from the Tools menu.

public class Form1 
{

private void Button1_Click(object sender, System.EventArgs e)
{
}
}

You select Tools - Code Snippets Manager.

The Code Snippets Manager identifies the language in which your code is written by default, and displays a list of folders containing code snippets grouped according to their functions.

To access the shortcut for the code snippet you require, you expand the collections and arrays folder and select Create a sorted dictionary.

The Code Snippets Manager then lists a description of the code snippet, its shortcut – which is colCreateSort, and its author.

Now that you've obtained the required shortcut, you close the Code Snippets Manager and then type the shortcut – colCreateSort – in your code, at the point at which you want to insert the code snippet.

public class Form1 
{

private void Button1_Click(object sender, System.EventArgs e)
{
colCreateSort
}
}

You then press the Tab key. Visual Studio 2005 inserts the code snippet over the shortcut you've added.

Visual Studio 2005 highlights parts of the code snippet that can be customized. Each of these areas displays tool tips containing extra information if you hold your cursor over them.

You press the Tab key to navigate between the highlighted areas.

public class Form1
{

private void Button1_Click(object sender, System.EventArgs e)
{
SortedDictionary<int, string> sortedStudents = new SortedDictionary<int, string>();

sortedStudents.Add(1, "Mary Chase");
}
}

The properties of variables inside code snippets update automatically. For example, suppose you change the variable sortedStudents to staffList.

public class Form1
{

private void Button1_Click(object sender, System.EventArgs e)
{
SortedDictionary<int, string> staffList = new SortedDictionary<int, string>();

sortedStudents.Add(1, "Mary Chase");
}
}

When you move your cursor off the highlighted entry, all other instances of the sortedStudents variable in the code snippet also change to staffList immediately.

Next suppose you want to access a graphical view of the classes in an application, using the Class Designer. To do this, you first select View - Solution Explorer to open the Solution Explorer.

From the Solution Explorer, you access a graphic view of classes in your application.

You click the View Class Diagram icon.

The Class Designer tabbed page displays details of classes using a diagram and columns in a Class Details pane, and creates a file with the .cd extension.

In the Class Details pane, you can navigate between classes.

Using the Class Designer View, you can right-click the form diagram and then

Question

Match Visual Studio 2005 developer productivity enhancements to their uses.

Options:

  1. Class Designer
  2. Code snippet
  3. DTEE
  4. Solution Explorer

Targets:

  1. Enables you to test sections of code quickly
  2. Enables you to visualize the structure of an application
  3. Provides a template for coding a commonly performed task

Answer

DTEE enables you to test sections of code quickly, the Class Designer enables you to visualize the structure of an application, and code snippets provide templates for coding commonly performed tasks.

The Class Designer provides a graphic representation of the class structure of an application. You can change the structure using the diagram, and the diagram updates automatically if you make changes to the code. It also provides access to the Object Test Bench to test an object's members, a DTEE enhancement.

Visual Studio 2005 includes over 500 code snippets, which are code templates for performing common tasks. You can import code snippets into a script and then customize them as necessary.

Design-Time Expression Evaluation (DTEE) is a feature of Visual Studio 2005 that enables you to execute pieces of code without compiling an entire application. This enables you to test code sections easily.

The Solution Explorer was present in Visual Studio 2003 and earlier versions. It provides a directory tree for navigating the components and resources of Visual Studio applications.

2. Strong typing and API enhancements

Visual Studio 2005 supports strong typing of resources and assemblies.

It also includes the ASP.NET configuration application programming interface (API), which makes application management easier.

Visual Studio 2005 supports strongly typed resources through the

Resource Builder
The Visual Studio Resource builder enables strong typing of resources through the StronglyTypedResourceBuilder class in the .NET Framework Class Library. This class can't be inherited. You reference a resource in an input parameter, and the Create method then generates an encapsulating class for a strongly-typed resource that controls its access through static, read-only properties.

Strong typing enables you to use a resource throughout a project, rather than re-creating it for each form. It also reduces required debugging, by enforcing strict coding rules.
Resource File Generator
The Resource File Generator utility – Resgen.exe – generates strongly typed resources.

It also converts XML-based text (.txt) and resource format (.resx) files to common language runtime (CLR) binary files with the .resource extension and vice versa. It can also change back and forth between said .txt and .resx formats.

Files with the .resource extension can be used directly in binary executables, or they can be compiled into satellite assemblies.

Once you've built a strongly-typed resource, you can use Resgen.exe to generate it from the command line.

resgen [parameters] [/compile]filename.extension _
[outputFilename.extension] [/str:lang[,namespace[,class[,file]]]]

This command, for example, generates the MainForm.resx resource in the specified container.

C:\Documents and Settings\Administrator>resgen e:\applications\MainForm.resx e:\applications\myResources.resources

Key optional parameters include /compile, which allows multiple conversions of .resx or .txt files into .resources in one command. Another option (which cannot be used at the same time as /compile) is /str, which creates a strongly-types resource file in the lanugage specified by lang. This option can also specify namepsace, classnames, and filenames.

Question

You enter this code:

resgen e:\applications\MainForm.resx e:\applications\my _
Resources.resources

What does this code do?

Options:

  1. Build a strongly typed resource
  2. Generate a strongly typed resource
  3. Enforce strict coding rules
  4. Call a static method without requiring that you instantiate it

Answer

The code generates a strongly typed resource from the command line.

Option 1 is incorrect. The code in this example generates, rather than builds, a strongly typed resource. You use the StronglyTypedResourceBuilder class in the .NET Framework Class Library to build a strongly typed resource.

Option 2 is correct. Once you've built a strongly typed resource, which represents embedded data such as strings or images, you can use the Resource Generator (Resgen.exe) to generate the resource from the command line. The syntax for doing this is

resgen [parameters] [/compile]_
filename.extension_
[outputFilename.extension]_
[/str:lang[,namespace_
[,class[,file]]]]

Option 3 is incorrect. Strict coding rules are enforced through strong typing of resources. However, the code in this example generates a resource, rather than building it using strong typing.

Option 4 is incorrect. To call static methods without instantiating them, you use the Object Test Bench, which is a platform for runtime testing of an application. Because it's a platform, rather than a coding structure, it doesn't use syntax.

In Visual Studio 2005, Signing settings in the Properties dialog box for a project enable you to create strongly named assemblies, which are named in strict adherence to coding best practices.

In addition to supporting strong assembly naming, the Properties dialog box for a project now enables you to

configure security permissions
You can test security credentials more easily, by using configurable security permissions when debugging. Using the Security settings in the Properties dialog box, you can choose which permissions to include, configure their settings, and calculate required permissions.

Description of the Security pane in the Properties dialog box follows.

The Security pane includes settings for specifying the required configuration and platform, an Enable ClickOnce Security Settings checkbox, and radio buttons for specifying whether an application must be fully or partially trusted. A ClickOnce Security Permissions section contains a drop-down list for selecting the zone from which an application will be installed, and a table that lists permissions, their settings, and icons to indicate whether they've been included. The section also contains Calculate Permissions, Properties, and Reset buttons.

End description.
apply Code Analysis Integration
Using the Code Analysis settings in the Properties dialog box, you can select code analysis rules individually or in groups. You can also specify how rule violations should be flagged – as Error or Warning.

Visual Studio 2005 Team System now incorporates FxCop as part of the IDE.

Description of the Code Analysis pane of the Properties dialog box follows.

The pane includes Configuration and Platform drop-down list boxes, and an Enable Code Analysis checkbox. It also contains a table that lists rule categories in the form of expandable nodes. A Status column in the table displays an icon for each rule category, indicating its current status. Examples of the rule categories listed are Design, Globalization, Interoperability, and Maintainability.

End description.

The ASP.NET Configuration Settings API is a tool for handling application configuration and data, via a single interface.

It enables you to automate application deployment, incorporates Internet Information Server (IIS) management tools, and create ASP utilities without editing XML files.

Using ASP.NET, you can also access application configuration settings by exposing the settings directly as strongly-typed properties.

The ASP.NET configuration API includes the

ASP.NET Microsoft Management Console (MMC) snap-in GUI
The ASP.NET MMC enables you to access and edit configuration settings via three directories – System Tools, Storage, and Services and Applications.

The System Tools directory enables you to view system events, manage folder sharing, manage local user and group settings, configure and view performance logs and alerts, and manage devices. You use settings under the Storage directory to configure storage devices, and to defragment and manage disks, for example. The Services and Applications directory provides access to settings for managing services, Windows Management Instrumentation (WMI), SQL server configuration, indexing, and Internet Information Services (IIS).

Description of the ASP.NET MMC follows.

The ASP.NET MMC is an application interface with five menus – File, Action, View, Window, and Help. There is a single toolbar that enables you to navigate the directory structure, which also displays in a tree pane.

End description.
Web Site Administration Tool overview
The Web Site Administration Tool enables you to configure a default web site, print Crystal Reports, and configure the default SMTP virtual server, which involves managing domains and current sessions.

You access the Web Site Administration Tool in the ASP.Net Configuration dialog box, via an option on the Website menu in Internet Information Services (IIS).

The browserCaps element is part of the Web.Config file, in the ASP.NET configuration schema. You use it to specify the browser capabilities of an application – that is, which browsers the application supports – and can update it when necessary.

You access the browserCaps code in a script via the Browser property of the System.Web.HttpRequest class. The actual <browserCaps> data is included in the machine.config file.

<%@ Page Language="C#" %>
<html>
<body style="font: 10pt verdana">
<h3>Retrieving Browser Capabilities</h3>
Boolean ActiveXControls =
<%=Request.Browser.ActiveXControls.ToString()%><br>
Boolean Cookies =
<%=Request.Browser.Cookies.ToString()%><br>
Boolean Crawler =
<%=Request.Browser.Crawler.ToString()%><br>
...
</body>
</html>

Supplement

Selecting the link title opens the resource in a new browser window.

Launch window

View the browserCaps property in code.

When you run the browserCaps code, output lists all browser capabilities expressed in the form of boolean values.

Boolean ActiveXControls = True;
Boolean Cookies = True;
Boolean Crawler = False;

Supplement

Selecting the link title opens the resource in a new browser window.

Launch window

View the full output of the browserCaps code.

You can now find generic types – code templates – in an object's IntelliSense shortcut menu, indicated by a blue diamond icon.

Generics enable you to create types efficiently and then declare variables using a template.



public class Form1
{
private void Form1_Load(object sender, System.EventArgs e)
{
Col<string> mystring = new Col<str
}
}
public class Col<T>
{
T _t;
public T Val {
get {
return _t;
}
set {
_t = value;
}
}
}

The IntelliSense menu lists recently opened or commonly used members on its Common tabbed page.

The IntelliSense menu now filters automatically. For example, if you add a Try/Catch block, the IntelliSense menu shows only exception types. The IntelliSense menu also filters attribute types and related code, based on the attribute you type.


private void Form1_Load(object sender, System.EventArgs e)
{
Col<string> mystring = new Col<string>();
try {
if (mystring.ToString() == "")
{
}
}
catch (E
{
}
}

Question

Which feature of Visual Studio 2005 enables you to convert XML-based .resx files to binary files?

Options:

  1. Generic types
  2. Object Test Bench
  3. Resource Builder
  4. Resource File Generator

Answer

The Resource File Generator enables you to convert XML-based .resx files into common language runtime binary files.

Option 1 is incorrect. Generic types are code templates for creating coding structures. They don't convert XML files into executable files, or generate resources.

Option 2 is incorrect. The Object Test Bench enables you to test objects by creating class instances, invoking methods, and viewing results. It doesn't convert XML files into binary files.

Option 3 is incorrect. The Resource Builder enables strong typing of resources, through the StronglyTypedResourceBuilder class in the .NET Framework Class Library. It doesn't, however, convert XML files into binary code.

Option 4 is correct. The Resource File Generator (Resgen.exe) enables you to convert XML-based .resx or .txt files into binary files with the .resource extension. You can execute .resource files directly or compile them into satellite assemblies.

Summary

Visual Studio 2005 includes many developer productivity enhancements. The code editing facilities have been improved – line revision marks, for example, help you keep track of code changes, and you can customize source code formatting. The new designers, which portray code structures visually, enable you to organize code easily, and to modify structures without having to type new code. Code snippets are provided as standardized code templates for coding commonly performed tasks.

Code benefits from the new strong-typing capabilities included in the Resource Builder and Resource File Generator. The ASP.NET configuration application programming interface (API) makes it easier to configure and manage applications, and the improved Property dialog box provides easy access to code analysis tools, application publishing, and security settings for projects.