that most closely matches the characters you typed, but it will not automatically select it. Instead,
the characters that you type are added to the top of the IntelliSense list and if you type one of the
commit characters or press Space or Enter, the exact string that you typed is inserted into the editor
window.
Figure 7 - 8 shows an example of the problem that suggestion mode is designed to address. On the
left - hand side we are writing a test for a new method called Load on the CustomerData class.
The CustomerData class does not have a method called Load yet but it does have a method called
LoadAll .
On the right - hand side of Figure 7 - 8 we have typed Load
followed by the open parenthesis character. IntelliSense has
incorrectly assumed that we wanted the LoadAll method
and has inserted it into the editor.
To avoid this behavior we can turn on suggestion mode by pressing
Ctrl+Alt+Space. Now when we type Load it appears at the top of the
IntelliSense list. When we type the open parenthesis character we get Load as
originally intended (see Figure 7 - 9).
fiGure 7 - 8
fiGure 7 - 9
Only Visual Basic gives you the option to fi lter the member list down to
commonly accessed properties, methods, and events.
You can still make a selection from the IntelliSense list by using the arrow keys.
Also, you can select the item that has focus in the member list by pressing the
Tab key.
intellisense explained .
125
IntelliSense remains in suggestion mode until you press Ctrl+Alt+Space again to
revert back to completion mode.
stub Completion
In addition to word and phrase completion, the IntelliSense engine has another feature known as
stub completion. This feature can be seen in Visual Basic when you create a function by writing
the declaration of the function and pressing Enter. Visual Studio automatically reformats the line,
adding the appropriate ByVal keyword for parameters that don’t explicitly define their contexts,
and also adding an End Function line to enclose the function code. Another example can be seen
when editing an XML document. When you type the open tag of a new element, Visual Studio
automatically puts the closing tag in for you.
Visual Studio 2010 takes stub completion an extra step by enabling you to do the same for interface
and method overloading. When you add certain code constructs such as an interface in a C# class
definition, Visual Studio gives you the opportunity to automatically generate the code necessary to
implement the interface. To show you how this works, the following steps outline a task in which
the IntelliSense engine generates an interface implementation in a simple class:
1 Start Visual Studio 2010 and create a C# Windows Forms Application project. When the
IDE has finished generating the initial code, open Form1.cs in the code editor.
2 At the top of the file, add a using statement to provide a shortcut to the System.Collections
namespace:
using System.Collections;
3 Add the following line of code to start a new class definition:
public class MyCollection: IEnumerable
As you type the IEnumerable interface, Visual Studio first
adds a red wavy line at the end to indicate that the class
definition is missing its curly braces, and then adds a smart
tag indicator at the beginning of the interface name (see
fiGure 7-10
Figure 7 - 10).
4 Hover your mouse pointer over the
smart tag indicator. When the drop-
down icon appears, click it to open
the menu of possible actions, as
shown in Figure 7-11. fiGure 7-11
5 Click either of the options to “implement interface ‘IEnumerable’” and Visual Studio 2010
automatically generates the rest of the code necessary to implement the minimum interface
126 .
chaPter 7 inTelliSenSe And bookmArkS
definition. Because it detects that the class definition
itself isn’t complete, it also adds the braces to correct that
issue at the same time. Figure 7-12 shows what the final
interface will look like.
Event handlers can also be automatically generated by Visual
fiGure 7-12
Studio 2010. The IDE does this much as it performs interface
implementation. When you write the first portion of the statement (for instance,
myBase.OnClick +=), Visual Studio gives you a suggested completion that you can select
simply by pressing Tab.
Generate from Usage
Rather than generating code from a definition that already exists, sometimes it is more convenient
to generate the definition of a code element from the way you have used it. This is especially true
if you practice test-driven development where you write tests for classes that have not been defined
yet. It would be convenient to be able to generate the classes from the tests themselves and this is the
purpose of the Generate From Usage feature in C# and Visual Basic.
To understand how you might use this in practice, the following steps outline the creation of a very
simple Customer class by writing some client code that uses it and then generating the class from
that usage:
1 Start Visual Studio 2010 and create a C# Command Line project. When the IDE is ready
open the Program.cs file.
2 Update the Main method with the following code:
c#
Customer c = new Customer
{
FirstName = "Joe",
LastName = "Smith"
};
Console.WriteLine(c.FullName);
c.Save();
3 You should see a red wiggly line underneath both instances of the class name Customer.
Right-click one of them and select Generate . Class. This should create a new class in your
project called Customer. If you open Customer.cs you will see an empty class declaration.
Visual Studio will discover that FirstName, LastName, FullName, and Save are not
members on this class.
4 For each property that does not exist, right-click it and select Generate . Property. Now
go and look at Customer.cs again and note that Visual Studio has been able to provide an
implementation for you.
5 You can do the same for the Save method by right-clicking and selecting Generate .
Method Stub.
If the undefi ned code that you are trying to generate is
a type, you will have the option to Generate Class or
Generate Other. If you select Generate Other, the Generate
New Type dialog is shown (see Figure 7 - 13). This dialog
gives you more options to confi gure your new type
including whether you want a class, enumeration, interface,
or structure; whether the new type should be public,
private, or internal; and where the new type should go.
Parameter information
In old versions of Microsoft development tools, such
as Visual Basic 6, as you created the call to a function,
IntelliSense would display the parameter information as
you typed. Thankfully, this incredibly useful feature is still
present in Visual Studio 2010.
The problem with the old way parameter information was displayed was that it would only be
shown if you were actually modifying the function call. Therefore, you could see this helpful tooltip
as you created the function call or when you changed it but not if you were just viewing the code.
The result was that programmers sometimes inadvertently introduced bugs into their code because
they intentionally modifi ed function calls so they could view the parameter information associated
with the calls.
Visual Studio 2010 eliminates that risk by providing an easily accessible command to display the
information without modifying the code. The keyboard shortcut Ctrl+Shift+Space displays the
information about the function call, as displayed in
Figure 7 - 14. You can also access this information
through the Edit . IntelliSense . Parameter Info menu
command.
fiGure 7 - 13
fiGure 7 - 14
Though generated properties and classes can be used straight away, method
stubs are generated to throw a NotImplementedException .
In Figure 7 - 14 the PrintGreeting method takes two parameters. The second
parameter is optional and displays in square brackets with an assignment
showing its default value if you don ’ t provide one. VB programmers will be
familiar with this syntax but it is new to C# 4.0.
intellisense explained . 127
128 . chaPter 7 inTelliSenSe And bookmArkS
quick info
In a similar vein, sometimes you want to see the information about an object or interface without
modifying the code. The Ctrl+K, Ctrl+I keyboard shortcut displays a brief tooltip explaining what
the object is and how it was declared (see Figure 7 - 15).
You can also display this tooltip through the Edit . IntelliSense .
Quick Info menu command.
JaVascriPt intellisense
If you are building web applications, you will fi nd yourself working in JavaScript to provide a richer
client - side experience for your users. Unlike C# and Visual Basic, which are compiled languages,
JavaScript is an interpreted language, which means that traditionally the syntax of a JavaScript
program has not been verifi ed until it is loaded into the browser. Although this can give you a lot of
fl exibility at run time, it requires discipline, skill, and a heavy emphasis on testing to avoid a large
number of common mistakes.
In addition to this, while developing JavaScript components for use in a browser, you must
keep track of a number of disparate elements. This can include the JavaScript language features
themselves, HTML DOM elements, and handwritten and third - party libraries. Luckily Visual
Studio 2010 is able to provide a full IntelliSense experience for JavaScript, which will help you to
keep track of all of these elements and warn you of syntax errors.
As you type JavaScript into the code editor window, Visual Studio lists keywords, functions,
parameters, variables, objects, and properties just as if you were using C# or Visual Basic. This
works for built - in JavaScript functions and objects as well as those you defi ne in your own custom
scripts and those found in third - party libraries. Visual Studio is also able to detect and highlight
syntax errors in your JavaScript code.
The keyboard shortcuts for each VS2010 install depend on the settings selected
(i.e. Visual Basic Developer, Visual C# Developer, and so on). All of the shortcut
keys in this chapter are based on using the General Developer Profi le setting.
fiGure 7 - 15
Since Internet Explorer 3.0 Microsoft, has maintained its own dialect of
JavaScript called JScript. Technically, the JavaScript tools in Visual Studio 2010
are designed to work with Jscript, so you will sometimes see menu options and
window titles containing this name. In practice, the differences between the two
languages are so minor that the tools work equally well with either one.
Javascript intellisense .
129
the Javascript intellisense context
To prevent you from accidentally referring to JavaScript elements that are not available, Visual
Studio 2010 builds up an “IntelliSense context” based on the location of the JavaScript block that
you are editing. The context is made up of the following items:
.
The current script block. This includes inline script blocks for .aspx, .ascx, .master,
.html, and .htm files.
.
Any script file imported into the current page via a < script / > element or a ScriptManager
control. In this case the imported script file must have the .js extension.
.
Any script files that are referenced with a references directive (see the section “Referencing
another JavaScript File” later in this chapter).
.
Any references made to XML Web Services.
.
The items in the Microsoft AJAX Library (if you are working in an AJAX-enabled
ASP.NET web application).
Visual Studio keeps track of files in the context and updates JavaScript
IntelliSense whenever one of them changes. Sometimes this update may be
pending and the JavaScript IntelliSense data will be out of date. You can force
Visual Studio to update the JavaScript IntelliSense data by selecting Edit .
IntelliSense . Update JScript IntelliSense.
Occasionally something will go wrong and
Visual Studio will be unable to build a JavaScript
IntelliSense context. Often, though, Visual Studio
will be able to determine what caused the error
and provide you with feedback that you can use
to correct the issue. In Figure 7-16, Visual Studio
has detected that we have made a reference to a
JavaScript file that does not exist. When you add
the file to the project, Visual Studio will detect its
presence and remove the error indicator and error
message. Although this error detection normally
happens as a background process you can force
Visual Studio to check a page by selecting Edit .
Advanced .
Validate Document.
referencing another Javascript file
Sometimes one JavaScript file builds upon the base functionality of another. When this happens
they are usually referenced together by any page using them but have no direct reference explicitly
defined. Because there is no explicit reference, Visual Studio 2010 is unable to add the file with the
base functionality to the JavaScript IntelliSense context and you won’t get full IntelliSense support.
fiGure 7-16
130
.
chaPter 7 inTelliSenSe And bookmArkS
To allow Visual Studio to discover the base file and add it to the context you can provide a reference
to it by using a references directive. A references directive is a special kind of comment that provides
information about the location of another file. You can use references directives to make a reference
to any of the following:
.
Other JavaScript files: This includes .js files and JavaScript embedded in assemblies. It does
not include absolute paths so the file you reference must be a part of the current project.
.
Web Service (.asmx) files: These also must be a part of the current project and Web Service
files in Web Application projects are not supported.
.
Pages containing JavaScript: One page may be referred to in this way. If any page is
referenced, no other references can be made.
Following are some examples of references directives. These must appear before any other code in