1. Introduction

Objectives

In this chapter you’ll learn:

• The history of the Visual C# programming language.

• Some basics of object technology.

• The importance of XML as a data-representation scheme.

• About Microsoft’s .NET initiative, which involves the Internet in developing and using software systems.

• About the .NET Framework and the Common Language Runtime.

• To test-drive a Visual C# 2010 drawing application.

The chief merit of language is clearness.

Galen

Our life is frittered away with detail. . . . Simplify, simplify.

Henry David Thoreau

Man is still the most extraordinary computer of all.

John F. Kennedy

Outline

1.1 Introduction

1.2 Microsoft’s Windows® Operating System

1.3 C, C++, Objective-C and Java

1.4 C#

1.5 Extensible Markup Language (XML)

1.6 Introduction to Microsoft .NET

1.7 The .NET Framework and the Common Language Runtime

1.8 Test-Driving the Advanced Painter Application

1.9 Introduction to Object Technology

1.10 Wrap-Up

1.1 Introduction

Welcome to Visual C# 2010, one of the world’s fastest-growing object-oriented programming languages! We hope that working with C# 2010 for Programmers, 4/e will be an informative, challenging and entertaining learning experience for you.

Perhaps most important, the book presents hundreds of complete, working C# programs and depicts their inputs and outputs. We call this the live-code approach. You can download all the book’s source code from www.deitel.com/books/csharpfp4/.

As you proceed, if you have any questions, send an e-mail to

[email protected]

To keep current with C# developments at Deitel & Associates and to receive updates to this book, subscribe to our e-mail newsletter, the Deitel® Buzz Online, at

www.deitel.com/newsletter/subscribe.html

check out our growing list of C# and related Resource Centers at

www.deitel.com/ResourceCenters.html

and follow us on Twitter (@deitel) and Facebook (www.deitel.com/deitelfan).

1.2 Microsoft’s Windows® Operating System

Microsoft became the dominant software company in the 1980s and 1990s. In the mid-1980s, Microsoft developed the Windows operating system, consisting of a graphical user interface built on top of DOS (a personal computer operating system that users interacted with by typing commands). The Windows operating system became incredibly popular after the 1993 release of Windows 3.1, whose successors, Windows 95 and Windows 98, virtually cornered the desktop operating systems market by the late 1990s. These operating systems, which borrowed from many concepts (such as icons, menus and windows) popularized by early Apple Macintosh operating systems, enabled users to work with multiple applications simultaneously. Microsoft entered the corporate operating systems market with the 1993 release of Windows NT. Windows XP was released in 2001 and combined Microsoft’s corporate and consumer operating system lines. Windows Vista, released in 2007, offered the attractive new Aero user interface, many powerful enhancements and new applications. A key focus of Windows Vista was enhanced security. Windows 7 is Microsoft’s latest operating system—its features include enhancements to the Aero user interface, faster startup times, further refinement of Vista’s security features, touch screen and multi-touch support, and more. This book is intended for Windows XP, Windows Vista and Windows 7 users. Windows is by far the world’s most widely used operating system.

1.3 C, C++, Objective-C and Java

The C programming language was developed in the early 1970s. C first gained widespread recognition as the development language of the UNIX operating system. C is a hardware-independent language, and, with careful design, it’s possible to write C programs that are portable to most computers.

C++, developed in the early 1980s, provides several features that “spruce up” the C language, and, more importantly, capabilities for object-oriented programming (OOP). Objects are reusable software components that model items in the real world. A modular, object-oriented approach to design and implementation can make software development groups much more productive than is possible using earlier programming techniques.

The Objective-C programming language, also developed in the early 1980s, added capabilities for object-oriented programming (OOP) to the C programming language. It eventually became the software development language for Apple’s Macintosh. Its use has exploded as the app development language for Apple’s popular iPod, iPhone and iPad consumer devices.

Microprocessors are having a profound impact in intelligent consumer electronic devices. Recognizing this, Sun Microsystems in 1991 funded an internal corporate research project that resulted in the development of a C++-based language, which Sun eventually called Java. As the World Wide Web exploded in popularity in 1993, Sun saw the possibility of using Java to add dynamic content (for example, interactivity, animations and the like) to web pages. Sun announced the language in 1995. This generated immediate interest in the business community because of the commercial potential of the web. Java is now used to develop large-scale enterprise applications, to enhance the functionality of web servers, to provide applications for consumer devices (such as cell phones, pagers and smartphones) and for many other purposes.

1.4 C#

