Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

10

Part I

INTRODUCING VC++.NET

the corresponding namespace(s). One significant advantage of using namespaces is that you can use classes of the same name, provided they belong to different namespaces.

The .NET Framework uses a dot to separate the name of the namespace and the types within, and thereby clearly depicts the hierarchy. Consider the statement System.Console. In this, Console represents a class (a type) while System is a namespace. In the .NET Framework class library, the System namespace is at the top of the hierarchy and contains classes, such as Console and Array, interfaces, and structures.

Table 1-1 describes some of the namespaces provided by the .NET Framework, and

Table 1-2 describes some of the important classes of the System namespace.

 

 

 

 

 

Y

 

 

 

 

L

Table 1-1 Namespaces in the Class Library

 

 

 

 

F

Namespace

 

 

 

Used to

 

 

 

 

 

Microsoft.Csharp

 

 

Compile and generate code in Visual C# .NET.

 

 

 

 

M

 

Microsoft.Jscript

 

 

Compile and generate code in JScript.

 

 

 

A

 

Microsoft.VisualBasic

 

Compile and generate code in Visual Basic .NET.

 

 

E

 

Microsoft.Win32

T

Manipulate the system registry and manage operating system

 

 

 

 

 

 

 

events.

 

System

 

 

 

Manage exceptions and define data types, events and event

 

 

 

 

handlers, interfaces, and attributes.

 

Table 1-2 Classes of the System Namespace

 

 

 

 

 

 

Class

 

 

 

Description

 

 

 

 

 

 

System.Array

 

 

 

The base class for all arrays in the CLR and provides methods

 

 

 

 

for managing arrays in your application.

System.Console

 

 

 

Provides methods to handle standard input, output, and error

 

 

 

 

messages in console-based applications.

System.Convert

 

 

 

Provides methods to convert data types from one type to

 

 

 

 

another.

 

System.String

 

 

 

Enables you to handle strings in your application. Once creat-

 

 

 

 

ed, a string can’t be modified.

System.Math

 

 

 

Provides you with all arithmetic, trigonometric, and logarith-

 

 

 

 

mic functions.

 

 

 

 

 

 

 

Team-Fly®

BASICS OF .NET FRAMEWORK

Chapter 1

11

 

 

 

Moving on, some of the pertinent features of the .NET Framework, including assemblies, debuggers, and security-related features, will be discussed next.

Assemblies

Assemblies are one of the main features of a .NET application. An assembly is a collection of types, such as classes, and other resources that function together to achieve the required functionality. In addition, an assembly also stores an assembly manifest that contains the metadata describing the contents of the assembly. The assembly manifest also stores the version of the assembly and the version of all other dependent assemblies. In short, the manifest is a description of the assembly itself. Some of the functionalities of an assembly are as follows:

An assembly contains code that is executed by the CLR.

An assembly manifest is associated with the portable executable (PE) file containing the MSIL code. Without the manifest, the MSIL code can’t be executed.

The assembly metadata within the assembly manifest file is used to implement types.

Assemblies also help you in resolving version-control problems. An assembly, along with the constituent types and resources, forms the smallest unit that can be versioned.

An assembly may be available in a single file or in multiple files. Assemblies may be either static or dynamic. Static assemblies are stored on disk in the PE file. Dynamic assemblies, on the other hand, are executed directly from memory. These can be saved to disk only after execution.

An assembly also may be either private or shared. As the name indicates, a private assembly is available only for the application for which you created it, whereas shared assemblies can be accessed by more than one application. To use a private assembly, you simply need to copy it to the application’s directory (the directory in which you copied your application). However, to share an assembly, you need to place it in the global assembly cache (GAC), a common repository of the shared assemblies.

Assemblies also have a side-by-side feature that enables you to run multiple versions of an assembly simultaneously. Code that supports side-by-side execution also provides support for backward compatibility, because the assembly required for the previous version can now coexist with the new assembly.

12

Part I

INTRODUCING VC++.NET

What makes this side-by-side feature work? When you execute an application, it searches for the version of the assembly used by the application. The version details are built into the assembly itself. This helps the runtime environment to trace and load the appropriate assembly while executing the application.

Debugger

The .NET Framework provides a common debugger for all .NET languages. Debugging is no longer language-dependent. The following are some of the significant features of the debugger:

Cross-language debugging

Debugging support for both .NET applications (targeted for the CLR) and Win32 applications

Debugging support for multiple programs simultaneously

Remote debugging

Code Access Security

Now that the entire world is connected through the Internet, the security risks have also increased. With the increased exposure to code from various sources that are sometimes unknown, the security needs have also increased. The .NET Framework addresses this by supporting a security mechanism called code access security (CAS). CAS allows you to set permissions and a trust level based on the code’s origin. It also allows you to specify the operations that a code can and cannot perform. Any application aimed to run in the CLR has to pass through CAS before execution.

