Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
17
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

404

Part III Creating Components

Implementing an Operator

In the following exercise, you will complete another digital clock application. This version of

the code is similar to the exercise in Chapter 17, “Interrupting Program Flow and Handling Events.” However, in this version, the delegate method (which is called every second) does not receive the current hour, minute, and second values when the event is raised. Instead, the delegate method keeps track of the time itself by updating three fields, one each for the hour, minute, and second values. The type of these three fields is Hour, Minute, and Second, respec-

tively, and they are all structures. However, the application will not yet compile, because the Minute structure is not finished. In the first exercise, you will finish the Minute structure by

implementing its missing addition operators.

Write the operator+ overloads

1.Start Microsoft Visual Studio 2008 if it is not already running.

2.Open the Operators project, located in the \Microsoft Press\Visual CSharp Step by Step \Chapter 21\Operators folder in your Documents folder.

3.In the Code and Text Editor window, open the Clock.cs file and locate the declarations of the hour, minute, and second fields at the end of the class.

These fields hold the clock’s current time:

class Clock

{

...

private Hour hour; private Minute minute; private Second second;

}

4.Locate the tock method of the Clock class. This method is called every second to update the hour, minute, and second fields.

The tock method looks like this:

private void tock()

{

this.second++;

if (this.second == 0)

{

this.minute++;

if (this.minute == 0)

{

this.hour++;

}

}

}

Chapter 21 Operator Overloading

405

The constructors for the Clock class contain the following statement that subscribes to the tick event of the pulsed field so that this method is called whenever the event is raised. (The pulsed field is a Ticker that uses a DispatcherTimer object to generate an

event every second, as described in the exercises in Chapter 17.)

this.pulsed.tick += tock;

5.On the Build menu, click Build Solution.

The build fails and displays the following error message:

Operator ‘==’ cannot be applied to operands of type ‘Operators.Minute’ and ‘int’.

The problem is that the tock method contains the following if statement, but the appropriate operator== is not declared in the Minute structure:

if (minute == 0)

{

hour++;

}

Your first task is to implement this operator for the Minute structure.

6.In the Code and Text Editor window, open the Minute.cs file.

7.In the Minute structure, implement a version of operator== that accepts a Minute as its left-hand operand and an int as its right-hand operand. Don’t forget that the return type of this operator should be a bool.

The completed operator should look exactly as shown in bold here:

struct Minute

{

...

public static bool operator==(Minute lhs, int rhs)

{

return lhs.value == rhs;

}

...

private int value;

}

8.On the Build menu, click Build Solution.

The build fails again and displays a different error message:

The operator ‘Operators.Minute.operator ==(Operators.Minute, int)’ requires a matching operator “!=” to also be defined.

The problem now is that you have implemented a version of operator== but have not implemented its required operator!= partner.

9.Implement a version of operator!= that accepts a Minute as its left-hand operand and an int as its right-hand operand.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]