In 2000, Microsoft announced the C# (pronounced “C-Sharp”) programming language—created specifically for the .NET platform (discussed in Sections 1.61.7). C# has roots in C, C++ and Java. Like Visual Basic, C# is object oriented and has access to the powerful .NET Framework Class Library—a vast collection of prebuilt components, enabling you to develop applications quickly. Both languages have similar capabilities to Java and are appropriate for the most demanding application development tasks, especially for building today’s enterprise applications, and web-based and mobile applications.

C# is object oriented—you’ll learn some basics of object technology shortly and will study a rich treatment later in the book. C# is event driven—you’ll write programs that respond to user-initiated events such as mouse clicks, keystrokes and timer expirations. Microsoft’s Visual C# is indeed a visual programming language—in addition to writing program statements to build portions of your applications, you’ll also use Visual Studio’s graphical user interface to conveniently drag and drop predefined objects like buttons and textboxes into place on your screen, and label and resize them. Visual Studio will write much of the GUI code for you.

C# has been standardized internationally, enabling other implementations of the language besides Microsoft’s Visual C#, such as Mono (www.mono-project.com).

1.5 Extensible Markup Language (XML)

As the popularity of the web exploded, HTML’s limitations became apparent. HTML’s lack of extensibility (the ability to change or add features) frustrated developers, and its ambiguous definition allowed erroneous HTML to proliferate. The need for a standardized, fully extensible and structurally strict language was apparent. As a result, XML was developed by the W3C.

Data independence, the separation of content from its presentation, is the essential characteristic of XML. Because XML documents describe data, any application conceivably can process them. Software developers are integrating XML into their applications to improve web functionality and interoperability.

XML isn’t limited to web applications. For example, it’s increasingly used in data-bases—an XML document’s structure enables it to be integrated easily with database applications. As applications become more web enabled, it’s likely that XML will become the universal technology for data representation. Applications employing XML would be able to communicate with one another, provided that they could understand their respective XML markup schemes, or vocabularies. Microsoft’s .NET technologies use XML to mark up and transfer data over the Internet, and to enable software components to interoperate.

1.6 Introduction to Microsoft .NET