You have gone through the .NET Framework and two of its main components. Now you will be introduced to Visual Studio .NET and some of its salient features.

Introduction to Visual Studio .NET

Visual Studio .NET is the latest version of Visual Studio and has been designed to work based on the .NET Framework. This is a development platform that provides you with multiple languages, including Visual Basic .NET, Visual C++

.NET, Visual C# .NET, and Visual FoxPro, and tools required to build Windows

BASICS OF .NET FRAMEWORK

Chapter 1

13

 

 

 

applications, Web applications and services, and mobile applications. The two most significant advantages of Visual Studio .NET are as follows:

All Visual Studio .NET languages have the same integrated development environment (IDE). This is unlike the previous version, Visual Studio 6.0, in which Visual Basic and Visual C++ had an IDE different from that of the other languages.

Programming in ASP.NET, which provides some advanced capabilities such as efficient database access, is made easier. You can code, compile, and debug your ASP.NET application in Visual Studio .NET itself.

First you will learn about the IDE of Visual Studio .NET, and then proceed to learn about some of the new and enhanced features.

The Visual Studio .NET IDE

The various components of the IDE, shown in Figure 1-2, simplify the tasks you perform in the Visual Studio .NET environment. Some of the components are as follows:

Start Page. The first screen that appears when you launch Visual Studio

.NET. This is set as the home page for the Visual Studio .NET IDE. You can use this page to create new projects or to open existing ones. The Start Page also allows you to search for information on MSDN online and to customize the IDE.

Solution Explorer. Contains all the projects that you create and their associated files. It displays the information in a tree view, thus easing the navigation.

Server Explorer. Helps you to connect to servers, such as SQL Server, and other databases. It also helps you to gain access to the available XML Web services on a server. The Data Connection node of Server Explorer lists all database connections, and the Servers node lists the servers that your application can use.

Toolbox. Contains all controls used to design an application. The controls are available in groups, such as Windows Forms controls and Data controls. One significant enhancement is that you can store code snippets in the toolbox simply by selecting and dragging it to the toolbox.

Task List and Output window. The IDE provides a single window with two tabs, Task List and Output. When you click Task List, the Task List

14

Part I

INTRODUCING VC++.NET

window is activated. This window helps you to mark your application’s code with comments, which are then displayed in a tabular format. The Output window displays the errors and warnings when you compile your application.

Properties window. Displays the characteristics associated with an object, such as a Text Box control on a form.

Class View window. Lists the classes, methods, and properties associated with a project.

Code and Text Editor. Enables you to enter and edit the code of your application.

FIGURE 1-2 The IDE of Visual Studio .NET

The following sections describe the language-based enhancements and changes.

Visual C# .NET

The Visual .NET suite provides a new object-oriented language, Visual C#

.NET. Visual C# is a combination of the best features of the most prominent languages, such as Visual C++ and Visual Basic. Visual C# allows you to create both managed and unmanaged applications.

BASICS OF .NET FRAMEWORK

Chapter 1

15

 

 

 

Visual Basic .NET

Visual Basic undoubtedly is the easiest programming language available to date, a language that is accepted readily and equally by both novices and experienced programmers. Visual Basic is one of the languages in the .NET suite that has undergone some phenomenal changes. The most significant change is that Visual Basic

.NET is now purely object-oriented and hence supports object-oriented features, such as inheritance and overloading.

Visual C++ .NET

Visual C++ .NET is an enhanced version of Visual C++ 6.0. The enhancements are really noteworthy. Visual C++ .NET is the only language that allows you to create applications that are not compatible with the .NET Framework. This implies that you can still create the traditional Windows applications (using MFC) the same way as you did earlier. This is the only language that offers this feature! Then what is new about the .NET version of the language? Well, apart from some enhancements to the existing features, new features including managed extensions and attribute programming have been added. Managed extensions enable you to create .NET-com- patible applications; in other words, you can create managed applications. Attributed programming significantly simplifies the creation of COM components. Figure 1-3 depicts the support for both managed and unmanaged code.

 

 

 

 

 

 

 

 

 

 

 

 

Unmanaged applications

 

 

 

Managed applications

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ATL Server applications

 

 

 

 

Web services

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MFC applications

 

 

 

 

Web applications

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.NET Framework

Windows API

FIGURE 1-3 Support for both managed and unmanaged code in Visual C++ .NET

16

Part I

INTRODUCING VC++.NET

Using the previously mentioned languages of Visual Studio .NET, you can develop different types of applications, including console applications, Windows applications, Web applications, and Web services. For developing .NET Frame- work–based Windows applications, the Visual Studio.NET provides Windows forms, and for developing Web-based applications, it provides Web forms. The following sections provide you with an overview of Windows forms, Web forms, and Web services.

