Visual Studio Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual Studio Hacks [Electronic resources] - نسخه متنی

Andrew Lockhart

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 8. Master IntelliSense

Visual Studio can read your mind. Learn how to
have Visual Studio's IntelliSense feature do lots of
the typing for you.


Even if you have used Visual Studio only
a couple times, I am sure you have noticed one of its trademark
features: IntelliSense. The most popular form of IntelliSense assists
you as you code by providing a list of the
members for each class
immediately after you finish typing the class name (and the following
period). This is not the only form of IntelliSense though; this
feature finds its way into a multitude of different areas of Visual
Studio.


2.4.1. Complete Word



The
first, and most useful, IntelliSense
feature is actually just a shortcut: Ctrl-Space (Edit.CompleteWord). By
using the Ctrl-Space shortcut, you can summon IntelliSense at any
point during your coding session, not just when you finish typing a
class name. This is one of the few shortcuts that can really change
the way that you write code.

If you have already starting typing when you press Ctrl-Space, Visual
Studio will take one of two actions. If there is only one object that
matches what you have typed in so far, it will automatically complete
the object name; for instance, if you typed in
HttpR, it would automatically complete the
rest of the object name (HttpRequest). If there are a number of
objects that match what you have typed in, it will display the full
list of members with the first match highlighted. Figure 2-7 shows what the listbox would look like if you
only typed in the letter P.


Figure 2-7. IntelliSense listbox, brought to you by the letter P

You can also press Ctrl-Space on a blank line and be shown a complete
list of members in the current context. This is a great way to find a
variable name that has slipped your mind. Figure 2-8 shows an example of this.


Figure 2-8. IntelliSense listbox showing all members

This shortcut can be even more useful if you name all of your private
member variables with a common prefix; for instance, if all of your
variables are prefixed with "_", by
typing _ and then pressing Ctrl-Space, you
would display a list of all of your variables to choose from.


2.4.2. Parameter Info


Another

useful form of IntelliSense is the
parameter information that is shown after you type the opening
parenthesis of a method (it goes away when the parentheses are
closed). When editing an already existing method, it would be nice to
have this information again without having to delete and then reenter
the opening parenthesis, wouldn't it? As long as the
cursor is located inside of the method parameters parenthesis,
pressing Ctrl-Shift-Space (Edit.ParameterInfo) will display the
parameter information pop up. Figure 2-9 shows this
pop up.


Figure 2-9. Parameter info IntelliSense pop up


2.4.3. Quick Info





When you move your mouse over a method or
variable, you will see a small tool tip pop up that contains
information about that method or variable. This is commonly called
Quick Info. If you are navigating by keyboard, you can also get this
small pop up by pressing Ctrl-K and then Ctrl-I
(Edit.QuickInfo). Using this shortcut is also the only way to bring
up this informational pop up during debug, since the default behavior
is to show the value of the object you are hovering over when using
the mouse.


2.4.4. Stub Completion


Another form of
autocompletion
called stub completion can be very useful when working with
interfaces or event handlers or when overriding a method. This
feature can be used to automatically stub out
the methods required by the interface or the default code for an
overridden method or event handler. This is a feature that was added
to Visual Studio .NET 2003, but its presence is not always obvious
when you are coding quickly.

2.4.4.1 Interface


When you first
create a class and add the
interface name after the colon (or the Implements in Visual Basic), a
small yellow window will pop up for just a few seconds, offering you
the ability to press the Tab key, as shown in Figure 2-10.


Figure 2-10. Interface IntelliSense prompt

When you press the Tab key with this window still displayed, the
methods required by the interface will be automatically added to your
class, including TODO comments to remind you to complete the methods:

public class CarCollection : IEnumerable
{
public CarCollection( )
{
}
#region IEnumerable Members
public IEnumerator GetEnumerator( )
{
// TODO: Add CarCollection.GetEnumerator implementation
return null;
}
#endregion
}

2.4.4.2 Overriding methods


When you type override into the document
window, you will see a list of methods you can override in the base
class. This can be seen in Figure 2-11.


Figure 2-11. Override IntelliSense

When you select the method you want to override, Visual Studio will
create the method and automatically add a call to the method you are
overriding on your base class like this:

public override void Start( )
{
base.Start ( );
}

2.4.4.3 Event handlers


When adding a new event handler to your class, Visual Studio will
automatically create the stub of this event handler, much as when
working with an interface. When you type the code to add an event
handler to an event, you will see a prompt inviting you to press the
Tab key, as shown in Figure 2-12.


Figure 2-12. EventHandler IntelliSense before pressing the Tab key

When you press the Tab key, you will then see the prompt shown in
Figure 2-13.


Figure 2-13. EventHandler IntelliSense after pressing the Tab key

When you press the Tab key again, the following code will be inserted
into your document:

private void Car_OnStart(object sender, EventArgs e)
{
}


2.4.5. IntelliSense Comments


When

comments
exist for a method or class, they are displayed in the IntelliSense
pop up shown in Figure 2-14.


Figure 2-14. IntelliSense comments

These comments can be created a number of different ways. If you are
using C#, then these comments are pulled from the XML-based comments
in your code, like the following example:

/// <summary>
/// This method loads any number of snippets into the toolbox
/// </summary>
public static void LoadSnippets( )

If you are using C++, these comments are generated based on any
comments at the end of the line for the member; if no comments exist
at the end of the line, then any comments on the line directly above
the member are used. Any comments on the declaration are used first;
if there are no comments on the declaration, any comments on the
definition are used. To get this same functionality with VB.NET, you
will need to install the VBCommenter power toy [Hack #70] .

IntelliSense is one of the best features of Visual Studio, and using
the shortcut keys summarized in Table 2-1 will
allow you to get the most out of this great
feature.

Table 2-1. IntelliSense shortcut keys

Command


Shortcut keys


Description


Edit.CompleteWord


Ctrl-Space


Completes the current word or shows all available methods and
properties for a class


Edit.ParameterInfo


Ctrl-Shift-Space


Shows the parameter information when the cursor is inside method
parentheses


Edit.QuickInfo


Ctrl-K, Ctrl-I


Shows a quick description about whatever object the cursor is
currently resting on


2.4.6. See Also


[Hack #32]



/ 172