p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 (http://p2p.wrox.com/forumdisplay.php?f=409)
-   -   Constructors and keyword " this " (http://p2p.wrox.com/showthread.php?t=81810)

pepitagold December 5th, 2010 10:49 AM

Constructors and keyword " this "
Reading an XNA book , i found this code ( i cut some irrelevant variables ,
the class is " Sprite " )


int millisecondsPerFrame;
protected Vector2 speed;
const int defaultMillisecondsPerFrame = 16;

public Sprite(Vector2 speed ) :this ( speed ,defaultMillisecondsPerFrame)

public Sprite(Vector2 speed , int millisecondsPerFrame)
    this.speed = speed;
    this.millisecondsPerFrame = millisecondsPerFrame;

The Author explains :

The only difference between the two constructors is that the second one requires a
millisecondsPerFrame variable, used to calculate the animation speed. Hence, the
first constructor will just call the second constructor (using the this keyword) and
pass to that constructor all of its parameters, as well as the constant representing the
default animation speed.

I don't understand clearly : is this something like the code explained by Dr.Purdum at page 235 ? ( constructors sloppiness )

Thanks a lot for any explanation

DrPurdum December 5th, 2010 02:41 PM

The this keyword and constructors
As you probably know, a default constructor is always called as part of object instantiation to initialize the members of a class if you don't write your own. This means that class members are set to normal default values (e.g., zero or null). If you're not happy with those default values for class members, you should write your own constructor to set them to whatever it is you want them to be. For example, if you are writing a membership class for a club in your local town, perhaps Denver, you might write your own constructor to initialize the city class property to "Denver" and the state class property to "CO". This way, when you fill in a new member, the city and state properties would already contain "Denver" and "CO". Therefore, the instantiation of a new instance of the membership object, as in

clsMembership myMember = new clsMembership();

would return with the city and state properties filled in with the default strings you selected.

Having done that, you might write another constructor that passes in the name of the new member, as in:

string name = "Bill Jones";
clsMembership myMember = new clsMembership(name);

Because this constructor takes an argument (i.e., name), the compiler knows to call a different constructor that uses the member's name as an argument. The problem is, unless you provide the code yourself, this constructor doesn't automatically call the first constructor to fill in the city and state defaults. Since it's never a good idea to write the code twice, the easiest way to write the second constructor is with the first line as:

public clsMembership(string memberName) : this()
// the second constructor code

Writing the second constructor in this manner guarentees that the use of the second constructor that passes in the member's name also calls the first constructor you wrote that fills in the city and state default data.

As a general rule, anytime you write a constructor yourself, its purpose is to initialize one or more class properties with values other than zero or null. If you write multiple constructors for the first class, it's usually a good idea that the other constructors get called, too, using the approach outlined above. You can write multiple constructors using different arguments to cause the class to be initialized in different ways, but more often than not, this seems to cause bugs later on. I think constructors should be used to initialize default values for all objects, and use methods to change other properties as needed rather than rely on the constructor logic to do it for you.

pepitagold December 5th, 2010 02:55 PM

Thanks a lot , very clear !

(Beside : i have a paper copy of your book [:)])

All times are GMT -4. The time now is 11:31 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.