In 2000, Microsoft announced its .NET initiative (www.microsoft.com/net), a broad new vision for using the Internet and the web in the development, engineering, distribution and use of software. Rather than forcing developers to use a single programming language, the .NET initiative permits developers to create .NET applications in any .NET-compatible language (such as C#, Visual Basic, and others). Part of the initiative includes Microsoft’s ASP.NET technology, which allows you to create web applications.

The .NET strategy extends the idea of software reuse to the Internet by allowing you to concentrate on your specialties without having to implement every component of every application. Visual programming (which you’ll learn throughout this book) has become popular because it enables you to create Windows and web applications easily, using such prepackaged controls as buttons, textboxes and scrollbars.

The .NET Framework is at the heart of Microsoft’s .NET strategy. This framework executes applications, includes the .NET Framework Class Library and provides many other programming capabilities that you’ll use to build C# applications.

1.7 The .NET Framework and the Common Language Runtime

The details of the .NET Framework are found in the Common Language Infrastructure (CLI), which contains information about the storage of data types (that is, data that has predefined characteristics such as a date, percentage or currency amount), objects and so on. The CLI has been standardized, making it easier to implement the .NET Framework for other platforms. This is like publishing the blueprints of the framework—anyone can build it by following the specifications.

The Common Language Runtime (CLR) is the central part of the .NET Frame-work—it executes .NET programs. Programs are compiled into machine-specific instructions in two steps. First, the program is compiled into Microsoft Intermediate Language (MSIL), which defines instructions for the CLR. Code converted into MSIL from other languages and sources can be woven together by the CLR. The MSIL for an application’s components is placed into the application’s executable file. When the application executes, another compiler (known as the just-in-time compiler or JIT compiler) in the CLR translates the MSIL in the executable file into machine-language code (for a particular platform), then the machine-language code executes on that platform.

If the .NET Framework is installed on a platform, that platform can run any .NET program. A program’s ability to run (without modification) across multiple platforms is known as platform independence. Code written once can be used on another type of computer without modification, saving time and money. Software can also target a wider audience—previously, companies had to decide whether converting their programs to different platforms (sometimes called porting) was worth the cost. With .NET, porting programs is no longer an issue (at least once .NET itself has been made available on the platforms).

The .NET Framework also provides a high level of language interoperability. Programs written in different languages (for example, C# and Visual Basic) are all compiled into MSIL—the different parts can be combined to create a single unified program. MSIL allows the .NET Framework to be language independent, because .NET programs are not tied to a particular programming language.

The .NET Framework Class Library can be used by any .NET language. The library contains a variety of reusable components, saving you the trouble of creating new components. This book explains how to develop .NET software with C#.

1.8 Test-Driving the Advanced Painter Application

In this section, you’ll “test-drive” an existing application that enables you to draw on the screen using the mouse. The Advanced Painter application allows you to draw with different brush sizes and colors. The elements and functionality you see in this application are typical of what you’ll learn to program in this text. The following steps show you how to test-drive the application. You’ll run and interact with the working application.

  1. Checking your setup. Confirm that you’ve set up your computer properly by reading the Before You Begin section located after the Preface.
  2. Locating the application directory. Open a Windows Explorer window and navigate to the C:examplesch01 directory (Fig. 1.1)—we assume you placed the examples in the C:examples folder.

    Fig. 1.1. Contents of C:examplesch01.

    image

  3. Running the Advanced Painter application. Now that you’re in the proper directory, double click the file name AdvancedPainter.exe (Fig. 1.1) to run the application (Fig. 1.2). [Note: Depending on your system configuration, Windows Explorer might not display file name extensions. To display file name extensions (like .exe in Fig. 1.1), type Alt + T in Windows Explorer to open the Tools menu, then select Folder options.... In the Folder Options window, select the View tab, uncheck Hide extensions for known file types and click OK.]

    Fig. 1.2. C# Advanced Painter application.

    image

    Figure 1.2 labels several graphical elements—called controls. These include GroupBoxes, RadioButtons, a Panel and Buttons (these controls and many others are discussed in depth throughout the text). The application allows you to draw with a red, blue, green or black brush of small, medium or large size. You can also undo your previous operation or clear the drawing to start from scratch.

    By using existing controls—which are objects—you can create powerful applications much faster than if you had to write all the code yourself.

    The brush’s properties, selected in the RadioButtons labeled Black and Medium, are default settings—the initial settings you see when you first run the application. Programmers include default settings to provide reasonable choices that the application will use if the user chooses not to change the settings. Default settings also provide visual cues for users to choose their own settings. Now you’ll choose your own settings as a user of this application.

  4. Changing the brush color and size. Click the RadioButton labeled Red to change the color of the brush, then click Small to change the size of the brush. Position the mouse over the white Panel, then press and hold down the left mouse button to draw with the brush. Draw flower petals, as shown in Fig. 1.3.

    Fig. 1.3. Drawing with a new brush color.

    image

  5. Changing the brush size. Click the RadioButton labeled Green to change the color of the brush again. Then, click the RadioButton labeled Large to change the size of the brush. Draw grass and a flower stem, as shown in Fig. 1.4.

    Fig. 1.4. Drawing with a new brush size.

    image

  6. Finishing the drawing. Click the Blue and Medium RadioButtons. Draw raindrops, as shown in Fig. 1.5, to complete the drawing.

    Fig. 1.5. Finishing the drawing.

    image

  7. Closing the application. Close your running application by clicking its close box, image (Fig. 1.5).

1.9 Introduction to Object Technology

When object-oriented programming became widely used in the 1980s and 1990s, it dramatically improved the software development process. What are objects, and why are they special? Object technology is a scheme for creating meaningful software units. There are date objects, time objects, paycheck objects, invoice objects, automobile objects, people objects, audio objects, video objects, file objects, record objects and so on. On your computer screen, there are button objects, textbox objects, menu objects and many more. In fact, almost any noun can be reasonably represented as a software object. Objects have attributes (also called properties), such as color, size, weight and speed; and perform actions (also called methods or behaviors), such as moving, sleeping or drawing.

Classes are types of related objects. For example, all cars belong to the “car” class, even though individual cars vary in make, model, color and options packages. A class specifies the attributes and actions available to an object of its class. An object is related to its class in much the same way as a building is related to the blueprint from which the building is constructed. Contractors can build many buildings from the same blueprint; programmers can instantiate (create) many objects from the same class.

With object technology, properly designed classes can be reused on future projects. Some organizations report that another key benefit they get from object-oriented programming is the production of software that’s better organized and has fewer maintenance requirements than software produced with earlier technologies.

Object orientation allows you to focus on the “big picture.” Instead of worrying about the details of how reusable objects are implemented, you focus on the behaviors and interactions of objects. A road map that showed every tree, house and driveway would be difficult to read. When such details are removed and only the essential information (roads) remains, the map becomes easier to understand. In the same way, an application that is divided into objects is easy to understand, modify and update because it hides much of the detail.

It’s clear that object-oriented programming will be the key programming methodology for the next several decades. C# is one of the world’s most widely used object-oriented languages, especially in the Microsoft software development community.

Basic Object-Technology Concepts

Object-oriented design (OOD) models software in terms similar to those that people use to describe real-world objects. It takes advantage of class relationships, where objects of a certain class, such as a class of vehicles, have the same characteristics—cars, trucks, little red wagons and roller skates have much in common. OOD takes advantage of inheritance relationships, where new classes of objects are derived by absorbing characteristics of existing classes and adding unique characteristics of their own. An object of class “convertible” certainly has the characteristics of the more general class “automobile,” but more specifically, the roof goes up and down.

Object-oriented design provides a natural and intuitive way to view the software design process—namely, modeling objects by their attributes, behaviors and interrelationships, just as we describe real-world objects. OOD also models communication between objects. Just as people send messages to one another (for example, a sergeant commands a soldier to stand at attention), objects also communicate via messages. A bank-account object may receive a message to decrease its balance by a certain amount because the customer is withdrawing that amount of money.

OOD encapsulates (that is, wraps) attributes and operations (behaviors) into objects—an object’s attributes and operations are intimately tied together. Objects have the property of information hiding. Objects may know how to communicate with one another across well-defined interfaces, but normally they’re not allowed to know how other objects are implemented—such details are hidden within the objects themselves. You can drive a car effectively, for example, without knowing the details of how engines, transmissions, brakes and exhaust systems work internally—as long as you know how to use the accelerator pedal, the brake pedal, the steering wheel and so on. Information hiding, as you’ll see, is crucial to good software engineering.

Classes, Fields and Methods

As a C# programmer, you’ll concentrate on creating your own classes. Each class contains data as well as the set of methods that manipulate that data and provide services to clients (that is, other classes that use the class). The data components of a class are called attributes or fields. For example, a bank account class might include an account number and a balance. The operation components of a class are called methods. For example, a bank-account class might include methods to make a deposit (increase the balance), make a withdrawal (decrease the balance) and inquire what the current balance is.

Classes are to objects as blueprints are to houses—a class is a “plan” for building objects of the class. Just as we can build many houses from one blueprint, we can instantiate (create) many objects from one class. You cannot cook meals in the kitchen of a blueprint, but you can cook meals in the kitchen of a house. Packaging software as classes makes it possible for future software systems to reuse the classes.

Software Engineering Observation 1.1

image

Reuse of existing classes when building new classes and programs saves time, money and effort. Reuse also helps you build better systems, because existing classes and components often have gone through extensive testing, debugging and performance tuning.

You’ll be using existing classes and making objects of those classes throughout the entire book. In Chapter 4, you’ll begin building your own customized classes.

With object technology, you can build much of the new software you’ll need by combining existing classes, just as automobile manufacturers combine standardized interchangeable parts. Each class you create will have the potential to become a valuable software asset that you and other programmers can reuse to speed future software development efforts.

Introduction to Object-Oriented Analysis and Design (OOAD)

To create the best solutions, you should follow a detailed process for determining your project’s requirements (that is, what your system is supposed to do) and developing a design that satisfies them (that is, deciding how your system should do it). Ideally, you would go through this process and carefully review the design (and have your design reviewed by other software professionals) before writing any code. If this process involves analyzing and designing your system from an object-oriented point of view, it’s called object-oriented analysis and design (OOAD). Proper analysis and design can help avoid an ill-planned system development approach that has to be abandoned partway through its implementation, possibly wasting considerable time, money and effort. Although many different OOAD processes exist, a single graphical language known as the UML (Unified Modeling Language) for communicating the results of any OOAD process has come into wide use. We introduce some simple UML diagrams in the early chapters and present a richer treatment in the ATM OOD case study (Chapters 3031).

1.10 Wrap-Up

This chapter introduced basic object-technology concepts, including classes, objects, attributes and behaviors. We discussed the different types of programming languages and some widely used languages. We presented a brief history of Microsoft’s Windows operating system. We discussed the history of the C# programming language and Microsoft’s .NET initiative, which allows you to program Internet and web-based applications using C# (and other languages). You learned the steps for executing a C# application and you test-drove the Advanced Painter C# application that we create later in the book.

In the next chapter, you’ll use the Visual Studio IDE (Integrated Development Environment) to create your first C# application, using the techniques of visual programming. You’ll also become familiar with Visual Studio’s help features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset