1
Answer

Explain code from C# how to program book

Tuan Tran

Tuan Tran

12y
1.3k
1
In "C# How to Program" by Deitel, a code generated errors in VS2010

it pointed out that error in "Property hour"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TimeTest3
{
    public class Time3
    {
        private int hour;
        private int minute;
        private int second;

        public Time3()
        {
            SetTime(0, 0, 0);
        }

        public Time3(int hour)
        {
            SetTime(hour, 0, 0);
        }

        public Time3(int hour, int minute)
        {
            SetTime(hour, minute, 0);
        }

        public Time3(int hour, int minute, int second)
        {
            SetTime(hour, minute, second);
        }

        //Copy Constructor
        public Time3(Time3 time)
        {
            SetTime(time.hour, time.minute, time.second);
        }

        public void SetTime(int h,int m,int s)
        {
            hour = h;
            minute = m;
            second = s;
        }

        //Property hour
        public int hour
        {
            get
            {
                return hour;
            }
            set
            {
                hour = ((value >= 0 && value < 24) ? value : 0);
            }
        }


    }
}

and here are what the book says: "Lines 57–69, 72–84 and 87–99 define Time3 properties Hour, Minute and Second,
respectively. Each property begins with a declaration line that includes the property's access
modifier (public), type (int) and name (Hour, Minute or Second).
The body of each property contains get and set accessors, which are declared using
the reserved words get and set. The get accessor declarations are on lines 59–62, 74–77
and 89–92. These accessors return the hour, minute and second instance variable values
that objects request. The set accessors are declared on lines 64–67, 79–82 and 94–97. The
body of each set accessor performs the same conditional statement that was previously performed
by method SetTime to set the hour, minute or second."

I don't understand. Why the book said that, but in VS2010 is error?

Answers (1)