Multiple constructors in a class definition : Class Definition : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » Class Definition »

 

Multiple constructors in a class definition









    

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Constrct.cs - Demonstrates the use of multiple constructors
//               in a class definition.
//
//               Compile this program with the following command line:
//                   C:>csc Constrct.cs
//
namespace nsConstructor
{
    using System;
    struct POINT
    {
        public POINT (int cx, int cy)
        {
            this.cx = cx;
            this.cy = cy;
        }
        public int cx;
        public int cy;
    }
    public class Constrct
    {
        static public void Main ()
        {
            clsRect rc1 = new clsRect();
            clsRect rc2 = new clsRect (10, 12, 84, 96);
            POINT pt1 = new POINT (10, 12);
            POINT pt2 = new POINT (84, 96);
            clsRect rc3 = new clsRect (pt1, pt2);
        }
    }
    class clsRect
    {
// The following constructor replaces the default constructor
        public clsRect ()
        {
            Console.WriteLine ("Default constructor called");
            m_Left = m_Top = m_Right = m_Bottom  = 0;
        }
        public clsRect (int cx1, int cy1, int cx2, int cy2)
        {
            Console.WriteLine ("Constructor 1 called");
            m_Left = cx1;
            m_Top = cy1;
            m_Right = cx2;
            m_Bottom = cy2;
        }
        public clsRect (POINT pt1, POINT pt2)
        {
            Console.WriteLine ("Constructor 2 called");
            m_Left = pt1.cx;
            m_Top = pt1.cy;
            m_Right = pt2.cx;
            m_Bottom = pt2.cy;
        }
        public POINT UpperLeft
        {
            get {return(new POINT(m_Left, m_Top));}
            set {m_Left = value.cx; m_Top = value.cy;}
        }
        public POINT LowerRight
        {
            get {return(new POINT(m_Right, m_Bottom));}
            set {m_Right = value.cx; m_Bottom = value.cy;}
        }
        private int m_Left;
        private int m_Top;
        private int m_Right;
        private int m_Bottom;
    }
}



           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» Class Definition