Class Constructors


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Class Constructors

  1. #1
    Join Date
    May 2006
    Posts
    12

    Class Constructors

    Hi, I'm pacify and this is my first time posting.

    Anyways, I have simple, class design type of question.

    I'm reading up a little bit on C# programming. The book that I'm reading recommends creating constructors in the following way: "the root constructor should represent the constructor with the most parameters. This should then call the constructor with the next level of paramets setting any data not handled by that constructor itself"(Guide to C# and...... )

    The book goes on to talk about a constructor syntax:

    Code:
    ClassConstructor(<paramaters>) : this(person){
    ....
    }
    ......where ": this(person)" calls the next, most parameter encompassing constructor.

    I do not understand the reasoning behind calling all the constructors in a class. It seems to me that some variables would be continously reinitialized as your progressed from the simplest, 0-parameter constructor, to the most parameter encompassing (most overloaded constructor).

    Here is the example from the book:

    Code:
    using System;
    
    public class Account{
         private double balance = 0.0;
         private String name= "";
    
         Account(double amount, String person) : this(person){
           balance = amount;
           Console.WriteLine("(double amount, String person) constructor");
         }
    
         Account(String person) : this(){
           name = person;
           Console.WriteLine("(String person) constructor"));
         }
    
         Account(){
          Console.WriteLine("Default Constructor");
         }
    
         public static void Main(){
           Account acc = new Account(120.00, "J B");
         }
    }
    -------output --------------
    Default Constructor
    (String person) constructor
    (double amount, String person) constructor


    Bottom line, what is the point of this?

  2. #2
    Join Date
    Feb 2004
    Location
    Indianapolis, IN
    Posts
    426
    One reason -

    If, when I created an object, I wanted to do something additional, I could make the change in one loacation and know it will be implimented regardless of which constructor is called. If I had made each of the constructors separate, then I'd have to make the change in each one, thus opening up the chance that I'd forget or mess one up....

    Code:
    using System;
    
    public class Account{
         private double balance = 0.0;
         private String name= "";
    
         Account(double amount, String person) : this(person){
           balance = amount;
           Console.WriteLine("(double amount, String person) constructor");
         }
    
         Account(String person) : this(){
           name = person;
           Console.WriteLine("(String person) constructor"));
         }
    
         Account(){
          Console.WriteLine("Default Constructor");
          // do something here and it happens regardless of which constructor I call....
          Console.WriteLine("Doing something additional.....");
    
         }
    
         public static void Main(){
           Account acc = new Account(120.00, "J B");
         }
    }
    -------------------------------------------------
    Bradley L. Jones
    Author, and more.
    QuinStreet Enterprise Network / Developer.com Network
    (Developer.com, CodeGuru.com, DevX, jGuru, and more!)

    -------------------------------------------------

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  2. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  3. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  4. Replies: 13
    Last Post: 10-03-2001, 05:05 PM
  5. Help needed before 25/1/01!!!!
    By Chris reid in forum Java
    Replies: 3
    Last Post: 02-11-2001, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center