After completing this topic, you should be able to identify various developer-productivity enhancements in Visual Studio 2005.
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
C# 2005 includes many new refactoring capabilities, which automate common tasks. These can help improve and simplify complex code structures.
Selecting the link title opens the resource in a new browser 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 type
s
Name
, 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
Match Visual Studio 2005 developer productivity enhancements to their uses.
Options:
Targets:
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.
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
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.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.
You enter this code:
resgen e:\applications\MainForm.resx e:\applications\my _
Resources.resources
What does this code do?
Options:
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
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
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>
Selecting the link title opens the resource in a new browser 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;
Selecting the link title opens the resource in a new browser 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
{
}
}
Which feature of Visual Studio 2005 enables you to convert XML-based .resx files to binary files?
Options:
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.
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.
| Top of page |
| 1. Productivity enhancements |
| 2. Strong typing and API enhancements |
| Summary |
Copyright © 2006 SkillSoft. All rights reserved.
SkillSoft and the SkillSoft logo are trademarks or registered trademarks
of SkillSoft in the United States and certain other countries.
All other logos or trademarks are the property of their respective owners.