Events are described in chapter 5 this means youre

Info icon This preview shows pages 95–97. Sign up to view the full content.

View Full Document Right Arrow Icon
be a keyword in property or event accessors. (Events are described in Chapter 5 .) This means you’re allowed to use value as an identifier in other contexts—for example, you can write a method that takes a pa- rameter called value . You can’t do that with other keywords—you can’t have a parameter called class , for example. This is a very flexible system indeed. You can provide properties that provide real stor- age in the class to store their data, or calculated properties that use any mechanism you like to get and/or set the values concerned. This choice is an implementation detail hidden from users of our class—we can switch between one and the other without changing our class’s public face. For example, we could switch the implementation of these speed properties around so that we stored the value in kilometers per hour, and calculated the miles per hour— Example 3-10 shows how these two properties would look if the “real” value was in km/h. Example 3-10. Swapping over the real and calculated properties public double SpeedInMilesPerHour { get { return SpeedInKilometersPerHour / 1.609344; } set { SpeedInKilometersPerHour = value * 1.609344; } } public double SpeedInKilometersPerHour { get; set; } As far as users of the Plane class are concerned, there’s no discernible difference between the two approaches—the way in which properties work is an encapsulated implemen- tation detail. Example 3-11 shows an updated Main function that uses the new prop- erties. It neither knows nor cares which one is the “real” one. Defining Classes | 71
Image of page 95

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example 3-11. Using the speed properties static void Main(string[] args) { Plane someBoeing777 = new Plane("BA0049"); someBoeing777.SpeedInMilesPerHour = 150.0; Console.WriteLine( "Your plane has identifier {0}, " + "and is traveling at {1:0.00}mph [{2:0.00}kph]", someBoeing777.Identifier, someBoeing777.SpeedInMilesPerHour , someBoeing777.SpeedInKilometersPerHour ); someBoeing777.SpeedInKilometersPerHour = 140.0; Console.WriteLine( "Your plane has identifier {0}, " + "and is traveling at {1:0.00}mph [{2:0.00}kph]", someBoeing777.Identifier, someBoeing777.SpeedInMilesPerHour , someBoeing777.SpeedInKilometersPerHour ); Console.ReadKey(); } Although our public API supports two different units for speed while successfully keeping the implementation for that private, there’s something unsatisfactory about that implementation. Our conversion relies on a magic number ( 1.609344 ) that appears repeatedly. Repetition impedes readability, and is prone to typos (I know that for a fact. I’ve typed it incorrectly once already this morning while preparing the example!) There’s an important principle in programming: don’t repeat yourself (or dry , as it’s sometimes abbreviated). Your code should aim to express any single fact or concept no more than once, because that way, you only need to get it right once. It would be much better to put this conversion factor in one place, give it a name, and refer to it by that instead. We can do that by declaring a field .
Image of page 96
Image of page 97
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern