Multiple constructors in a class definition : Class Definition « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » Class DefinitionScreenshots 
Multiple constructors in a 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 (10128496);
            POINT pt1 = new POINT (1012);
            POINT pt2 = new POINT (8496);
            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;
    }
}



           
       
Related examples in the same category
1. A simple inventory exampleA simple inventory example
2. Declaring and Defining Classes
3. Declaring Class Instances
4. Assign value to classAssign value to class
5. Declare class and use itDeclare class and use it
6. Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
7. Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
8. illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
9. Illustrates nested classesIllustrates nested classes
10. Demonstrate the use of a nested class to contain dataDemonstrate the use of a nested class to contain data
11. Show name hiding in a derived classShow name hiding in a derived class
12. Illustrates hidingIllustrates hiding
13. Variable in and out a classVariable in and out a class
14. Create classCreate class
15. A program that uses the Building classA program that uses the Building class
16. This program creates two Building objectsThis program creates two Building objects
17. Return an objectReturn an object
18. Uses a class from Example16_3a.cs
19. A Simple C# ClassA Simple C# Class
w___w_w._j___a_v__a___2___s._c___o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.