Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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 " )

Code:
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
Default

Thanks a lot , very clear !

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


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





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