Previous Page
Next Page

Implementing an Operator

In the following exercise, you will complete another Microsoft Windows digital clock application. This version of the code is similar to the exercise in Chapter 16, “Delegates and 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, respectively, and they are all structs. However, the application will not yet compile because the Minute struct is not finished. In the first exercise, you will finish the Minute struct by implementing its missing addition operators.

Write the operator+ overloads
  1. Start Microsoft Visual Studio 2005 if it is not already running.

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

  3. In the Code and Text Editor window, open the Clock.cs source 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 seconds variables.

    The tock method looks like this:

    private void tock()
    {
        this.second++;
        if (this.second == 0)
        {
            this.minute++;
            if (this.minute == 0)
            {
                this.hour++;
            }
        }
    }

    The constructors for the Clock class contain the following statement that arranges for this method to be called whenever the tick event of the pulsed field is raised (the pulsed field is a Ticker, that uses a Timer object to generate an event every second, as described in the exercises in Chapter 16.

    this.pulsed.tick += tock;
  5. On the Build menu, click Build Solution.

    The project builds but displays the following error message in the Output pane:

    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 struct:

    if (minute == 0)
    {
        hour++;
    }

    Your next task is to implement this operator for the Minute struct.

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

  7. In the Code and Text Editor window, 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 the following class:

    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 project builds but displays the following error message in the Output pane:

    The operator 'Operators.Minute.operator ==(Operators.Minute, int)' requires a matching 
    operator "!=" to also be defined.

    There is a still an error. The problem 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.

    The completed operator should look exactly like this:

    struct Minute
    {
        ...
        public static bool operator!= (Minute lhs, int rhs)
        {
            return lhs.value != rhs;
        }
        ...
        private int value;
    }
  10. On the Build menu, click Build Solution.

    This time, the application builds without errors.

  11. On the Debug menu, click Start Without Debugging.

    The application runs and displays a digital clock that updates itself every second.

  12. Close the application and return to the Visual Studio 2005 programming environment.


Previous Page
Next Page