Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 5th, 2010, 10:49 AM
Registered User
Join Date: Jan 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
  #2 (permalink)  
Old December 5th, 2010, 02:41 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default 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.
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #3 (permalink)  
Old December 5th, 2010, 02:55 PM
Registered User
Join Date: Jan 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks a lot , very clear !

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties with "web.config" and "ASPNETDB" CFRham BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 2 July 3rd, 2010 10:19 AM
Need code corrections "get sub or function not defined" error" bluesboytoo Excel VBA 1 October 22nd, 2009 11:12 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM

All times are GMT -4. The time now is 06:54 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.