298 Object-Based Programming Chapter 8 Providing set and (Make web site)
Monday, October 22nd, 2007298 Object-Based Programming Chapter 8 Providing set and get capabilities appears to be the same as making the instance variables public. However, this is another one of C# s subtleties that makes the language so attractive from a software-engineering standpoint. If an instance variable is public, the instance variable can be read or written to by any method in the program. If an instance variable is private, a public get accessor seems to allow other methods to read the data at will. However, the getaccessor can control the formatting and display of the data. Similarly, a public set accessor can scrutinize attempts to modify the instance variable s value, thus ensuring that the new value is appropriate for that data member. For example, an attempt to setthe day of the month to 37 would be rejected, and an attempt to seta person s weight to a negative value would be rejected. So, setand getaccessors can provide access to private data, but the implementation of these accessors controls what the client code can do to the data. The declaration of instance variables as privatedoes not guarantee their integrity. Programmers must provide validity checking C# provides only the framework with which programmers can design better programs. Testing and Debugging Tip 8.1 Methods that set the values of private data should verify that the intended new values are valid; if they are not, the set accessors should place the private instance variables into an appropriate consistent state. The set accessors of a property cannot return values indicating a failed attempt to assign invalid data to objects of the class. Such return values could be useful to a client of a class when handling errors. The client could take appropriate actions if the objects occupy invalid states. Chapter 11 presents exception handling a mechanism that can be used to indicate attempts to set an object s members to invalid values. Figure 8.6 enhances our Timeclass, now called Time3, to include properties for the private instance variables hour, minute and second. The set accessors of these properties strictly control the setting of the instance variables to valid values. An attempt to set any instance variable to an incorrect value causes the instance variable to be set to zero (thus leaving the instance variable in a consistent state). Each get accessor returns the appropriate instance variable s value. This application also introduces enhanced GUI event-handling techniques, as we define a GUI (Fig. 8.7) that includes several buttons the user can click to manipulate the time stored in a Time3object. 1 // Fig. 8.6: Time3.cs 2 // Class Time2 provides overloaded constructors. 3 4 using System; 5 6 // Time3 class definition 7 public class Time3 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 Fig. 8.6 Fig. 8.Fig.Fi 8.6g. 8.66Properties provide controlled access to an object s data. (Part 1 of 3.) Fig. 8.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.