Access specifier
Found it! (Page 240, second to last paragraph.)
I think the confusion arises because I refer back to a data definition in Chapter 9. If you look on page 225, you will see the statement:
private DateTime current;
which defined current as a private property within the clsDates object. In Chapter 10, you learn that setter and getter property methods are the proper way to change the value of a property within a class. This forces the programmer to use the code you write to change the state of any clsDates object the programmer instantiates.
If we don't use the private access specifier on current, you have no control over what gets assigned into current. By using the private access specifier on current, any time you change any aspect of the date (e.g., month, day, or year), you have the opportunity of checking what the user is trying to assign into the property. On page 240, for example, if the programmer is trying to assign a value into month, your code has the opportunity to insure that its value is reasonable (i.e., 1 to 12). If you make current a public property, you have no control over what gets assigned into current. The public access specifier turns your pristine piece of data into a prostitute that anyone can have their way with...not good. There are few reasons (none?) for making a class property method public, since you, by definition, lose some ability to check what gets assigned into that property. While there may be a few reasons for public properties, there aren't too many good ones. As a rule, make your property methods private and use getters and setters to gain better control over those properties.
I hope this helps...
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|