View Single Post
  #2 (permalink)  
Old December 5th, 2010, 02:41 PM
DrPurdum DrPurdum is offline
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)