Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone!

i am Reading code of C# from the PDF File Please Tell me Why we "Set" Set Accessors As Private While Get Method Are Set A Public.Please tell me Reason,

CODE

C#
class DateClass
    {
        private int Day;
        private int Month;
        private int Year;
        public int year
        {
            get { return Year;}
            private set
            {
                if (Year < 0 || Year <= 1970 || Year >= 2051)
                {
                    throw new ArgumentOutOfRangeException(
                    "Year", Year, "Outof Range From the Current Program");
                }
                else
                {
                    Year = value;
                }
            }
        }
         public DateClass ( int D, int M, int Y )
        {
            Day = D;
            Month = M;
            Year = Y;
        }
        /// <summary>
        /// Set Month And Check The Condition
        /// </summary>
        public int SetMonth
        {
            get
            {
                return Month;
            }
            private set
            {
                if (Month > 0 && Month < 12)
                {
                    Month = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                        "Month", value, "Month Must Be Between (1-12)");
                }
            }
        }
        public int SetDay
        {
            get
            {
                return Day;
            }
            private set
            {
                int[] DayArray={0,31,28,31,30,31,30,31,31,30,31,30,31};
                if (value > 0 && value <= DayArray[Month])
                {
                    Day = value;
                }
                else if (Month == 2 && Day == 29 && (Year % 400 == 0 || Year % 4 == 0 && year % 100 != 0))
                {
                    Day = value;
                }
                else
                    throw new ArgumentOutOfRangeException(
                        "Day ", Day, "day out of range of current month && Year");
            }
        }
Posted

This way the class consumers can set the date only via the constructor. This is a legitimate design choice making every DateClass instance a de-facto const.

[update]
As Sergey correctly pointed out, I overlooked setters are never used in the class. That makes them useless and the class overall design is bad.
[/update]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Oct-14 8:24am    
Probably you did not pay attention that the private setters are never used in the shown class. It makes the whole class badly wrong. Besides, "only via the constructor" is just not true. How about, for example, static factory methods? how about other cases?

Please see my answer.

—SA
CPallini 5-Oct-14 4:28am    
Yes, you are right, thank you for pointing it out. Setters are useless in such a class.
'Only via constructors' is true when you don't provide, by design, factories.
Sergey Alexandrovich Kryukov 5-Oct-14 12:34pm    
Sure, 5ed.
—SA
This is the usual and important technique, but pay attention that those read-only properties are never mentioned in the class.

It may mean one of the two things: 1) not all code of the class is shown; some code which uses this private setter should exist, otherwise the setter would make no sense at all; 2) all code of the class is show, which means that the code of the class is a big mistake: if some private setter is never used in the class, it cannot be used at all, it means that the property can never be set anywhere.

See also my comments to Solution 1 and Solution 2.

—SA
 
Share this answer
 
v2
Comments
CPallini 5-Oct-14 4:31am    
True, 5.
Sergey Alexandrovich Kryukov 5-Oct-14 12:35pm    
Thank you, Carlo.
—SA
This is called readonly property. Suppose your property depends on some other property or logic inside your class and you don't want to let your object set that property manually, then this readonly property will be more logical.

Example: Suppose you have two radio button. based on your selection you want to enable or disable some controls. You should decide/set that controls' visibility/state in radio button's selection value property.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Oct-14 8:26am    
"Readonly property" is inaccurately put. This is read-write property for the code of the class, and read-only property for all other classes.

Probably you did not pay attention that the private setters are never used in the shown class. It makes the whole class badly wrong.

Please see my answer.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900