Instead of repeating all the code from the first

Info icon This preview shows pages 111–114. Sign up to view the full content.

just took the polar coordinates, and initialized the altitude to zero by default. Instead of repeating all the code from the first constructor, we could just add this extra con- structor to our definition for PolarPoint3D , as shown in Example 3-30 . Example 3-30. Sharing common initialization code public PolarPoint3D(double distance, double angle) : this(distance, angle, 0) { } public PolarPoint3D( double distance, double angle, double altitude) : this() { Distance = distance; Angle = angle; Altitude = altitude; } Incidentally, this syntax for calling one constructor from another works equally well in classes, and is a great way of avoiding code duplication. Value Types and Reference Types | 87
Image of page 111

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

Too Many Constructors, Mr. Mozart You should be careful of adding too many constructors to a class or struct . It is easy to lose track of which parameters are which, or to make arbitrary choices about which constructors you provide and which you don’t. For example, let’s say we wanted to add yet another constructor to PolarPoint3D that lets callers pass just the angle and altitude, initializing the distance to a default of zero, as Example 3-31 shows. Example 3-31. A constructor too far public PolarPoint3D( double altitude, double angle ) : this( 0, angle, altitude ) { } Even before we compile, we can see that there’s a problem—we happen to have added the altitude parameter so that it is the first in the list, and angle stays second. In our main constructor, the altitude comes after the angle. Because they are both just doubles, there’s nothing to stop you from accidentally passing the parameters “the wrong way round.” This is the exactly the kind of thing that surprises users of your class, and leads to hard-to-find bugs. But while inconsistent parameter ordering is bad design, it’s not a showstopper. However, when we compile, things get even worse. We get another error: Type 'PolarPoint3D' already defines a member called 'PolarPoint3D' with the same parameter types We have too many constructors. But how many is too many? Overloading When we define more than one member in a type with the same name (be it a con- structor or, as we’ll see later, a method) we call this overloading . Initially, we created two constructors (two overloads of the constructor) for Polar Point3D , and they compiled just fine. This is because they took different sets of param- eters . One took three doubles, the other two. In fact, there was also the third, hidden constructor that took no parameters at all. All three constructors took different num- bers of parameters, meaning there’s no ambiguity about which constructor we want when we initialize a new PolarPoint3D . The constructor in Example 3-31 seems different: the two doubles have different names. Unfortunately, this doesn’t matter to the C# compiler—it only looks at the types of the parameters, and the order in which they are declared. It does not use names for 88 | Chapter 3: Abstracting Ideas with Classes and Structs
Image of page 112
disambiguation. This should hardly be surprising, because we’re not required to pro- vide argument names when we call methods or constructors. If we add the overload in Example 3-31
Image of page 113

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

Image of page 114
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