2.6 Overloading Class Constructors

 <  Day Day Up  >  

You want to change the way an object is created by overloading its default constructor.


Technique

Create a new method within your class with public access that has the same name as your class and at least one parameter. You must create the method within the source code window because the Add Method dialog does not let you create overloaded constructors.

Comments

Overloaded constructors offer a convenient way for users of a class to initialize an object without having to call a number of methods or properties after the class is created. Overloading a constructor is similar to overloading any other method with the exception, of course, being the lack of a return value. You can easily distinguish different constructors within a class by noting their different signatures.

You are free to have as many constructors for a class as you want. However, you can also have no callable constructors for a class by creating a private constructor, which prevents any object instance being created from that class. You might find it advantageous if your class, for example, emulates a data type and only contains static methods. In this instance, by using a private access modifier on the constructor, you only allow any client to gain access to the class through the type and not an instance of an object. If you want the benefit of having a constructor to initialize internal data within your class, while still preventing instances of the class from being created, you can create a static constructor. A static constructor is called before any instance of the class is created and before any static members are accessed. Any client can call a static method within the class, and the Common Language Runtime (CLR) first calls the static constructor to allow you to perform any initialization steps. To declare a static constructor, use the static keyword followed by the name of the class. A static constructor does not contain an access modifier, and it also does not contain any parameters because it is called by the CLR and never directly from a client.

You might need to call other constructors from within a different constructor to ensure member variables are properly initialized . Listing 2.5 contains the Television class used throughout this chapter. You can see that in addition to a default constructor, an overloaded constructor initializes the private channel member variable. You can also see how the channel member variable can be initialized if you use the default constructor by calling the overloaded constructor with the default channel for the class.

Listing 2.5 Overloading a Constructor Is Similar to Overloading a Regular Class Method
 using System; namespace _6_Constructors {     public class Television     {         public Television() : this(2)       {       }         public Television( int initChannel )         {             channel = initChannel;         }         private int channel;         public int GetChannel()         {             return channel;         }     }     class Class1     {         [STAThread]       static void Main(string[] args)       {           Television tv = new Television(42);              Console.WriteLine( "TV Channel = {0}", tv.GetChannel() );       }     } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net