Windows Forms

Windows Forms provide an object-oriented platform for developing Windows applications based on the .NET Framework. You can use Windows Forms controls to design a Windows application. Windows applications typically run on stand-alone computers.

Web Forms

Visual Studio .NET introduces Web Forms, which are based on Microsoft ASP.NET technology. Web Forms are used to create Web applications that are further deployed on the Web server and then accessed through a browser. Visual Studio .NET provides the ASP.NET Web Application project template to create Web applications. This template has inherent support for Web forms that you can use to create the user interface.

Web Services

Web Services are used to exchange data between applications by using the Extensible Markup Language (XML). Web services, unlike Windows and Web applications, don’t have any user interface. You can

XML use Visual Basic .NET, Visual C# .NET, or ATL Server to create Web Services.

Visual Studio .NET includes built-in support for XML, a markup language based on the Standard Generalized Markup Language (SGML), which is a standard for all markup languages. XML enables you to define the structure of data by using markup tags. Besides this, you can also define new tags using XML.

You are now familiar with the .NET Framework, the .NET Visual Studio, and the various .NET languages. The following section briefly describes the new and enhanced features of Visual C++ .NET and also highlights the significant differences between Visual C++ .NET and Visual 6.0.

BASICS OF .NET FRAMEWORK

Chapter 1

17

 

 

 

Enhanced and New Features of

VC++.NET

Almost all languages in the .NET suite have undergone phenomenal changes. Visual Basic .NET is one such language that has changed radically. Comparatively, Visual C++ .NET has undergone fewer changes, but these changes have resulted in an increased level of productivity to the language. Some of the enhanced features of Visual C++ .NET are the following:

Shared classes. In Visual C++ 6.0, if you had to use MFC classes, such as CRect, in an ATL Server project, you had to add appropriate MFC dependencies. However, in Visual C++ .NET, such common MFC classes are shared and no longer are MFC-dependent.

Unified event handling. In Visual C++ 6.0, the implementation of event handling differed for native and COM classes, whereas Visual C++

.NET follows the same event-handling method for native code (C++), COM, and managed code.

Enhanced MFC class library. Some new MFC classes have been introduced that help you create windowless controls and edit DHTML (Dynamic Hypertext Markup Language) components. The improved MFC class library also provides support for enhanced use of HTML help in an MFC application. Besides this, MFC classes now use static casting to have more control on the return types of message handlers. For instance, in Visual C++ 6.0, you can code a member function to return “void” instead of “LPRESULT” and compile such code successfully with no compilation errors; in Visual C++ .NET, this is not possible.

Integrated debugger. As mentioned earlier, Visual C++ .NET applications also use the common debugger provided by the Visual Studio

.NET.

Class View. Class View enables you to navigate to the classes, functions, and member variables in your application easily by listing them in a treelike structure. Also, in Class View, you can access wizards to add member classes and variables.

Removal of the Class Wizard and WizardBar are removed from VC++

.NET. To add a class, you need to use the Add Class Wizard. In addition, to add member variables and member functions, you use the Add Variable and Add Function Wizards, respectively.

18

Part I

INTRODUCING VC++.NET

Removal of the New Database Wizard and the ability to export an NMAKE makefile.

Apart from these enhancements, some new features have been added to Visual C++ .NET. A brief description of these features follows:

Managed extensions for C++. Managed extensions bring VC++.NET into the scope of the .NET Framework. You use managed extensions to create managed applications that can target the CLR and thereby gain from the features of the CLR. In addition, you can use managed extensions to migrate an existing C++ application to the .NET Framework.

Attribute based programming. Attributes are new to Visual C++ and are designed to simplify COM programming. An attribute is just a declaration that, when compiled, is replaced by the appropriate code.

ATL Server. Besides the ATL class library that was available in Visual C++ 6.0, Visual C++ .NET now also supports ATL Server, which enables you to create Web applications, Web services (using XML), and other server applications.

New Build events. New events, such as Pre-Build, Pre-Link, and PostBuild, have been added to customize the build process. These events enable you to specify actions that you need to perform at any stage during the build time of the project.

Summary

This chapter introduced the .NET Framework and its main components, the CLR and class library. The CLR is the common runtime environment provided by the .NET Framework for all .NET languages. The CLR’s components, the CTS and CLS, contribute to the language interoperability and application interoperability features provided by the .NET Framework. The CLR also provides a Garbage Collector that contributes to the increased efficiency of memory handling in the .NET Framework. You also learned about the role of assemblies in the .NET Framework.

The chapter also gave you a glimpse of the new and improved Visual Studio

.NET’s IDE. Finally, you learned about the enhanced and new features of Visual C++ .NET and the differences between VC++ 6.0 and VC++ .NET.

Chapter 2

Basics of

VC++.NET

Programming