Sayfalar

5 Şubat 2011 Cumartesi

Member List


Visual Studio makes it easy for you to interact with controls and classes. When you type a period (.) after
a class or object name, Visual Studio pops up a list of available properties and methods (see Figure 2-16).
It uses a similar trick to provide a list of data types when you define a variable and to provide a list of
valid values when you assign a value to an enumeration.


Visual Studio also provides a list of parameters and their data types when you call a method or
invoke a constructor. This information is presented in a tooltip below the code and is shown as you type.
Because the .NET class library heavily uses function overloading, these methods may have multiple
different versions. When they do, Visual Studio indicates the number of versions and allows you to see
the method definitions for each one by clicking the small up and down arrows in the tooltip. Each time
you click the arrow, the tooltip displays a different version of the overloaded method

Outlining


Outlining allows Visual Studio to “collapse” a subroutine, block structure, or region to a single line. It
allows you to see the code that interests you, while hiding unimportant code. To collapse a portion of
code, click the minus box next to the first line. Click the box again (which will now have a plus symbol) to
expand it You can collapse an entire code file so that it only shows definitions (such as the namespace and
class declarations, member variables and properties, method declarations, and so on), but hides all
other details (such as the code inside your methods and your namespace imports). To get this top-level
view of your code, right-click anywhere in the code window and choose Outlining ➤ Collapse to
Definitions. To remove your outlining and expand all collapsed regions so you can see everything at
once, right-click in the code window and choose Outlining ➤ Stop Outlining.

IntelliSense and Outlining


As you program with Visual Studio, you’ll become familiar with its many time-saving conveniences. The
following sections outline the most important features you’ll use (none of which is new in Visual Studio
2010).

Adding Assembly References


By default, ASP.NET makes a small set of commonly used .NET assemblies available to all web pages.
These assemblies (listed in Table 2-4) are configured through a special machine-wide configuration file.
You don’t need to take any extra steps to use the classes in these assemblies.


If you want to use additional features or a third-party component, you may need to import more
assemblies. For example, if you want to use an Oracle database, you need to add a reference to the
System.Data.OracleClient.dll assembly. To add a reference, select Website ➤ Add Reference (or
Project ➤ Add Reference in a web project). The Add Reference dialog box will appear, with a list of
registered .NET assemblies (see Figure 2-14).
■ Note Visual Studio 2010 has enhanced the Add Reference window to use asynchronous loading. As a result, it
appears much quicker and doesn’t freeze you out while it scans your system for assemblies. However, while these
assemblies are being added to the list, you may find it difficult to select the item you want before it “jumps” to a
new position.


In the Add Reference dialog box, select the component you want to use. If you want to use a
component that isn’t listed here, you’ll need to click the Browse tab and select the DLL file from the
appropriate directory (or from another project in the same solution, using the Projects tab).
If you’re working with a projectless website and you add a reference to another assembly, Visual
Studio modifies the web.config file to indicate the assembly you’re using. Here’s an example of what you
might see after you add a reference to the System.Web.Routing.dll file:


<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly=
"System.Web.Routing, Version=4.0.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
If you’re working with a web project, and you add a reference to another assembly, Visual Studio
doesn’t need to change the web.config file. That’s because Visual Studio is responsible for compiling the
code in a web project, not ASP.NET. Instead, Visual Studio makes a note of this reference in the .csproj
project file. The reference also appears in the Solution Explorer window under the References node. You
can review your references here, and remove any one by right-clicking it and choosing Remove.
If you add a reference to an assembly that isn’t stored in the GAC (global assembly cache), Visual
Studio will create a Bin subdirectory in your web application and copy the DLL into that directory. (This
happens regardless of whether you’re using project-based or projectless development.) This step isn’t
required for assemblies in the GAC because they are shared with all the .NET applications on the
computer.
If you look at the code for a web-page class, you’ll notice that Visual Studio imports just a few core
.NET namespaces. Here’s the code you’ll see:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Adding a reference isn’t the same as importing the namespace with the using statement. The using
statement allows you to use the classes in a namespace without typing the long, fully qualified class
names. However, if you’re missing a reference, it doesn’t matter what using statements you include—the
classes won’t be available. For example, if you import the System.Web.UI namespace, you can write
Page instead of System.Web.UI.Page in your code. But if you haven’t added a reference to the
System.Web.dll assembly that contains these classes, you still won’t be able to access the classes in the
System.Web.UI namespace.



