programming c# 4.0.pdf

Compiling again we get a new error message the

Info icon This preview shows pages 92–94. Sign up to view the full content.

Compiling again, we get a new error message: The property or indexer 'Plane.Identifier' cannot be used in this context because the set accessor is inaccessible The problem is with this bit of code from Example 3-3 : someBoeing777.Identifier = "BA0049"; We’re no longer able to set the property, because we’ve made the setter private (which means that we can only set it from other members of our class). We wanted to prevent the property from changing, but we’ve gone too far: we don’t even have a way of giving it a value in the first place. Fortunately, there’s a language feature that’s perfect for this situation: a constructor . Initializing with a Constructor A constructor is a special method which allows you to perform some “setup” when you create an instance of a class. Just like any other method, you can provide it with pa- rameters, but it doesn’t have an explicit return value. Constructors always have the same name as their containing class. Example 3-6 adds a constructor that takes the plane’s identifier. Because the construc- tor is a member of the class, it’s allowed to use the Identifier property’s private setter. Example 3-6. Defining a constructor class Plane { public Plane(string newIdentifier) { Identifier = newIdentifier; } public string Identifier { get; private set; } } 68 | Chapter 3: Abstracting Ideas with Classes and Structs
Image of page 92

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

Notice how the constructor looks like a standard method declaration, except that since there’s no need for a return type specifier, we leave that out. We don’t even write void , like we would for a normal method that returns nothing. And it would be weird if we did; in a sense this does return something—the newly created Plane —it just does so implicitly. What sort of work should you do in a constructor? Opinion is divided on the subject— should you do everything required to make the object ready to use, or the minimum necessary to make it safe? The truth is that it is a judgment call—there are no hard and fast rules. Developers tend to think of a constructor as being a relatively low-cost op- eration, so enormous amounts of heavy lifting (opening files, reading data) might be a bad idea. Getting the object into a fit state for use is a good objective, though, because requiring other functions to be called before the object is fully operational tends to lead to bugs. We need to update our Main function to use this new constructor and to get rid of the line of code that was setting the property, as Example 3-7 shows. Example 3-7. Using a constructor static void Main(string[] args) { Plane someBoeing777 = new Plane("BA0049"); Console.WriteLine( "Your plane has identifier {0}", someBoeing777.Identifier); Console.ReadKey(); } Notice how we pass the argument to the constructor inside the parentheses, in much the same way that we pass arguments in a normal method call.
Image of page 93
Image of page 94
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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