Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

5.2CLASS LIBRARIES

Finally, we are ready to specify our album class. We have decided to base this on CollectionBase, and use our own Photograph object for the elements. As we discussed in the previous section, the CollectionBase class provides a limited set of methods, so it will be up to us to implement the appropriate class members to support the required interfaces.

As a result, our PhotoAlbum class will look something like the following. Since this is a photo album and we expect to display photos from it, we will also add some methods to manage the current position within the album.

public class PhotoAlbum : CollectionBase

b Inherit from

{

CollectionBase class

//Default constructor

//The IEnumerable interface is provided by CollectionBase

//This allows the use of foreach with an album

//ICollection members

//IList members

//Position operations

//- Get/Set current position (as index).

//- Get photograph at current position.

//- Move to the next photograph.

//- Move to the previous photograph.

}c End of PhotoAlbum class

Some syntactic points here:

bAs already mentioned, classes in C# support inheritance from a single class only, in this case from the CollectionBase class, although multiple interfaces can be specified. This is the same as Java, and a break from C++. Also unlike the C++ language, C# classes do not support private or protected inheritance.

cIf you haven’t realized it by now, also note that there are no header files in C#. Like Java, the entire class is specified in a single file. For C++ programmers, also note that a semicolon (;) is not required after the class definition.

The Photograph class will hold the original file name for the image, and the Bitmap object when necessary. Its definition will look something like this:

public class Photograph

d Inherit from System.Object

{

 

//Create a new instance from a file name.

//Properties:

//- get the file name for the Photograph

//- get the Bitmap for the Photograph

//Methods:

//- see if two Photographs are equal

}

CLASS LIBRARIES

133

One additional point here:

dIt is worth noting that all classes in C# implicitly inherit from the object class even when it is not specified. This ensures that all classes have a common ancestor. So even though it is not shown, our Photograph class inherits from the base System.Object class implicitly, which is equivalent to the C# object class.

Now that we understand the framework for our classes, let’s perform the actual implementation. This section will create the class library in Visual Studio, discuss creating such a library using the command line tools, and provide the initial implementation of our PhotoAlbum and Photograph classes.

5.2.1CREATING THE CLASS LIBRARY

Enough preparation: time to create our library. If you are not using Visual Studio

.NET here, create your library as a separate directory and place the files discussed here in it. We’ll give you some hints for building this from the command line later in the chapter.

In this section we will create a new project as part of our MyPhotos solution. This project will build the new MyPhotoAlbum library. We will create a top-level namespace called Manning for this project, and reference the new library from our MyPhotos project.

Set the version number of the application to 5.2.

CREATE A REUSABLE LIBRARY IN VISUAL STUDIO .NET

Action

Result

1Add a new project to the MyPhotos solution.

How-to

a.Click the File menu in Visual Studio .NET.

b.Click on the Add Project menu.

c.Select the New Project… item.

134

CHAPTER 5 REUSABLE LIBRARIES

CREATE A REUSABLE LIBRARY IN VISUAL STUDIO .NET (continued)

 

Action

Result

 

 

 

2

Configure the new project

 

 

as a class library named

 

 

“MyPhotoAlbum.”

 

 

How-to

 

 

a. Select Visual C# Projects

 

 

as the Project Type.

 

 

b. Select Class Library as

 

 

the Template.

 

 

c. Enter “MyPhotoAlbum”

 

 

for the name of the

 

 

project.

 

 

 

 

3

Click the OK button to

In the Solution Explorer window, the new project appears with

 

create the new project.

a default initial class named Class1. The main window displays

 

 

the Class1.cs source file.

 

 

Note: The MyPhotos project is in bold to indicate that it

 

 

is the default project, or the startup project in Visual Stu-

 

 

dio .NET terms.

 

 

 

That’s all it takes. The solution MyPhotos now contains two projects: a MyPhotoAlbum project to create a DLL library, and a MyPhotos project to create a Windows Forms application. You will note that the new project has its own AssemblyInfo.cs file to support an independent version number for the library.

CLASS LIBRARIES

135

We do not want a class called Class1, so let’s rename it to PhotoAlbum. We will also adjust the version number of our new project to reflect the current section number.

 

RENAME THE CLASS1.CS CLASS FILE

 

 

 

 

Action

Result

 

 

 

4

Set the MyPhotoAlbum

When you compile the MyPhotoAlbum library, the new version

 

version number to 5.2.

number is included, and will be visible when displaying the

 

How-to

properties for the generated library assembly.

 

 

 

a. Double-click the Assem-

Note: Your main window now displays two Assembly-

 

blyVersion.cs file.

Info.cs tabs for the corresponding files in each project.

 

b. Modify the Assembly-

Make sure you keep track of which is which. The dis-

 

Version line to contain

played file is always selected in the Solution Explorer win-

 

the desired version

dow, which identifies the project that contains the file. To

 

number.

display the Solution Explorer window while editing a file,

 

 

use the keyboard shortcut Ctrl+Alt+L.

 

 

 

5

Rename the Class1.cs file

The Class1.cs tab in the main window is renamed as well.

 

name to PhotoAlbum.cs.

 

 

How-to

 

 

a. Right-click on the

 

 

Class1.cs file.

 

 

b. Select Rename.

 

 

c. Enter “PhotoAlbum.cs”

 

 

for the file name.

 

 

 

 

6

Rename the Class1 class

The PhotoAlbum.cs file should look like this:

 

name to PhotoAlbum.

using System;

 

 

 

How-to

namespace MyPhotoAlbum

 

a. Double-click the PhotoAl-

 

{

 

bum.cs file.

/// <summary>

 

b. Change the three

/// Summary description for PhotoAlbum.

 

/// </summary>

 

instances of “Class1” to

 

public class PhotoAlbum

 

“PhotoAlbum” in the

{

 

code.

public PhotoAlbum()

 

 

{

 

 

//

 

 

// TODO: Add Constructor Logic here

 

 

//

 

 

}

 

 

}

 

 

}

 

 

 

Visual Studio automatically uses the project name as the namespace for all files in the project. Here, the PhotoAlbum class is in the MyPhotoAlbum namespace, so that our class called PhotoAlbum will not interfere with anyone else who may have a class called PhotoAlbum. By convention, namespaces should specify the company name, followed by the project name. Since our library might be used outside of this book (hey, you never know!), we should follow this convention as well. We will use the publisher’s name Manning as our top-level namespace.

136

CHAPTER 5 REUSABLE LIBRARIES