The Code Editor


Many of Visual Studio’s handiest features appear when you start to write the code that supports your
user interface. To start coding, you need to switch to the code-behind view. To switch back and forth,
you can use two buttons that are placed just above the Solution Explorer window. The tooltips identify
these buttons as View Code and View Designer. When you switch to code view, you’ll see the page class
for your web page. You’ll learn more about code-behind later in this chapter.
ASP.NET is event-driven, and everything in your web-page code takes place in response to an event.
To create a simple event handler for the Button.Click event, double-click the button in design view.
Here’s a simple example that displays the current date and time in a label:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Current time: " + DateTime.Now.ToLongTimeString();
}
To test this page, select Debug ➤ Start Debugging from the menu. Because this is the first time
running any page in this application, Visual Studio will inform you that you need a configuration file that
specifically enables debugging, and will offer to change your current web.config file accordingly


Click OK to change the web.config configuration file. Visual Studio will then start the integrated test
web server and launch your default browser with the URL set to the current page that’s open in Visual
Studio. At this point, your request will be passed to ASP.NET, which will compile the page and execute it.
To test your event-handling logic, click the button on the page. The page will then be submitted to
ASP.NET, which will run your event-handling code and return a new HTML page with the data

Server Explorer


The Server Explorer provides a tree that allows you to explore various types of services on the current
computer (and other servers on the network). It’s similar to the Computer Management administrative
tool. Typically, you’ll use the Server Explorer to learn about available event logs, message queues,
performance counters, system services, and SQL Server databases on your computer.
The Server Explorer is particularly noteworthy because it doesn’t just provide a way for you to
browse server resources; it also allows you to interact with them. For example, you can create databases,
execute queries, and write stored procedures using the Server Explorer in much the same way that you
would using SQL Server Management Studio, the administrative utility that’s included with the full
version of SQL Server. To find out what you can do with a given item, right-click it. Figure 2-11 shows the
Server Explorer window listing the databases in a local SQL Server and allowing you to retrieve all the
records in the selected table.

Error List and Task List


The Error List and Task List are two versions of the same window. The Error List catalogs error
information that’s generated by Visual Studio when it detects problematic code. The Task List shows a
similar view with to-do tasks and other code annotations you’re tracking. Each entry in the Error List and
Task List consists of a text description and, optionally, a link that leads you to a specific line of code
somewhere in your project.


With the default Visual Studio settings, the Error List appears automatically whenever you build a
project that has errors


To see the Task List, choose View ➤ Task List. Two types of tasks exist—user tasks and comments.
You can choose which you want to see from the drop-down list at the top of the Task List. User tasks are
entries you’ve specifically added to the Task List. You create these by clicking the Create User Task icon
(which looks like a clipboard with a check mark) in the Task List. You can give your task a basic
description, a priority, and a check mark to indicate when it’s complete.

■ Note As with breakpoints, any custom tasks you add by hand are stored in the hidden solution files. This makes
them fairly fragile—if you rename or move your project, these tasks will disappear without warning (or without
even a notification the next time you open the website).


The comment entries are more interesting because they’re added automatically and they link to a
specific line in your code. To try the comment feature, move somewhere in your code, and enter the
comment marker (//) followed by the word TODO (which is commonly referred to as a token tag). Now
type in some descriptive text:
// TODO: Replace this hard-coded value with a configuration file setting.
string fileName = @"c:\myfile.txt"
Because your comment uses the recognized token tag TODO, Visual Studio recognizes it and
automatically adds it to the Task List


To move to the line of code, double-click the new task entry. Notice that if you remove the
comment, the task entry is automatically removed as well.
Three token tags are built-in: HACK, TODO, and UNDONE. However, you can add more. Simply
select Tools ➤ Options. In the Options dialog box, navigate to the Environment ➤ Task List tab. You’ll
see a list of comment tokens, which you can modify, remove, and add to. Figure 2-10 shows this window
with a new ASP comment token that you could use to keep track of sections of code that have been
migrated from classic ASP pages.






Bing'ı Seviyoruz. :)
Google'ı Seviyoruz. :)